问题
I am developing an android app using android studio my app has ROOM database when i store in the database a run time error appears and I can not solve it
this is my database code
@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;
}
}
and this insert into database code
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();
}
} );
and this is the error
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)
回答1:
Your Context
is null, it would be better if you create a Context
parameter in your getInstance()
method instead of storing it in your CartDatabase
class.
Try the following code
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;
}
Then on your
onClick()
CartDatabase.getInstance(getApplicationContext()).cartDAO().insertToCart(cart);
回答2:
Add a constructor and pass context to it from the activity:
In your activity:
CartDatabase cart = new CartDatabase(this);
and in the CartDatabase class :
public CartDatabase(Context context){
this.context=context;
}
or pass context to the class using getInstance from your activity:
CartDatabase.getInstanse(this).cartDAO().insertToCart( cart);
and in the CartDatabase class :
public static CartDatabase getInstance(Context context) ...
回答3:
You are getting this because you are not passing any context to the CartDatabase. You can simply add a context parameter to your getInstance method like that...
public static CartDatabase getInstance(Context context) {
if (instance == null) {
instance = Room.databaseBuilder(context, CartDatabase.class, "Cart").allowMainThreadQueries().build();
}
return instance;
}
And then need to call from activity like that...
CartDatabase.getInstance(getApplicationContext()).cartDAO().insertToCart(cart);
来源:https://stackoverflow.com/questions/58286636/null-pointer-exception-with-room-database