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)
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Clam
  • 935
  • 1
  • 12
  • 24

2 Answers2

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);
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • also change from DbPrice to DbHelper – Piyush Jan 10 '14 at 11:32
  • i guess there is something still wrong. need to take a closer look – Raghunandan Jan 10 '14 at 11:37
  • I think yes..there is. – Piyush Jan 10 '14 at 11:39
  • @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() – Clam Jan 11 '14 at 01:51
  • the "in Activity" part, I am calling that in AddItem.class? "MainActivity" referring to... my GrocerApp class? – Clam Jan 11 '14 at 02:14
  • @user2827338 all wrong can't create a instance of activity class. Read the docs and start over again – Raghunandan Jan 11 '14 at 03:45
  • can you recommend a better guide? i was following this http://www.youtube.com/watch?v=c5n90wiv75M&list=SPE08A97D36D5A255F&index=20 but don't really understand initializing +context... – Clam Jan 11 '14 at 10:01
  • @user2827338 on youtube there is a guide by thenewboston video number 113 to 118 for database. Chekc that – Raghunandan Jan 11 '14 at 10:02
  • @user2827338 starting from http://www.youtube.com/watch?v=gEg9OdufXmM. The code shwon by the guy works i have tried it myself long time back. video 112 to 118 i guess – Raghunandan Jan 11 '14 at 10:05
1

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

vikas singh
  • 241
  • 1
  • 10