Android NullPointerException尽管它不是null? [重复]

时间:2021-03-16 17:04:24

This question already has an answer here:

这个问题在这里已有答案:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exampledinLoginActivity}: java.lang.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();
        }

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).

这是我的应用程序的第一个活动。登录活动。它检查db是否存在,如果db存在且在数据库中,loginremember为true,它将转到mainactivirt directlry而没有setcontent(这个工作)。

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

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

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

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

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

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

in my activity, the code is like that

在我的活动中,代码就是这样

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

it shows first line.

它显示第一行。

1 个解决方案

#1


3  

A null check should be made with

应该使用空值检查

dbHandler.isLoginRemember() != null

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

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

#1


3  

A null check should be made with

应该使用空值检查

dbHandler.isLoginRemember() != null

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

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