user2845482
user2845482

Reputation: 57

Android Null Pointer Exception on Textview setText

Android Null pointer exception thrown on settext ran in UI thread forced

runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String name=portalData.getFullname();

                        setContentView(R.layout.gradebook);
                            tname.setText(name);

                           }
                });

the

i have made a static member of TextView on Create Im doing this

super.onStart();

    setContentView(R.layout.gradebook);
      tname=(TextView)findViewById(R.id.fullname); 

cannot understand what is causing this stacktrace

10-15 15:19:07.090: E/AndroidRuntime(1604): FATAL EXCEPTION: main
10-15 15:19:07.090: E/AndroidRuntime(1604): java.lang.NullPointerException
10-15 15:19:07.090: E/AndroidRuntime(1604):     at com.example.campus.Gradebook$ObservableDemo$6.run(Gradebook.java:518)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at android.os.Handler.handleCallback(Handler.java:730)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at android.os.Looper.loop(Looper.java:137)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at java.lang.reflect.Method.invoke(Method.java:525)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at dalvik.system.NativeStart.main(Native Method)

here is the XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".vbook" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="70dp"
        android:layout_marginTop="40dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#007FFF" />
</RelativeLayout>

Upvotes: 4

Views: 22738

Answers (4)

Bhupat Bheda
Bhupat Bheda

Reputation: 1978

As @Ravi said Set contentview should always come before binding view. In addition to that, there is no id like R.id.fullname in your xml posted. Instead there is textview3 So, try this code:

setContentView(R.layout.gradebook);// I assume gradebook is your xml file 

tname=(TextView)findViewById(R.id.textView3);  //textView3 is the id you passed in TextView. 

Hope this helps you.

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

Reverse these

  TextView tname=(TextView)findViewById(R.id.fullname); 
  setContentView(R.layout.vbook);

findViewById looks for a view with the id mentioned in the current inflated layout.

So if your getting NPE even after reversing the above two statements you should check your vbook.xml whether you have a textview with id fullname.

Also all initializations of your view is generally done in onCreate which is the ui thread. I am not sure what you are trying to do.

You have the below in vbook.xml

   <TextView
    android:id="@+id/textView3"

So change to TextView tname=(TextView)findViewById(R.id.textView3)

Upvotes: 13

Lior Ohana
Lior Ohana

Reputation: 3527

The null reference is since the TextView is null. In order for

TextView tname=(TextView)findViewById(R.id.fullname);

to return the TextView you must initialize the layout first. Change the code to and you'll do fine:

setContentView(R.layout.vbook);    
TextView tname=(TextView)findViewById(R.id.fullname); 

Upvotes: 3

Ravi
Ravi

Reputation: 3772

setContentView(layout) should always be called before you map your views using findViewById(id)

If same error occurs after reversing it make sure the id you are trying to map is available in your layout file

Upvotes: 1

Related Questions