raman rayat raman rayat - 6 months ago 12
Android Question

Why is my record update giving a null pointer exception?

enter image description here

I am creating notepad application and want to update my notes but I am unable to record in sqlite database. Please check my update query
I am sharing error details.

public class MainUpdateActivity extends Activity {

SQLiteDatabase mydb1;
EditText titleedit,notetextedit;
Button update;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

titleedit=(EditText)findViewById(R.id.title);
notetextedit=(EditText)findViewById(R.id.notetext);
update=(Button) findViewById(R.id.update);

Intent myintent = getIntent();

String product7 = myintent.getStringExtra("product1");
String product8 = myintent.getStringExtra("product");

titleedit.setText(product7);
notetextedit.setText(product8);

update.setOnClickListener(new View.OnClickListener()
{

public void onClick(View v)
{
mydb1.execSQL("UPDATE notes SET (title,notetext) values(?,?);",new String[]{titleedit.getText().toString(),notetextedit.getText().toString()});

Toast.makeText(getApplicationContext(), "note Updated", 3000).show();



Intent myIntent1 = new Intent(MainUpdateActivity.this,menu.class);

startActivity(myIntent1);

finish();

}

});

Answer

Looks like in

mydb1.execSQL("UPDATE notes SET (title,notetext) values(?,?);",new String[]{titleedit.getText().toString(),notetextedit.getText().toString()});

mydb1 is null,you didn't new and init mydb1.