-1

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exampledinLoginActivity}: java.lang.NullPointerException

protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "login activity OnCreate************");

        super.onCreate(savedInstanceState);
        dbHandler = new DatabaseHandler(this);


        wasDbExisted = dbHandler.ifDatabaseExists();
        Log.d(TAG, "login activity wasdbexister="+wasDbExisted);
        if (wasDbExisted &&//if db exists, this becomes true to send to mainactivity. mainactivity needs to know if db existed or not 
                !dbHandler.isLoginRemember().equals(null)) {//isloginremember returns string, username if loginremember checked, if not only returns null
Intent i = new Intent(LoginActivity.this, MainActivity.class);
            i.putExtra("username", dbHandler.isLoginRemember());
            i.putExtra("wasDbExisted", wasDbExisted);

            startActivity(i);
            finish();
        }

This is first activity of my applicaiton. Login activity. It checks if db exists, if db exists and in database, loginremember is true, it goes to mainactivirt directlry without setcontent (this worked).

But if db doesnot exist , it setscontent and gets username password. or if loginremember false.

Caused by: java.lang.NullPointerException at com.example.caneraydin.androidwithlogin.LoginActivity.onCreate(LoginActivity.java:167)

error is that for that line:

 if (wasDbExisted &&

but logs say

login activity wasdbexister=true

to sum up

Log.d(TAG, "login activity wasdbexister="+wasDbExisted);//log output is true for this
        if (wasDbExisted &&//it gives nullexception for this

this is my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.caneraydin.androidwithlogin">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        <activity android:name=".LoginActivity"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity"
            android:screenOrientation="landscape">
        </activity>


    </application>

</manifest>

this is dbexists method of dbhandler class

public boolean ifDatabaseExists() {

        Log.d(TAG,"dbhandlerifdbexists");

        SQLiteDatabase db = this.getWritableDatabase();

        Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_OBJECT, null);//it is looking for object tble to check if db is null or not
        Boolean dbExists;

    if(mCursor.moveToFirst())
    {
        Log.d(TAG,"dbhandlerifdbexists true");
        dbExists = true;
    }

    else
    {
        Log.d(TAG,"dbhandlerifdbexists false");
        dbExists = false;
    }

        return dbExists;
}

in my activity, the code is like that

if (wasDbExisted &&
                !dbHandler.isLoginRemember().equals(null)) {

it shows first line.

user6412889
  • 39
  • 1
  • 8
  • 1
    Sure that the NPE points exactly to that, and not to the second check? Post the stacktrace please. – Seth Jun 02 '16 at 15:55
  • two checks are in two lines but. it shows first line. can it be? – user6412889 Jun 02 '16 at 15:56
  • 1
    change `!dbHandler.isLoginRemember().equals(null)` to `dbHandler.isLoginRemember() != null`, if your object is null, .equal() throw NPE. – Shayan Pourvatan Jun 02 '16 at 15:58
  • @shayanpourvatan Just add the stacktrace :) The exception. – Seth Jun 02 '16 at 16:01
  • From the error, `dbHandler.isLoginRemember()` is obviously null. Your compiler should be telling you that this `.equals(null)` is probably not what you want to do. – njzk2 Jun 02 '16 at 16:18
  • i think @developeralways have answered your problem. there is a bug in you code. – SAIR Jun 02 '16 at 16:38

1 Answers1

3

A null check should be made with

dbHandler.isLoginRemember() != null

Using .equals() on a null object will result in NPE

developeralways
  • 527
  • 4
  • 20