NullPointerException Android [duplicate]

2019-09-20 13:21发布

问题:

This question already has an answer here:

  • What is a NullPointerException, and how do I fix it? 12 answers

I am getting NullPointerException and I couldn't fix it. Could anyone please have a look at my code to see what the problem is and how to fix it. Thanks in advance.

DatabeseDEO.class

            public class DatabaseDEO  {
    private DatabaseHelper DbHelper;
     public DatabaseDEO(Context context) {
            DbHelper = new DatabaseHelper(context);
        }

        public DatabaseDEO() {

        }

        public ArrayList<String> getToSpinner() {
            ArrayList<String> list=new ArrayList<String>();
            SQLiteDatabase db = DbHelper.getReadableDatabase();
            db.beginTransaction();
            try
            {

                String selectQuery = "SELECT code FROM "+ Currency.NAME_TABLE;
                Cursor cursor = db.rawQuery(selectQuery, null);
                if(cursor.getCount() >0)

                {
                    while (cursor.moveToNext()) {

                        String currency= cursor.getString(cursor.getColumnIndex("code"));
                        list.add(currency);

                    }


                }
                db.setTransactionSuccessful();

            }
            catch (SQLiteException e)
            {
                e.printStackTrace();

            }
            finally
            {
                db.endTransaction();

            }

            return list;
        }
    }

AddWallet.class

public class AddWallet extends AppCompatActivity {
Spinner currency;
DatabaseDEO db = new DatabaseDEO();
        ArrayList<String> list= db.getToSpinner();
        ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,R.layout.currency_spinner, R.id.text,list);
        currency.setAdapter(adapter);
    }

}

currency_spinner.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5sp"
        android:paddingBottom="10sp"
        android:paddingTop="10sp"

        >

    </TextView>


</RelativeLayout>

Error

              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.martin.xxx/com.example.martin.xxx.AddWallet}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase Database.DatabaseHelper.getReadableDatabase()' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase Database.DatabaseHelper.getReadableDatabase()' on a null object reference
                  at Database.DatabaseDEO.getToSpinner(DatabaseDEO.java:32)
                  at com.example.martin.xxx.AddWallet.onCreate(AddWallet.java:41)
                  at android.app.Activity.performCreate(Activity.java:6664)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

回答1:

First of all as naming convention rename the DbHelper to dbHelper

Solution: you have to constructors

    public DatabaseDEO(Context context) {
        DbHelper = new DatabaseHelper(context);
    }

    public DatabaseDEO() {

    }

first one you initialize the databaseHelper and the second one you don't initialize it.

In AddWallet you call the empty Constructor so the DatabaseHelper is not initialized you have to call the second constructor and delete the empty one to not wrongly call it again.