Android NullPointerException尽管它不是null?

时间:2016-06-02 15:53:30

标签: java android nullpointerexception

  

java.lang.RuntimeException:无法启动活动   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();
        }

这是我申请的第一项活动。登录活动。它检查db是否存在,如果db存在且在数据库中,loginremember为true,它将转到mainactivirt directlry而没有setcontent(这有效)。

但是如果db不存在,则设置内容并获取用户名密码。或者如果loginremember为false。

引起:java.lang.NullPointerException                                                                                          在com.example.caneraydin.androidwithlogin.LoginActivity.onCreate(LoginActivity.java:167)

错误就是该行:

 if (wasDbExisted &&

但是日志说

login activity wasdbexister=true

总结

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

这是我的清单

<?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>

这是dbhandler类的dbexists方法

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;
}

在我的活动中,代码就像那样

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

它显示第一行。

1 个答案:

答案 0 :(得分:3)

应使用

进行空检查
dbHandler.isLoginRemember() != null

在null对象上使用.equals()将导致NPE