用户定义方法中的NullPointerException" insert"在datahelper类

时间:2015-07-11 09:08:15

标签: android database nullpointerexception sqliteopenhelper

这是logcat

07-11 04:34:41.360: E/AndroidRuntime(10132): FATAL EXCEPTION: main
07-11 04:34:41.360: E/AndroidRuntime(10132): Process:app.example.movierating, PID: 10132
07-11 04:34:41.360: E/AndroidRuntime(10132): java.lang.NullPointerException
07-11 04:34:41.360: E/AndroidRuntime(10132): at model.DataHelper.insert(DataHelper.java:75)
07-11 04:34:41.360: E/AndroidRuntime(10132):    at app.example.movierating.MainActivity1$1.onClick(MainActivity1.java:85)
07-11 04:34:41.360: E/AndroidRuntime(10132):    at android.view.View.performClick(View.java:4424)
07-11 04:34:41.360: E/AndroidRuntime(10132):    at android.view.View$PerformClick.run(View.java:18383)
07-11 04:34:41.360: E/AndroidRuntime(10132):    at android.os.Handler.handleCallback(Handler.java:733)
07-11 04:34:41.360: E/AndroidRuntime(10132):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-11 04:34:41.360: E/AndroidRuntime(10132):    at android.os.Looper.loop(Looper.java:137)
07-11 04:34:41.360: E/AndroidRuntime(10132):    at android.app.ActivityThread.main(ActivityThread.java:4998)
07-11 04:34:41.360: E/AndroidRuntime(10132):    at java.lang.reflect.Method.invokeNative(Native Method)
07-11 04:34:41.360: E/AndroidRuntime(10132):    at java.lang.reflect.Method.invoke(Method.java:515)
07-11 04:34:41.360: E/AndroidRuntime(10132):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-11 04:34:41.360: E/AndroidRuntime(10132):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-11 04:34:41.360: E/AndroidRuntime(10132):    at dalvik.system.NativeStart.main(Native Method)

这是datahelper类代码 填写我的主要活动布局...在保存我的appp关闭不幸由于上述错误。 帮助我,因为我无法弄清楚null异常在哪里以及如何去做 我是学习者。

package model;

import java.util.Collection;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataHelper extends SQLiteOpenHelper {

static String dbname="info.db";
static String tablename="records";
SQLiteDatabase db; 
static int dbversion=1;

public DataHelper(Context context)
        {
    super(context,dbname,  null, dbversion);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub
    arg0.execSQL("createtable" + tablename + "(id integer.PRIMARY KEY AUTOINCREMENT, name1 text,yr1 text,dur1 text,starcast1 text,review1 text,dir1 text, genre1 text)");
    Log.e("SQLiteData", "tablecreated");    
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub
    arg0.execSQL("drop table if exists" + tablename);
    onCreate(arg0);
}



public boolean insert(String name1, String genre1, String yr1, String dur1,Float ratingbar1, String review1, String starcast1, String dir1)
{
    // TODO Auto-generated method stub
     boolean result;
        try
        {
            db=this.getWritableDatabase();
            ContentValues c=new ContentValues();
            c.put("name", name1);
            c.put("genre", genre1);
            c.put("yr", yr1);
            c.put("dur", dur1);
            c.put("review", review1);
            c.put("starcast", starcast1);
            c.put("dir", dir1);
            long res=db.insert(tablename, null , c);

            if(res>0)
            {
                result=true;
                //return result;
            }
            else
            {
                result=false;
                //return result;
            }
        }
        catch(Exception ex)
        {
            Log.e("error",ex.toString());
        }
        finally
        {
            db.close();
        }
        return result=false;

}



}

返回 我被困了

1 个答案:

答案 0 :(得分:0)

在onCreate中更改您的代码;

arg0.execSQL("create table " + tablename + "(id integer PRIMARY KEY AUTOINCREMENT, name text,yr text,dur text,starcast text,review text,dir text, genre text)");