1

Ok I'm stuck at this point where I'm trying to insert data into sqlite database. It's crashing cause it is saying my db is null I think. I think I am not initializing (or maybe another term... basically to recognize what I did in the other class) in my Additem class but not sure how to solve and why exactly.

Pointers?

Additem.class

public class AddItem extends Activity implements OnClickListener {

private final String TAG = "Main Activity";
View v;
SQLiteDatabase db; 
DbHelper dbHelper;



@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_item); 
    Log.i(TAG, "OnCreate"); 

}

@Override 
public void onClick(View v) {


            Log.i(TAG, "Extracting Data"); 

            DatePicker datePicker1 = (DatePicker) findViewById(R.id.datePicker1);
            AutoCompleteTextView autoCompleteSubCat = (AutoCompleteTextView) findViewById(R.id.autoCompleteSubCat);
            EditText editItem = (EditText) findViewById(R.id.editItem);
            EditText editPrice = (EditText) findViewById(R.id.editPrice);
            EditText editQuantity = (EditText) findViewById(R.id.editQuantity); 
            EditText editWeight = (EditText) findViewById(R.id.editWeight);
            EditText editVolume = (EditText) findViewById(R.id.editVolume);
            //CheckBox checkSale = (CheckBox) findViewById(R.id.checkSale);
            AutoCompleteTextView autoCompleteStore = (AutoCompleteTextView) findViewById(R.id.autoCompleteStore);
            EditText editExtra = (EditText) findViewById(R.id.editExtra);

            String subcat,item,store,extra;
            Integer day,month,year;
            Double price,quantity,weight,volume,sale;

            Log.i(TAG, "Converting to String and Int"); 

            day = datePicker1.getDayOfMonth();
            month = datePicker1.getMonth();
            year = datePicker1.getYear();
            subcat = autoCompleteSubCat.getText().toString();
            item = editItem.getText().toString();
            extra = editExtra.getText().toString();


                    price = Double.parseDouble(editPrice.getText().toString());
            quantity = Double.parseDouble(editQuantity.getText().toString());
            weight = Double.parseDouble(editWeight.getText().toString());
            volume = Double.parseDouble(editVolume.getText().toString());
            // sale = checkSale; 
            store = autoCompleteStore.getText().toString();

            db = dbHelper.getWritableDatabase(); 
            ContentValues cv = new ContentValues();
            cv.put(DbPrice.SUBCAT, subcat);
            cv.put(DbPrice.ITEM, item);
            cv.put(DbPrice.PRICE, price);
            cv.put(DbPrice.QUANTITY, quantity);
            cv.put(DbPrice.WEIGHT, weight);
            cv.put(DbPrice.VOLUME, volume);
            // cv.put(DbPrice.SALE, sale);
            cv.put(DbPrice.STORE, store);
            cv.put(DbPrice.EXTRA, extra);

            db.insert(DbPrice.TABLE_NAME, null, cv); 

            dbHelper.close();

            Log.i(TAG, "Starting New Activity"); 
            Intent allItemsActivity = new Intent (AddItem.this, AllItems.class);
            startActivity(allItemsActivity);

            }

DbPrice.class

    public class DbPrice extends Activity {
    public static final String DATABASE_NAME = "data";
    public static final String TABLE_NAME = "price_table";
    public static final String C_ID = "_id";
    public static final String DAY = "day";  
    public static final String MONTH = "month";  
    public static final String YEAR = "year";  
    public static final String SUBCAT = "subcategory";
    public static final String ITEM = "item";
    public static final String PRICE = "price";  
    public static final String QUANTITY = "quantity"; 
    public static final String WEIGHT = "weight"; 
    public static final String VOLUME = "volume"; 
    public static final String SALE = "sale";
    public static final String STORE = "store";
    public static final String EXTRA = "extra";
    public static final int VERSION = 1; 

    Context context; 
    DbHelper dbHelper;
    SQLiteDatabase db; 

public DbPrice(Context context) {
         this.context = context;
         dbHelper = new DbHelper(context); 
     }




public Cursor query() {
    db = dbHelper.getReadableDatabase(); 
    Cursor cursor = db.query(DbPrice.TABLE_NAME,null, null, null, null, null, SUBCAT + " DESC");
    return cursor;

}



class DbHelper extends SQLiteOpenHelper {




    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSION);


    }

    private final String createDb = "create table if not exists " + TABLE_NAME+ " ( "
        + C_ID + " integer primary key autoincrement, "
        // + DAY + " integer, "
        // + MONTH + " integer, "
        // + YEAR + " integer, "
        + SUBCAT + " text, "
        + ITEM + " text, "
        + PRICE + " integer, "
        + QUANTITY + " integer, "
        + WEIGHT + " integer, "
        + VOLUME + " integer, "
        // + SALE + " text, "
        + STORE + " text, "
        + EXTRA + " text) ";

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("DbPrice", "Oncreate with SQL"+ createDb);
        db.execSQL(createDb); 
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
        db.execSQL("drop table if exists " + TABLE_NAME);
        onCreate(db);

        }

    }




}

In my GroceryApp.class (Activity class, I did this)

public class GroceryApp extends Application {
    static final String TAG = "GroceryApp";
    DbPrice dbPrice;


    @Override
    public void onCreate() {
        super.onCreate();

        dbPrice = new DbPrice(this);
        Log.d(TAG, "OnCreated GroceryApp");
    } 



}

Here's my logcat, just fyi

 01-10 06:18:38.763: E/AndroidRuntime(1119): java.lang.IllegalStateException: Could not execute method of the activity
01-10 06:18:38.763: E/AndroidRuntime(1119):     at android.view.View$1.onClick(View.java:3814)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at android.view.View.performClick(View.java:4424)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at android.view.View$PerformClick.run(View.java:18383)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at android.os.Handler.handleCallback(Handler.java:733)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at android.os.Looper.loop(Looper.java:137)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at android.app.ActivityThread.main(ActivityThread.java:4998)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at java.lang.reflect.Method.invoke(Method.java:515)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at dalvik.system.NativeStart.main(Native Method)
01-10 06:18:38.763: E/AndroidRuntime(1119): Caused by: java.lang.reflect.InvocationTargetException
01-10 06:18:38.763: E/AndroidRuntime(1119):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at java.lang.reflect.Method.invoke(Method.java:515)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at android.view.View$1.onClick(View.java:3809)
01-10 06:18:38.763: E/AndroidRuntime(1119):     ... 11 more
01-10 06:18:38.763: E/AndroidRuntime(1119): Caused by: java.lang.NullPointerException
01-10 06:18:38.763: E/AndroidRuntime(1119):     at com.unsuccessfulstudent.grocerypricehistory.AddItem.onClick(AddItem.java:73)
7
  • what is that AddItem.java:73 line Commented Jan 10, 2014 at 11:26
  • where is dbhelper creation or initialization in your code? Commented Jan 10, 2014 at 11:26
  • this line getting error db = dbHelper.getWritableDatabase(); right? Commented Jan 10, 2014 at 11:27
  • DBHelper you made but what about the initialization?? Commented Jan 10, 2014 at 11:31
  • i suggest you look at the docs and start over again coz there are many mistakes Commented Jan 10, 2014 at 11:50

2 Answers 2

1

first you have to create the object of
DbHelper in oncreate like this DbHelper dbHelper = new DbHelper(AddIten.this);

Sign up to request clarification or add additional context in comments.

Comments

1

You forgot to initialize DbHelper dbHelper;

dbHelper = new DbHelper(AddIten.this);

in onCreate

But you have

public class DbPrice extends Activity {

I think you need to look at the docs. There are more mistakes.

And you are creating an instance of activity class which is very wrong

dbPrice = new DbPrice(this);

Edit:

Have the below in DbPrice

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

public DbPrice open()throws SQLException
{
    dbHelper= new DbHelper(context);
    db= dbHelper.getWritableDatabase();
    return this;
}


public void addContent(String subcat,String item,Double price2,Double quantity2,Double weight2,Double volume2,String store,String extra)
{
    ContentValues cv = new ContentValues();
    cv.put("SUBCAT", subcat);
    cv.put(DbPrice.ITEM, item);
    cv.put(DbPrice.PRICE, price2);
    cv.put(DbPrice.QUANTITY, quantity2);
    cv.put(DbPrice.WEIGHT, weight2);
    cv.put(DbPrice.VOLUME, volume2);
    // cv.put(DbPrice.SALE, sale);
    cv.put(DbPrice.STORE, store);
    cv.put(DbPrice.EXTRA, extra);
    db.insert(DbPrice.TABLE_NAME, null, cv);
}

And in Activity

dbHelper = new DbPrice(MainActivity.this);
DbPrice db= dbHelper.open();
db.addContent(subcat,item,price,quantity,weight,volume,store,extra);

9 Comments

also change from DbPrice to DbHelper
i guess there is something still wrong. need to take a closer look
I think yes..there is.
@PiyushGupta - which DbPrice should change to DbHelper? There's a lot of them up there... You have to initialize for every activity that uses the db right? like you said dbHelper = new DbHelper(AddItem.this); in OnCreate()
the "in Activity" part, I am calling that in AddItem.class? "MainActivity" referring to... my GrocerApp class?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.