Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SQLite插入上的Null指针异常_Java_Android_Database_Sqlite_Sqliteopenhelper - Fatal编程技术网

Java SQLite插入上的Null指针异常

Java SQLite插入上的Null指针异常,java,android,database,sqlite,sqliteopenhelper,Java,Android,Database,Sqlite,Sqliteopenhelper,好的,我现在正试图将数据插入到sqlite数据库中。它崩溃了,因为它说我的数据库是空的。我想我没有在我的Additem类中初始化(或者可能是另一个术语…基本上是为了识别我在另一个类中所做的),但不确定如何解决,以及为什么 指针 附加类 public class AddItem extends Activity implements OnClickListener { private final String TAG = "Main Activity"; View v; SQLiteDataba

好的,我现在正试图将数据插入到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.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);

        }

    }




}
在我的GroceryApp.class(活动课,我做了这个)

这是我的日志,仅供参考

 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)

您忘记初始化
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);

首先,您必须创建
oncreate中的DbHelper
类似于这个DbHelper DbHelper=newdbhelper(AddIten.this)

那是什么AddItem.java:73行代码中的dbhelper创建或初始化在哪里?这一行出现错误db=dbhelper.getWritableDatabase();对吗?你制作了DBHelper,但是初始化呢?我建议你看看文档,重新开始,因为有很多错误,所以从DbPrice改为DBHelper我想还是有问题。需要仔细看看我想是的。@piyusgupta-哪个DbPrice应该改成DbHelper?上面有很多。。。您必须为每个使用db的活动初始化,对吗?正如您所说的dbHelper=newdbhelper(AddItem.this);在OnCreate()的“in Activity”部分中,我将其称为AddItem.class?“主要活动”指。。。我的GrocerApp课?
dbHelper = new DbPrice(MainActivity.this);
DbPrice db= dbHelper.open();
db.addContent(subcat,item,price,quantity,weight,volume,store,extra);