在将数据插入数据库时​​,sqlite数据库中出现NullPointerException

时间:2013-03-02 06:21:25

标签: android sqlite

我在sqlite数据库中获取NullPointerException,同时将数据插入数据库 请帮我解决这个问题。它在插入方法中显示错误。

DatabaseHelper.java

    package com.example.mydbdemo;

    import com.example.mydbdemo.R.string;

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

    public class DatabaseHelper extends SQLiteOpenHelper {

        private static final String DB_NAME="reena.db";
        private static final String TBL_NAME="MY_TBL";

        private static final String User_Name="name";
        private static final String Mobile_No="mobile";

        public DatabaseHelper(Context context, String name, CursorFactory factory,
                int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub

            //CREATE TABLE 
        db.execSQL("CREATE TABLE"+TBL_NAME+"("+User_Name+" TEXT, "+Mobile_No+" TEXT)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }


        void insert_user(String name1, String num)
        {
            SQLiteDatabase mydb=this.getWritableDatabase();
            ContentValues cv=new ContentValues();

            cv.put(User_Name, name1);
            cv.put(Mobile_No, num);
            mydb.insert(TBL_NAME, null, cv);
        }
    }

MyDBDemo.java

    package com.example.mydbdemo;

    import android.os.Bundle;
    import android.app.Activity;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class MyDBDemo extends Activity {

        EditText txt_NAME;
        EditText txt_PH;
        Button btn_Ok;
        DatabaseHelper dbHelper;
        SQLiteDatabase db;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my_dbdemo);

            txt_NAME=(EditText)findViewById(R.id.txt_Name);
            txt_PH=(EditText)findViewById(R.id.txt_Moblie);
            btn_Ok=(Button)findViewById(R.id.button1);
            btn_Ok.setOnClickListener(new View.OnClickListener() {

                @Override
         public void onClick(View v) {
                    // TODO Auto-generated method stub
                    insert();
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_my_dbdemo, menu);
            return true;
        }


        public void insert()
        {
            String name=txt_NAME.getText().toString();
            String mobile=txt_PH.getText().toString();
            dbHelper.insert_user(name, mobile);
            Toast.makeText(getApplicationContext(), "Data Saved Successfully!!",   Toast.LENGTH_LONG).show();


        }
    }

logcat的

03-02 11:38:04.118: E/AndroidRuntime(371): FATAL EXCEPTION: main
03-02 11:38:04.118: E/AndroidRuntime(371): java.lang.NullPointerException
03-02 11:38:04.118: E/AndroidRuntime(371):  at com.example.mydbdemo.MyDBDemo.insert(MyDBDemo.java:51)
03-02 11:38:04.118: E/AndroidRuntime(371):  at com.example.mydbdemo.MyDBDemo$1.onClick(MyDBDemo.java:33)
03-02 11:38:04.118: E/AndroidRuntime(371):  at android.view.View.performClick(View.java:2408)
03-02 11:38:04.118: E/AndroidRuntime(371):  at android.view.View$PerformClick.run(View.java:8816)
03-02 11:38:04.118: E/AndroidRuntime(371):  at android.os.Handler.handleCallback(Handler.java:587)
03-02 11:38:04.118: E/AndroidRuntime(371):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-02 11:38:04.118: E/AndroidRuntime(371):  at android.os.Looper.loop(Looper.java:123)
03-02 11:38:04.118: E/AndroidRuntime(371):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-02 11:38:04.118: E/AndroidRuntime(371):  at java.lang.reflect.Method.invokeNative(Native Method)
03-02 11:38:04.118: E/AndroidRuntime(371):  at java.lang.reflect.Method.invoke(Method.java:521)
03-02 11:38:04.118: E/AndroidRuntime(371):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-02 11:38:04.118: E/AndroidRuntime(371):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-02 11:38:04.118: E/AndroidRuntime(371):  at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:3)

因为,您的DatabaseHelper dbHelper; NULL

内部insert()方法。

所以代码行dbHelper.insert_user(name, mobile);给你 NullPointerException

<强>解决方案:

在访问之前初始化DatabaseHelper dbHelper;

答案 1 :(得分:0)

您应该初始化您的DatabaseHleper,目前 Null

此行抛出NullPointerException

 dbHelper.insert_user(name, mobile);

答案 2 :(得分:0)

你需要初始化你的Dbhelpers ..写入insert()dB = dbHelper.getWritableDatabase();并初始化dbHelper类。