0

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

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


        }
    }

日志猫

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)
4

3 回答 3

3

因为,你 DatabaseHelper dbHelper;NULL

insert()法。

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

解决方案:

DatabaseHelper dbHelper;在访问它之前进行初始化。

于 2013-03-02T06:25:35.953 回答
0

你应该初始化你的DatabaseHleper,目前它的Null

这条线抛出NullPointerException

 dbHelper.insert_user(name, mobile);
于 2013-03-02T06:26:56.153 回答
0

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

于 2013-03-02T06:31:50.127 回答