Tevin J
Tevin J

Reputation: 721

Why does this cause a NullPointerException?

I have a program which is as follows, Can anyone tell me what is the reason for getting an NPE.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        secondClass x = new secondClass();
        x.sText("Test Spring");

    }

}



public class secondClass extends MainActivity {

    public void sText(String s) {

        TextView text = (TextView) findViewById(R.id.text);
        text.setText(s);
    }

}

Mainfest

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    ...

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:id="@+id/text"
     />

</RelativeLayout>

LogCat

08-16 01:31:11.320  15032-15032/? E/Trace: error opening trace file: No such file or directory (2)
08-16 01:31:11.420      836-987/? E/EmbeddedLogger: App crashed! Process: com.example.myapplication
08-16 01:31:11.420      836-987/? E/EmbeddedLogger: App crashed! Package: com.example.myapplication v1 (1.0)
08-16 01:31:11.420      836-987/? E/EmbeddedLogger: Application Label: My Application
08-16 01:31:11.420  15032-15032/? E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
        at android.app.ActivityThread.access$600(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
        at android.os.Handler.dispatchMessage(Handler.java)
        at android.os.Looper.loop(Looper.java)
        at android.app.ActivityThread.main(ActivityThread.java)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:110)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at android.app.Activity.findViewById(Activity.java)
        at com.example.myapplication.secondClass.sText(secondClass.java:13)
        at com.example.myapplication.MainActivity.onCreate(MainActivity.java:14)
        at android.app.Activity.performCreate(Activity.java)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)

To my understanding, this should instantiate and initialize a secondClass object then call its sText() method. The sText method should then store a reference in TextView. Next it calls the setText() method on TextView, which changes the text in the TextView to what was passed into sText().

The logcat says it throws a NullPointerException but I'm not entirely sure why. If this is a rookie mistake, it's because I am a rookie. Thank you.

Upvotes: 0

Views: 404

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

You need to Override onCreate in your secondActivity and have setContentView(R.layout.mylayout). Then you can initialize your textview.

Also you need to start an Activity using intent

     Intent intent = new Intent(MainActivtiy.this,SecondActivity.class);
     startActivity(intent);

Remember to make an entry for the activity (SecondActivtiy) in manifest file

If you need to pass the string to second activity use intent.putExtra

      Intent intent = new Intent(MainActivtiy.this,SecondActivity.class);
      intent.putExtra("key",mystring); 
      startActivity(intent);

Then in secondActivity

    TextView tv ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mysecondlayout);
    tv = (TextView) findViewByid(R.id.textView1);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
    String value = extras.getString("key");
    tv.setText(value);
    //get the value based on the key
    }

http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, android.os.Bundle)

You can findViewById of the current view herarchy set to the activity.

Also you need to startActivity using intent rather doing this econdClass x = new secondClass()

Upvotes: 3

Related Questions