Room Dao空指针异常

时间:2018-10-23 07:50:15

标签: java android android-studio dao android-room

我在房间数据库类中创建的抽象Dao方法上收到了空指针异常。虽然我提到的几个示例项目都做同样的事情,但它确实有效。我不了解在其他项目中何时和何处初始化此实例。这是我下面的主要活动代码:

主要活动:

package com.example.datta.roomtest;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.datta.roomtest.Dao.PlayerDao;
import com.example.datta.roomtest.Database.AppDatabase;
import com.example.datta.roomtest.Entity.Player;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    AppDatabase appDatabase;
    TextView tvName,tvRole;
    EditText etName,etRole;
    Button btnGet,btnPut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        appDatabase= AppDatabase.getInstance(this);
        init();

        setListeners();

    }

    private void init() {

    tvName=(TextView)findViewById(R.id.tvPlayername);
    tvRole=(TextView)findViewById(R.id.tvPlayerRole);
    etName=(EditText)findViewById(R.id.etName);
    etRole=(EditText)findViewById(R.id.etRole);
    btnGet=(Button)findViewById(R.id.btnGetData);
    btnPut=(Button)findViewById(R.id.btnPutData);
    }
    private void setListeners() {

        btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                new DbAsync().execute("get");
            }
        });

        btnPut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                new DbAsync().execute("put");
            }
        });

    }


     class DbAsync extends AsyncTask<String,Void,Void>{


        @Override
        protected Void doInBackground(String... strings) {


            Log.i("MainTag",strings[0]);


            if(strings[0].equals("get")){

                PlayerDao playerDao = appDatabase.getPlayerDao();

              List<Player>playerList=playerDao.getAllPlayers();
                for (Player player:playerList){

                    tvName.setText(player.getName());
                    tvRole.setText(player.getRole());

                }


            }
            if(strings[0].equals("put")){

                PlayerDao playerDao=appDatabase.getPlayerDao();
                playerDao.insertAll(new Player("Ronaldo","Striker"));
                playerDao.insertAll(new Player("Messi","Winger"));

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }
    }


}

这是我的Appdatabase:

package com.example.datta.roomtest.Database;

import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.content.Context;

import com.example.datta.roomtest.Dao.PlayerDao;
import com.example.datta.roomtest.Entity.Player;

@Database(entities = {Player.class},version = 1)
public abstract class AppDatabase extends RoomDatabase {

private static AppDatabase appDatabase;


public static AppDatabase getInstance(Context context){

    if(appDatabase!=null){

        appDatabase = Room.databaseBuilder(context.getApplicationContext(),
                AppDatabase.class, "database-name").build();
        }

    return appDatabase;

}

  public abstract PlayerDao getPlayerDao();


}

调用此getPlayerDao()方法时,出现此空指针错误:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.datta.roomtest.Dao.PlayerDao com.example.datta.roomtest.Database.AppDatabase.getPlayerDao()' on a null object reference
        at com.example.datta.roomtest.MainActivity$DbAsync.doInBackground(MainActivity.java:97)
        at com.example.datta.roomtest.MainActivity$DbAsync.doInBackground(MainActivity.java:71)
        at android.os.AsyncTask$2.call(AsyncTask.java:316)

1 个答案:

答案 0 :(得分:2)

getInstance()总是由于错误的测试而返回null:

public static AppDatabase getInstance(Context context){
    if(appDatabase!=null){ // <= ERROR HERE, should be == null
        appDatabase = Room.databaseBuilder(context.getApplicationContext(),
                AppDatabase.class, "database-name").build();
    }
    return appDatabase;

}