使用insert方法时出现空指针异常

时间:2012-06-25 10:17:55

标签: android sqlite nullpointerexception

我在使用我的数据库时遇到问题我得到空指针异常,我无法解决任何帮助?在这里我的代码,我不知道为什么我得到这个错误,但我肯定会抛出 添加好友方法

database.java

    public class frienddatabase extends SQLiteOpenHelper {
        public static String databasename="frienddatabase";
        public static int databaseVersion =6;
        public String tablename="friends";
        public String id="_id";
        public String name="name";
        public String email="email";
        public String phone="phone";
        public String pic="pic";
        public String age="age";
        public String edu="edu";

构造函数:

        public frienddatabase(Context context) {
            super(context,databasename,null,databaseVersion);
            // TODO Auto-generated constructor stub
        }



        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            String create = "CREATE TABLE "+tablename+"("+id+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"+name+" Text unique Not null,"+
            email+" TEXT NOT NULL,"+phone+" TEXT NOT NULL,"+pic+" TEXT,"+age+" TEXT NOT NULL,"+edu+" TEXT NOT NULL"+")";
            db.execSQL(create);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS "+tablename);
            onCreate(db);
        }

这是我尝试使用的函数并获取NullPointer Exception

        public boolean addFriend(String Fname,String Femail,String Fphone,String Fage,String Fedu)
        {
            SQLiteDatabase db=this.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(name, Fname);
            cv.put(email, Femail);
            cv.put(phone, Fphone);
            cv.put(age, Fage);
            cv.put(edu, Fedu);
            db.insert(tablename, pic, cv);
            db.close();
            return true;
        }

当我尝试使用addfriend方法时出现错误:这里addfriend类我在其中调用addfriend方法

    public class addfriend extends Activity {
        EditText name,email,mobile,age,edu;
            Button save,cancel,addimage;
            frienddatabase helper;
            String namex;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.add);
                name=(EditText)findViewById(R.id.name);
                email=(EditText)findViewById(R.id.email);
                mobile=(EditText)findViewById(R.id.mobile);
                age=(EditText)findViewById(R.id.age);
                edu=(EditText)findViewById(R.id.edu);
                save=(Button)findViewById(R.id.save);
                cancel=(Button)findViewById(R.id.cancel);
                addimage=(Button)findViewById(R.id.addimage);
                helper=new frienddatabase(getApplicationContext());
                        save.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        namex=name.getText().toString();
                        Toast.makeText(getApplicationContext(),namex, Toast.LENGTH_LONG).show();
                        Boolean check = helper.addFriend(name.getText().toString(), email.getText().toString(), mobile.getText().toString(), age.getText().toString(), edu.getText().toString());
                        if(check==null)
                        {
                            Toast.makeText(getApplicationContext(), "null boolean",Toast.LENGTH_LONG).show();
                        }
                        if(check)
                        {
                            Toast.makeText(getApplicationContext(), "new friend added successfull!!", Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "data not saved ", Toast.LENGTH_LONG).show();
                        }
                    }
                });
}
}

这里也是log cat

06-25 12:53:25.041: W/dalvikvm(1290): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
06-25 12:53:25.061: E/AndroidRuntime(1290): FATAL EXCEPTION: main
06-25 12:53:25.061: E/AndroidRuntime(1290): java.lang.NullPointerException
06-25 12:53:25.061: E/AndroidRuntime(1290):     at com.apk.addfriend$1.onClick(addfriend.java:38)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at android.view.View.performClick(View.java:3460)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at android.view.View$PerformClick.run(View.java:13955)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at android.os.Handler.handleCallback(Handler.java:605)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at android.os.Looper.loop(Looper.java:137)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at android.app.ActivityThread.main(ActivityThread.java:4340)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at java.lang.reflect.Method.invokeNative(Native Method)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at java.lang.reflect.Method.invoke(Method.java:511)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at dalvik.system.NativeStart.main(Native Method)

任何帮助吗??

1 个答案:

答案 0 :(得分:0)

您没有在addFriend()方法中插入ID 但在主键字段中声明ID 在你的代码中检查这个......