0

My mobile application stopped working after I added the checking for ip section in my onCreate function to make registered users bypass the login screen. This might have something to do with the variable declarations but I'm still not sure what's going on.

private EditText etUsername;
private EditText etPassword;
private EditText etIpAddress;
private Button btnLogin;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    String username = etUsername.toString();
    String password = etPassword.toString();
    String ipAddress = etIpAddress.toString();

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
    if (sharedPreferences.contains("ip")) {
        performLogin(username, password, sharedPreferences.getString("ip", ipAddress));
    }

    // declaring variebles
    etUsername = (EditText)findViewById(R.id.etUsername);
    etPassword= (EditText)findViewById(R.id.etPassword);
    btnLogin = (Button)findViewById(R.id.btnLogin);
    etIpAddress = (EditText) findViewById(R.id.etIpAddress);

    // setting up things for login button
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String ipAddress = etIpAddress.getText().toString();

            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);

            sharedPreferences.edit()
                    .putString("ip", ipAddress)
                    .apply();

            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            performLogin(username, password, ipAddress);
        }
    });
}

private void performLogin(String username, String password, String ipAddress) {
    try {
        Device.login(username, password, ipAddress, this);
    } catch (JSONException e) {
        onLoginFailure(e);
    }
}

Logcat:

06-06 16:40:11.408 4000-4000/com.itemlocator.findit E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.itemlocator.findit, PID: 4000
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.itemlocator.findit/com.itemlocator.findit.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.widget.EditText.toString()' on a null object reference
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.widget.EditText.toString()' on a null object reference
                                                                          at com.itemlocator.findit.LoginActivity.onCreate(LoginActivity.java:32)
                                                                          at android.app.Activity.performCreate(Activity.java:6237)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:148) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Lotse
  • 51
  • 8
  • Share you logcat file – SaravInfern Jun 06 '16 at 06:48
  • What do you mean by `String username = etUsername.toString();` line ?? it should be `String username = etUsername.getText().toString();` and after `etUsername = (EditText)findViewById(R.id.etUsername);` line – ρяσѕρєя K Jun 06 '16 at 06:51
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – OneCricketeer Jun 06 '16 at 06:56

2 Answers2

1
String username = etUsername.toString();
String password = etPassword.toString();
String ipAddress = etIpAddress.toString();

will produce NullPointerExceptions since your EditTexts are not initialized yet. You have to call first:

etUsername = (EditText)findViewById(R.id.etUsername);
etPassword= (EditText)findViewById(R.id.etPassword);
btnLogin = (Button)findViewById(R.id.btnLogin);
etIpAddress = (EditText) findViewById(R.id.etIpAddress);

Also I think you mean

String username = etUsername.getText().toString();
Dominik Vincenz
  • 445
  • 3
  • 10
  • yep, this did the trick thanks. Just one more thing, when I reopen the application it's says invalid credentials in the login screen which I need it to bypass this. How can I approach this? – Lotse Jun 06 '16 at 07:05
  • your variables username, password and ipAddress will be empty, since these variables are initialized from the content of the EditTexts. These will probably be empty on start. You need to save username,... and restore it before calling your login code – Dominik Vincenz Jun 06 '16 at 07:11
  • So just for testing purposes, I'm currently calling data from a table called Device and that only stored users can access the application. Do we save the data in the application so we can call it later or is there another approach? – Lotse Jun 06 '16 at 07:47
0

In Login Screen put this code after login_WS call

`SharedPreferences pref = getActivity().getSharedPreferences(MyPre, Context.MODE_PRIVATE); Editor editor = pref.edit(); editor.putString(PARAMS.KEY_PROFILE_DATA, jsonObject.get("data").toString()); editor.putString(PARAMS.KEY_USER_ID, ConstantData.USER_ID); editor.putBoolean(PARAMS.KEY_STAY_LOGGED_IN, true); editor.commit();

In splash screen put this code

` private Runnable mRunnable = new Runnable() {

    @Override
    public void run() {

        pref = getSharedPreferences(MyPre, Context.MODE_PRIVATE);

        //Log.i("splash","Boolen value 1" + pref.getBoolean(PARAMS.KEY_STAY_LOGGED_IN,false));

         is = Boolean.parseBoolean(String.valueOf((pref.getBoolean(PARAMS.KEY_STAY_LOGGED_IN, false))));

        Log.i("splash","Boolen value 2" + is);

        if (is) {

            Intent mIntent = new Intent(SplashScreenActivity.this, MainActivity.class);
            startActivity(mIntent);
            // overridePendingTransition(R.anim.enter, R.anim.no_anim);
            finish();

        } else {

            Intent mIntent = new Intent(SplashScreenActivity.this, LoginActivity.class);
            startActivity(mIntent);
            overridePendingTransition(R.anim.enter, R.anim.no_anim);
            finish();

        }`

Hope this works for you