Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 582
user2845482 Avatar asked Dec 03 '22 21:12

user2845482


2 Answers

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)

like image 58
Raghunandan Avatar answered Dec 10 '22 12:12

Raghunandan


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); 
like image 34
Lior Ohana Avatar answered Dec 10 '22 11:12

Lior Ohana