new_jerry new_jerry - 6 months ago 24
Java Question

Java null pointer exception on using simple Intent Extra

When I try to send some text over to the next

Activity
the program crashes saying "null pointer exception unable to start activity" but without using
Intent Extra
the
Intent
works fine.

Main Activity

public class MainActivity extends Activity implements OnClickListener{
EditText et;

public final static String KEY="com.example.myprotrial";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
Button btn =(Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this,MyActivity.class);
i.putExtra(KEY, et.getText().toString());
startActivity(i);
}

}


MyActivity

public class MyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Intent intent=getIntent();
String a = intent.getStringExtra(MainActivity.KEY);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(a);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
}


LogCat

12-09 13:58:33.115: E/AndroidRuntime(1588): FATAL EXCEPTION: main
12-09 13:58:33.115: E/AndroidRuntime(1588): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myprotrial/com.example.myprotrial.MyActivity}: java.lang.NullPointerException
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.os.Looper.loop(Looper.java:137)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-09 13:58:33.115: E/AndroidRuntime(1588): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 13:58:33.115: E/AndroidRuntime(1588): at java.lang.reflect.Method.invoke(Method.java:525)
12-09 13:58:33.115: E/AndroidRuntime(1588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-09 13:58:33.115: E/AndroidRuntime(1588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-09 13:58:33.115: E/AndroidRuntime(1588): at dalvik.system.NativeStart.main(Native Method)
12-09 13:58:33.115: E/AndroidRuntime(1588): Caused by: java.lang.NullPointerException
12-09 13:58:33.115: E/AndroidRuntime(1588): at com.example.myprotrial.MyActivity.onCreate(MyActivity.java:18)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.Activity.performCreate(Activity.java:5133)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-09 13:58:33.115: E/AndroidRuntime(1588): ... 11 more

Answer

The problem is that your LogCat doesn't tell on which line the NPE occurs.

From your code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Intent intent=getIntent();
    String a = intent.getStringExtra(MainActivity.KEY);
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(a);
}

The if the NPE is occurring on intent.getStringExtra then your intent is null. If it's on tv.setText(a) then most likely the activity_my layout doesn't have textView1 in it. Step through it in the debugger and watch the instance variables.

Comments