Pedro Teran Pedro Teran - 10 days ago 7
Java Question

Null pointer exception creating an intent

I'm trying to create an intent to a new activity on an application but a null pointer exception is thrown when the activity is created, which forces the emulator to close. Any ideas?

...
...
//connect objects with the xml view
searchbar = (EditText) findViewById(R.id.searchtxt);
btmsearch = (Button) findViewById(R.id.searchbtm);
searchopt = (TextView) findViewById(R.id.advsearchtxt);

searchopt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent bi = new Intent(getApplicationContext(), adv_search.class);
startActivity(bi);
}
});
//make the search when click on search buttom
btmsearch.setOnClickListener(new View.OnClickListener() {

...
...


The logcat of the application is the follwing:

02-29 19:54:42.313: D/AndroidRuntime(628): Shutting down VM
02-29 19:54:42.313: W/dalvikvm(628): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
02-29 19:54:42.313: E/AndroidRuntime(628): Uncaught handler: thread main exiting due to uncaught exception
02-29 19:54:42.323: E/AndroidRuntime(628): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.remotedata.firstapp/com.remotedata.firstapp.DataconectActivity}: java.lang.NullPointerException
02-29 19:54:42.323: E/AndroidRuntime(628): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
02-29 19:54:42.323: E/AndroidRuntime(628): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
02-29 19:54:42.323: E/AndroidRuntime(628): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
02-29 19:54:42.323: E/AndroidRuntime(628): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
02-29 19:54:42.323: E/AndroidRuntime(628): at android.os.Handler.dispatchMessage(Handler.java:99)
02-29 19:54:42.323: E/AndroidRuntime(628): at android.os.Looper.loop(Looper.java:123)
02-29 19:54:42.323: E/AndroidRuntime(628): at android.app.ActivityThread.main(ActivityThread.java:4203)
02-29 19:54:42.323: E/AndroidRuntime(628): at java.lang.reflect.Method.invokeNative(Native Method)
02-29 19:54:42.323: E/AndroidRuntime(628): at java.lang.reflect.Method.invoke(Method.java:521)
02-29 19:54:42.323: E/AndroidRuntime(628): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-29 19:54:42.323: E/AndroidRuntime(628): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
02-29 19:54:42.323: E/AndroidRuntime(628): at dalvik.system.NativeStart.main(Native Method)
02-29 19:54:42.323: E/AndroidRuntime(628): Caused by: java.lang.NullPointerException
02-29 19:54:42.323: E/AndroidRuntime(628): at com.remotedata.firstapp.DataconectActivity.onCreate(DataconectActivity.java:54)
02-29 19:54:42.323: E/AndroidRuntime(628): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
02-29 19:54:42.323: E/AndroidRuntime(628): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
02-29 19:54:42.323: E/AndroidRuntime(628): ... 11 more
02-29 19:54:42.334: I/dalvikvm(628): threadid=7: reacting to signal 3
02-29 19:54:42.334: E/dalvikvm(628): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

Answer

According to the code you gave, there are only two lines that could produce a NullPointerException.

Either it because searchopt is null (does R.id.searchtxt belong to you current layout ??) Or it is btmsearch that is null (does R.id.searchbtm belong to you current layout ??)