0

我在使用我的数据库时遇到问题,我得到空指针异常,我无法解决它任何帮助??这是我的代码,我不知道为什么会出现此错误,但我确信这是从添加朋友方法引发的

数据库.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();
                        }
                    }
                });
}
}

这里也记录猫

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)

请问有什么帮助吗??

4

1 回答 1

0

您没有在 addFriend() 方法中插入 ID,而是在主键字段中声明 ID,请在您的代码中检查此内容....

于 2012-06-25T10:32:45.543 回答