0
votes

I want to skip login activity using shared preferences but whenever I try to implement this code java.lang.nullpointerexception error occur. This is a java code of launch or login screen. here i have no splash screen.

public class MainActivity extends Activity {
    Button sub;
    SharedPreferences prefs;
    EditText useret,pwdet;
    String IS_LOGIN = "IsLoggedIn";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(this.isLoggedIn()==true) {
            checklogin();
        }
        setContentView(R.layout.activity_main);
        useret=(EditText) findViewById(R.id.etuser);
        pwdet=(EditText) findViewById(R.id.etpwd);
        sub=(Button) findViewById(R.id.submit);
        sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username=useret.getText().toString();
                String password=pwdet.getText().toString();
                prefs=getSharedPreferences("codelearn_twitter", MODE_PRIVATE);
                SharedPreferences.Editor editor=prefs.edit();
                editor.putString("key_for_username",username);
                editor.putString("key_for_passwrd",password);
                editor.putBoolean(IS_LOGIN, true);
                editor.commit();

                Intent i=new Intent(MainActivity.this,listactivity.class);
                startActivity(i);
            }
        });
    }
    public void checklogin(){
        if(this.isLoggedIn()==true){
            Intent i=new Intent(MainActivity.this,listactivity.class);
            startActivity(i);
            finish();
        }
    }
    public boolean isLoggedIn(){
        return prefs.getBoolean(IS_LOGIN, false);
    }
}

below is a list of error..

08-19 17:58:17.061 14978-14978/com.example.viren.codelearn
E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.viren.codelearn, PID: 14978 java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.viren.codelearn/com.example.vire n.codelearn.MainActivity}:java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2305) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2363) at android.app.ActivityThread.access$900(ActivityThread.java:161) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1265) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5356) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1265) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.viren.codelearn.MainActivity.isLoggedIn (MainActivity.java:56) at com.example.viren.codelearn.MainActivity.onCreate (MainActivity.java:23) at android.app.Activity.performCreate(Activity.java:5426) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2269)  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2363)

   
           

4
try doing if(this.isLoggedIn()==true) { checklogin(); } after setContentView is called.Bhargav
By the way, you shouldn't ever check a boolean value with an equal-to operator against true/false. That's just silly. if(this.isLoggedIn()){...}d0nut

4 Answers

2
votes

Looking at your stacktrace, you can see that at line 56 you call isLoggedIn which calls your prefs variable, that is not initialized yet (it's null), thus your getting the null pointer exception.

Make the prefs variable accesible for your whole class, and initialize it before calling isLoggedIn or initialize it inside the isLoggedIn method.

0
votes

your prefs are not initialized at that point... you must

 prefs=getSharedPreferences("codelearn_twitter", MODE_PRIVATE);

before returning

return prefs.getBoolean(IS_LOGIN, false);
0
votes

As you called isLoggedIn() which returns the value from Shared Preferences but the shared preferences instance is not initialized yet and is null so before calling

if(this.isLoggedIn()==true) {
        checklogin();
    }

you must initialize shared preference variable

prefs=getSharedPreferences("codelearn_twitter", MODE_PRIVATE);

That is :

 prefs=getSharedPreferences("codelearn_twitter", MODE_PRIVATE);

 if(this.isLoggedIn()==true) {
            checklogin();
        }
0
votes

You have to initialize your SharedPreferences first before calling it.

Move this:

prefs = getSharedPreferences("codelearn_twitter", MODE_PRIVATE);

Above this:

if (this.isLoggedIn() == true) {
    checklogin();
}

To be like this:

prefs = getSharedPreferences("codelearn_twitter", MODE_PRIVATE);

if (this.isLoggedIn() == true) {
    checklogin();
}