ROOM数据库的空指针异常

时间:2019-10-08 12:45:13

标签: android android-room

我正在使用android studio开发一个android应用程序,当我在数据库中存储运行时错误并且无法解决时,我的应用程序具有ROOM数据库

这是我的数据库代码

@Database(entities = {Cart.class},version = 1,exportSchema = false)
public  abstract class CartDatabase extends RoomDatabase {

private static Context context;

public abstract CartDAO cartDAO();

private static CartDatabase instance;


public static CartDatabase getInstance() {
    if (instance == null) {
        instance = Room.databaseBuilder(context.getApplicationContext(), CartDatabase.class, "Cart")//if we want in memory builder  ithink we can add it here
                .allowMainThreadQueries().build();
    }
    return instance;


}


}

并将其插入数据库代码

 addBTn.setOnClickListener( new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Cart cart=new Cart();
                                cart.setName( (String) productNameTxt.getText() );
                                cart.setPrice(Integer.parseInt( (String ) productPriceTxt.getText()  ));
                                CartDatabase.getInstance( ).cartDAO().insertToCart( cart );

                                Toast.makeText(getActivity(),"Item added successfully",Toast.LENGTH_SHORT).show();



                            }
                        } );

这是错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.queueskip, PID: 3544
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at com.example.queueskip.Database.Local.CartDatabase.getInstance(CartDatabase.java:25)
        at com.example.queueskip.ui.home.HomeFragment$2$1$1.onClick(HomeFragment.java:169)
        at android.view.View.performClick(View.java:5721)
        at android.widget.TextView.performClick(TextView.java:10936)
        at android.view.View$PerformClick.run(View.java:22620)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7406)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

3 个答案:

答案 0 :(得分:3)

您的Context为空,最好在Context方法中创建一个getInstance()参数,而不是将其存储在CartDatabase类中。

  

尝试以下代码

public static CartDatabase getInstance(Context context) {
    if (instance == null) {
        instance = Room.databaseBuilder(context, CartDatabase.class, "Cart")//if we want in memory builder  ithink we can add it here
                .allowMainThreadQueries().build();
    }
    return instance;
}
  

然后在您的onClick()

CartDatabase.getInstance(getApplicationContext()).cartDAO().insertToCart(cart);

答案 1 :(得分:0)

添加一个构造函数,并通过活动向其传递上下文:

在您的活动中:

CartDatabase cart = new CartDatabase(this);

以及在CartDatabase类中:

   public CartDatabase(Context             context){
    this.context=context;
     }

或使用活动中的getInstance将上下文传递给类:

CartDatabase.getInstanse(this).cartDAO().insertToCart( cart);

以及在CartDatabase类中:

public static CartDatabase getInstance(Context context) ...

答案 2 :(得分:0)

之所以得到这个,是因为您没有将任何上下文传递给CartDatabase。您可以像这样简单地将上下文参数添加到您的getInstance方法中...

public static CartDatabase getInstance(Context context) {
if (instance == null) {
    instance = Room.databaseBuilder(context, CartDatabase.class, "Cart").allowMainThreadQueries().build();
}
return instance;

}

然后需要通过类似的活动进行呼叫...

CartDatabase.getInstance(getApplicationContext()).cartDAO().insertToCart(cart);