Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid null pointer exception on savebutton click
    primarykey
    data
    text
    <p>I am encountering a <code>Null Pointer Exception</code> on the click on a button. Following is the error log and code. Kindly help. Thanks in advance. </p> <p><strong>LOGCAT:</strong> </p> <pre><code>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></pre> <p><strong>CODE:</strong></p> <pre><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(); } }); }} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload