【问题标题】:null pointer exception when using insert method使用插入方法时出现空指针异常
【发布时间】:2012-06-25 10:17:54
【问题描述】:

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

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

这里也记录猫

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)

有什么帮助吗??

【问题讨论】:

标签: android sqlite nullpointerexception


【解决方案1】:

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

【讨论】:

  • 不是我的 id 自动增量,所以我不需要将 id 传递给 addfriend () thx
  • 只需打印并检查您传递的所有数据是否包含任何字符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 2017-09-18
  • 2016-03-31
  • 2014-05-23
  • 2014-11-15
  • 2013-04-10
相关资源
最近更新 更多