2
@EBean
public class CartDB {

@OrmLiteDao(helper = DatabaseHelper.class, model = Cart.class)
CartDao cartDao;

public Cart getCart() {
    return cartDao.getCart()}

public String count(){
    long count =0;
    try {
        count= cartDao.countOf();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return count+"";
  }

In my second Activity onCreate() I call Count method

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

Log.e("Count",cartDB.count()+"");
}

than count method return data , but In my first activity onCreate() I call count method

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_layout);
            Log.e("Count",cartDB.count()+"");
    }

nullpointer exeption and my app crashes

Note: retrieving data from Sqlite database

Payal Sorathiya
  • 234
  • 1
  • 12
  • Please show your logcat @Payal.Please follow the answer below. – Lips_coder Apr 22 '16 at 04:48
  • 04-22 15:00:44.453 567-567/com.gems.anychinese E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gems.anychinese/com.gems.anychinese.DashBoardActivity}: java.lang.NullPointerException – Payal Sorathiya Apr 22 '16 at 09:38
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Seth Apr 22 '16 at 13:45

1 Answers1

3

The null pointer exception is because you have not initialized your Java Class CardDB in your Activity;

Please make an instance of CartDB:

CartDB mycart;

then in your Activity's onCreate method initialize it properly:

onCreate(Bundle savedInstance){
 super.onCreate(savedInstanceState);
    setContentView(R.layout.content_layout);
mycart=new CartDB();

}
Lips_coder
  • 686
  • 1
  • 5
  • 17