Reputation: 133
I receive a NullPointerException error when starting my activity. My logcat says it is occurring in my activity but doesn’t specify where. I am trying to use SharedPreferences for the first time and don’t know where the error is occurring.
Here is my activity:
public class SharedPrefLoginActivity extends Activity {
private Button cancelBttn;
private EditText rname;
private EditText rpwrd;
private Button rBttn;
/** Called when the activity is first created. */
private SharedPreferences dhj;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dhj = this.getSharedPreferences("DHJ", MODE_WORLD_READABLE);
if(dhj.getString("password", null) !=null) {
setContentView(R.layout.register);
rname = (EditText) findViewById(R.id.reg_uname);
rpwrd = (EditText) findViewById(R.id.reg_pswd);
rBttn = (Button) findViewById(R.id.reg_button);
rBttn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SharedPreferences.Editor dhjEditor = dhj.edit();
dhjEditor.putString("rname", rname.getText().toString());
dhjEditor.putString("rpwrd", rpwrd.getText().toString());
dhjEditor.commit();
Toast.makeText(getApplicationContext(), "Credentials Saved!", Toast.LENGTH_SHORT).show();
}
}
);
}
else {
Intent i = new Intent(SharedPrefLoginActivity.this, AccessApp.class);
startActivity(i);
}
cancelBttn.setOnClickListener (new OnClickListener() {
@Override
public void onClick(View v) {
//Close application
finish();
}
});
}}
logcat:
09-14 09:27:26.244: E/AndroidRuntime(589): Uncaught handler: thread main exiting due to uncaught exception
09-14 09:27:26.293: E/AndroidRuntime(589): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.SharedPreferences.Login/com.SharedPreferences.Login.SharedPrefLoginActivity}: java.lang.NullPointerException
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.os.Looper.loop(Looper.java:123)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.ActivityThread.main(ActivityThread.java:4363)
09-14 09:27:26.293: E/AndroidRuntime(589): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 09:27:26.293: E/AndroidRuntime(589): at java.lang.reflect.Method.invoke(Method.java:521)
09-14 09:27:26.293: E/AndroidRuntime(589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-14 09:27:26.293: E/AndroidRuntime(589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-14 09:27:26.293: E/AndroidRuntime(589): at dalvik.system.NativeStart.main(Native Method)
09-14 09:27:26.293: E/AndroidRuntime(589): Caused by: java.lang.NullPointerException
09-14 09:27:26.293: E/AndroidRuntime(589): at com.SharedPreferences.Login.SharedPrefLoginActivity.onCreate(SharedPrefLoginActivity.java:58)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
Upvotes: 0
Views: 445
Reputation: 4816
These lines
Caused by: java.lang.NullPointerException
09-14 09:27:26.293: E/AndroidRuntime(589): at com.SharedPreferences.Login.SharedPrefLoginActivity.onCreate(SharedPrefLoginActivity.java:58)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
Let's you know that you're NullPointer is coming from the onCreate method. You must first instantiate your cancelButtn variable before you register it's listener. I wouldn't think it's an issue with the dhj member since it's not attached to a view, so the layout doesn't need to be inflated before it can be referenced..although it would be wise to set it's value after a call to setContentView().
Upvotes: 0
Reputation: 2205
You forgot to set the cancelBttn
field:
cancelBttn = (Button) findViewById(R.id.cancel_button); // Insert your R.id.XXX here.
Also move
setContentView(R.layout.register);
to right after super.onCreate(savedInstanceState);
. The NullPointerException
occurs because the layout isn't inflated. You can see that in the last 4 lines of the stacktrace.
Upvotes: 3
Reputation: 10343
Probably this line is causing it:
cancelBttn.setOnClickListener (new OnClickListener()
I don't see any instantiation for cancelBttn
so it's null on this line...
Upvotes: 2