Android java.lang.NullPointerException: println needs a message

15,992

In the catch block e.getMessage() may be null. Try this:

String msg = (e.getMessage()==null)?"Login failed!":e.getMessage();
Log.i("Login Error1",msg); 
Share:
15,992
jeff7
Author by

jeff7

Updated on June 04, 2022

Comments

  • jeff7
    jeff7 almost 2 years

    I have a problem with my app when I try to login.

    In fact, if I register (if the checkbox remerberMe isChecked() on register action, at the next connection the main activity is displayed otherwise the login activity appears) first, it works fine.

    However, the Login Activity doesn't work when it's called on the next connection after register action.

    Login Activity

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);
            setTitle(R.string.loginTitle);
    
            // Importing all assets like buttons, text fields
            inputUsername = (EditText) findViewById(R.id.username_value);
            inputPassword = (EditText) findViewById(R.id.password_value);
            btnLogin = (Button) findViewById(R.id.loginButton);
            btnExit = (Button) findViewById(R.id.exitButton);
            btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegister);
            rememberMe = (CheckBox) findViewById(R.id.rememberNameRegister);
    
            // Login button Click Event
            btnLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LoginAction();
                }
            });
    
            // Link to Register Screen
            btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View view) {
                    Intent i = new Intent(getApplicationContext(),
                            RegisterActivity.class);
                    startActivity(i);
                    finish();
                }
            });
    
            btnExit.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    finish();
    
                }
            });
        }
    
        public void LoginAction() {
            Log.i("Login", "Login Action");
            progressDialog = ProgressDialog.show(LoginActivity.this, null,
                    getResources().getString(R.string.loginProgressMessage), true);
    
            // try {
            runOnUiThread(new Runnable() {
                String username = inputUsername.getText().toString();
                String password = inputPassword.getText().toString();
    
                @Override
                public void run() {
                    Log.i("Login Run", "username " + username);
    
                    switch (username.length()) {
                    case 0:
                        blankUserName();
    
                        break;
                    }
    
                    switch (password.length()) {
                    case 0:
                        blankPassWord();
                        // thread.stop();
                        break;
                    }
    
                    try {
                        if (username.length() > 0 && password.length() > 0) {
                            Log.i("Login Run", "password " + password);
                            // Password pass = new Password(username, password);
                            DatabaseHelper db = new DatabaseHelper(
                                    getApplicationContext());
                            int count = db.getRowCount();
                            Log.i("Login", "getRowCompte " + count);
                            if (count == 1) {
                                Log.i("Login","getLogin1 ");
                                // if (db.getLogin(username, password) == 1) {
                                if (db.Login(username, password)== true) {
                                     Log.i("Login", "rememberMe.isChecked()");
                                    if (rememberMe.isChecked() == true) {
                                        statut = "on";
                                    } else if (rememberMe.isChecked() == false) {
                                        statut = "off";
                                    }
                                    Log.i("Login", "ok ischecked");
                                    Log.i("Login","getRowCompteStat "+ db.getRowCountStat());
                                    if (db.getRowCountStat() == 1) {
                                        db.UpdateStatut(statut);
                                        Log.i("Login", "getRowCompte " + count);
                                    }
                                    Toast.makeText(getApplicationContext(),
                                            "Student Moyenne \n   Bienvenu!",
                                            Toast.LENGTH_LONG).show();
                                    Log.i("Login Run", "Connecté");
                                    Intent moy = new Intent(
                                            getApplicationContext(),
                                            MoyenneMain.class);
                                    // Close all views before launching
                                    // Dashboard
                                    moy.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(moy);
                                    finish();
                                    db.close();
                                } else if (db.Login(username, password)==false){
                                    // if (db.getLogin(username, password) == 0) {
    //                              Log.i("Login Run", "faux password");
                                    Toast.makeText(LoginActivity.this,"Invalid Username/Password",Toast.LENGTH_LONG).show();
                                    Intent login = new Intent(getApplicationContext(),LoginActivity.class);
                                    login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(login);
                                    finish();
                                    db.close();
                                }
                            } else if (count == 0) {
                                Log.i("Login Run", "Enregistrez vous");
                                Toast.makeText(LoginActivity.this,
                                        "Enregistrez vous!", Toast.LENGTH_SHORT)
                                        .show();
                                Intent register = new Intent(
                                        getApplicationContext(),
                                        RegisterActivity.class);
                                register.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(register);
                                finish();
                                db.close();
                            }
                        }
    
                    } catch (Exception e) {
                        Log.i("Login Error1", e.getMessage());
                        Toast.makeText(LoginActivity.this, e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
            // } catch (Exception e) {
            // Thread.currentThread().destroy();
            // Log.i("Login Error2", e.getMessage());
            // Toast.makeText(LoginActivity.this, e.getMessage(),
            // Toast.LENGTH_LONG).show();
            // }
    
        }
    
        private void blankUserName() {
            Toast.makeText(LoginActivity.this, "Entrez un nom Utilisateur SVP!",
                    Toast.LENGTH_SHORT).show();
            Intent login = new Intent(getApplicationContext(), LoginActivity.class);
            login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(login);
            finish();
        }
    
        private void blankPassWord() {
            Toast.makeText(LoginActivity.this, "Entrez un mot de passe SVP!",
                    Toast.LENGTH_SHORT).show();
            Intent login = new Intent(getApplicationContext(), LoginActivity.class);
            login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(login);
            finish();
        }
    

    A part of DatabaseHelper

    public int getRowCountStat() {
            String countQuery = "SELECT  * FROM " + TABLE_STATUT;
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(countQuery, null);
            int rowCount = cursor.getCount();
            db.close();
            cursor.close();
    
            // return row count
            return rowCount;
        }
    
    public int getRowCount() {
            String countQuery = "SELECT  * FROM " + TABLE_LOGIN;
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(countQuery, null);
            int rowCount = cursor.getCount();
            db.close();
            cursor.close();
    
            // return row count
            return rowCount;
        }
    
     public boolean Login(String username, String password) throws SQLException 
            {
                Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_LOGIN + " WHERE username=? AND password=?"
                        , new String[]{username,password});
                if (mCursor != null) {           
                    if(mCursor.getCount() > 0)
                    {
                        return true;
                    }
                }
             return false;
            }
    
    public int UpdateStatut(String statut) {
                final static int idStat = 1;
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(STATUT, statut);
            return db.update(TABLE_STATUT, cv, ID_STAT + "=?",
                    new String[] { String.valueOf(idStat) });
        }
    

    Logcat

    10-24 01:48:08.819: E/AndroidRuntime(29242): FATAL EXCEPTION: main
    10-24 01:48:08.819: E/AndroidRuntime(29242): java.lang.NullPointerException: println needs a message
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at android.util.Log.println_native(Native Method)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at android.util.Log.i(Log.java:143)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at com.android.moyenne.activity.LoginActivity$4.run(LoginActivity.java:163)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at android.app.Activity.runOnUiThread(Activity.java:3707)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at com.android.moyenne.activity.LoginActivity.LoginAction(LoginActivity.java:78)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at com.android.moyenne.activity.LoginActivity$1.onClick(LoginActivity.java:46)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at android.view.View.performClick(View.java:2408)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at android.view.View$PerformClick.run(View.java:8816)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at android.os.Handler.handleCallback(Handler.java:587)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at android.os.Handler.dispatchMessage(Handler.java:92)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at android.os.Looper.loop(Looper.java:123)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at android.app.ActivityThread.main(ActivityThread.java:4627)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at java.lang.reflect.Method.invokeNative(Native Method)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at java.lang.reflect.Method.invoke(Method.java:521)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    10-24 01:48:08.819: E/AndroidRuntime(29242):    at dalvik.system.NativeStart.main(Native Method)
    10-24 01:48:12.260: I/Process(29242): Sending signal. PID: 29242 SIG: 9
    
  • jeff7
    jeff7 over 10 years
    I have done it, but now the message "Login failed!" appears in the logcat, and in the Toast (so e.getmessage returns null) and the Thread do not destroy automatically however getRowCount() returns 1 and the username and password that i entrer is the same than i entrered on registration. so db.Login(username, password) should return true but it returns false. to destroy the thread i want to use Thread.currentThread().destroy(); but destroy() is strikethrough. help me please
  • Amulya Khare
    Amulya Khare over 10 years
    Since it is going to the catch block, first thing you should do is find out what exception was thrown, do a e.printStackTrace() in the catch and report the error log. Also, since it is completely unrelated issue than what you had asked before, you might want to close this question and ask a new one so more people can help you out.