有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

SQLite插入上的java空指针异常

好的,我现在正试图将数据插入到sqlite数据库中。它崩溃了,因为它说我的数据库是空的。我想我没有在我的Additem类中初始化(或者可能是另一个术语…基本上是为了识别我在另一个类中所做的事情),但不确定如何解决,以及为什么

指针

另外。阶级

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。阶级

    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);

        }

    }




}

在我的杂货店里。课堂(活动课,我做了这个)

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");
    } 



}

这是我的日志,仅供参考

 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 安卓.view.View$1.onClick(View.java:3814)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at 安卓.view.View.performClick(View.java:4424)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at 安卓.view.View$PerformClick.run(View.java:18383)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at 安卓.os.Handler.handleCallback(Handler.java:733)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at 安卓.os.Handler.dispatchMessage(Handler.java:95)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at 安卓.os.Looper.loop(Looper.java:137)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-10 06:18:38.763: E/AndroidRuntime(1119):     at com.安卓.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 安卓.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)

共 (1) 个答案

  1. # 1 楼答案

    你忘了初始化DbHelper dbHelper;

    dbHelper = new DbHelper(AddIten.this);
    

    onCreate

    但你有

    public class DbPrice extends Activity {
    

    我想你需要看看医生。还有更多的错误

    你正在创建一个activity类的实例,这是非常错误的

    dbPrice = new DbPrice(this);
    

    编辑:

    价格如下

     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);
    }
    

    在活动中

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