保存按钮单击时的an​​droid空指针异常

时间:2012-03-15 06:39:55

标签: android nullpointerexception

我在点击按钮时遇到Null Pointer Exception。以下是错误日志和代码。请帮助。提前致谢。

LOGCAT:

03-15 11:56:06.611: E/AndroidRuntime(856): FATAL EXCEPTION: main
    03-15 11:56:06.611: E/AndroidRuntime(856): java.lang.NullPointerException
    03-15 11:56:06.611: E/AndroidRuntime(856):  at com.android.database.Database_Manipulation.insertTitle(Database_Manipulation.java:58)
    03-15 11:56:06.611: E/AndroidRuntime(856):  at com.android.database.Task3Activity$1.onClick(Task3Activity.java:49)
    03-15 11:56:06.611: E/AndroidRuntime(856):  at android.view.View.performClick(View.java:3511)
    03-15 11:56:06.611: E/AndroidRuntime(856):  at android.view.View$PerformClick.run(View.java:14105)
    03-15 11:56:06.611: E/AndroidRuntime(856):  at android.os.Handler.handleCallback(Handler.java:605)
    03-15 11:56:06.611: E/AndroidRuntime(856):  at android.os.Handler.dispatchMessage(Handler.java:92)
    03-15 11:56:06.611: E/AndroidRuntime(856):  at android.os.Looper.loop(Looper.java:137)
    03-15 11:56:06.611: E/AndroidRuntime(856):  at android.app.ActivityThread.main(ActivityThread.java:4424)
    03-15 11:56:06.611: E/AndroidRuntime(856):  at java.lang.reflect.Method.invokeNative(Native Method)
    03-15 11:56:06.611: E/AndroidRuntime(856):  at java.lang.reflect.Method.invoke(Method.java:511)
    03-15 11:56:06.611: E/AndroidRuntime(856):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    03-15 11:56:06.611: E/AndroidRuntime(856):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    03-15 11:56:06.611: E/AndroidRuntime(856):  at dalvik.system.NativeStart.main(Native Method)
    03-15 11:56:08.102: I/Process(856): Sending signal. PID: 856 SIG: 9

CODE:

public class Database_Manipulation extends Activity
{   
    public static final String KEY_ROWID="id";
    public static final String KEY_ISBN="isbn";
    public static final String KEY_TITLE="title";
    public static final String KEY_PUBLISHER="publisher";
    private static final String TAG="DBAdapter";

    private static final String DATABASE_NAME="books";
    private static final String DATABASE_TABLE="titles";
    private static final int  DATABASE_VERSION=1;


    private static final String DATABASE_CREATE="create table titles(id integer primary key autoincrement,"+"isbn text not null,title text not null,"+"publisher text not null);";
private final Context context;
    private DatabaseHelper DBhelper;
    private SQLiteDatabase db;

public Database_Manipulation(Context ctx)
{
this.context=ctx;
DBhelper=new DatabaseHelper(context);
}

public Database_Manipulation open() throws SQLException
{
    db=DBhelper.getWritableDatabase();

    return this;


}

public void close()
{
DBhelper.close();   
}

public long insertTitle(String isbn,String title,String publisher)
{
    ContentValues initialvalues=new ContentValues();
initialvalues.put(KEY_ISBN,isbn);
initialvalues.put(KEY_TITLE, title);
initialvalues.put(KEY_PUBLISHER, publisher);
return db.insert(DATABASE_TABLE, null, initialvalues);

}
    private static class DatabaseHelper extends SQLiteOpenHelper
    {

        public DatabaseHelper(Context context)
        {
                // TODO Auto-generated constructor stub
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

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

            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            Log.w(TAG,"UPGRADING"+oldVersion+"to"+newVersion+"which will destroy every old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
        onCreate(db);
        }



    }
}







//calling the insert title method from the class below at the click event...Everything runs fine but when i click on save button 
it shows the above given log stack....HERE iS THE CLASS





public class Task3Activity extends Activity {
    /** Called when the activity is first created. */
Context ctx;    
  private String name_get=null,address_get=null,contact_no_get=null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //getting the name
  final EditText name=(EditText)findViewById(R.id.editText1);
  //getting the address
  final EditText address=(EditText)findViewById(R.id.editText2);
 //getting the contact no
  final EditText contactno=(EditText)findViewById(R.id.editText3);
  name_get=name.getText().toString();
  address_get=address.getText().toString();
  contact_no_get=contactno.getText().toString();
  Toast.makeText(getApplicationContext(),name_get+address_get+contact_no_get,Toast.LENGTH_LONG).show();
  //buttons 

  final Button save=(Button)findViewById(R.id.button1);
  final Button update=(Button)findViewById(R.id.button2);
  final Button delete=(Button)findViewById(R.id.button3);
  //SAVE BUTTON

  save.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
long ret=0;
        Database_Manipulation dm=new Database_Manipulation(ctx);        
if(dm!=null)
{ret=dm.insertTitle(name_get,address_get,contact_no_get);
Toast.makeText(getApplicationContext(),"recieved somthing",Toast.LENGTH_SHORT).show();
}
else
    Toast.makeText(getApplicationContext(), "empty context",Toast.LENGTH_SHORT).show();


    }
});
  //UPDATE BUTTON

  update.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), address_get,Toast.LENGTH_SHORT).show();


        }
    });
  //DELETE BUTTON

  delete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), contact_no_get,Toast.LENGTH_SHORT).show();


        }
    });

    }}

2 个答案:

答案 0 :(得分:1)

我认为在这种方法中(如果我没有错),

public long insertTitle(String isbn,String title,String publisher)
{
    ContentValues initialvalues=new ContentValues();
    initialvalues.put(KEY_ISBN,isbn);
    initialvalues.put(KEY_TITLE, title);
    initialvalues.put(KEY_PUBLISHER, publisher);
    return db.insert(DATABASE_TABLE, null, initialvalues);
}

db对象为空 ..

只需使用可写数据库初始化它。

<强>更新

把这一行,

if (db == null || !db.isOpen())
  {
    db = DBhelper.getWritableDatabase();
  }
insertTitle方法中

,在数据库中插入值之前,

答案 1 :(得分:1)

我认为您应该检查正在使用的未初始化对象 检查必要的permisions