-4

I get this error in yellow in logcat, app does not freeze. Here is logcat:

04-25 14:07:39.889: W/System.err(1647): java.lang.NullPointerException
04-25 14:07:39.899: W/System.err(1647):     at com.example.classorganizer.EditListItemDialog.saveItToDB(EditListItemDialog.java:77)
04-25 14:07:39.899: W/System.err(1647):     at com.example.classorganizer.EditListItemDialog.onClick(EditListItemDialog.java:68)
04-25 14:07:39.899: W/System.err(1647):     at android.view.View.performClick(View.java:2485)
04-25 14:07:39.899: W/System.err(1647):     at android.view.View$PerformClick.run(View.java:9080)
04-25 14:07:39.899: W/System.err(1647):     at android.os.Handler.handleCallback(Handler.java:587)
04-25 14:07:39.899: W/System.err(1647):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 14:07:39.899: W/System.err(1647):     at android.os.Looper.loop(Looper.java:130)
04-25 14:07:39.899: W/System.err(1647):     at android.app.ActivityThread.main(ActivityThread.java:3687)
04-25 14:07:39.899: W/System.err(1647):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 14:07:39.899: W/System.err(1647):     at java.lang.reflect.Method.invoke(Method.java:507)
04-25 14:07:39.899: W/System.err(1647):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-25 14:07:39.899: W/System.err(1647):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-25 14:07:39.899: W/System.err(1647):     at dalvik.system.NativeStart.main(Native Method)

And here is the file that is meant to update the row:

class EditListItemDialog extends Dialog implements View.OnClickListener {
MyDB dba;
private View editText;
private DiaryAdapter adapter;




public EditListItemDialog(Context context, List<String> fragment_monday) {     //first constructor
    super(context);
    dba = new MyDB(context);
}

@Override
 protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
View btnOk = findViewById(R.id.button_ok);
editText = findViewById(R.id.edit_text);
btnOk.setOnClickListener(this);


}

private List<String> fragment_monday;

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Position is the number of the item clicked
//You can use your adapter to modify the item
adapter.getItem(position); //Will return the clicked item
}

public EditListItemDialog(Context context, DiaryAdapter adapter, int position) {
super(context);
this.fragment_monday = new ArrayList<String>();
this.adapter = adapter;
}

In general I cannot get the code to update my row in sql. Any help would be appreciated.

@Override
public void onClick(View v) {


fragment_monday.add(((TextView) v).getText().toString());//here is your updated(or not updated) text
adapter.notifyDataSetChanged();
dismiss();
try {
    saveItToDB();
} catch (Exception e) {
    e.printStackTrace();
}


}

private void saveItToDB() {
dba.open();
dba.updateDiaryEntry(((TextView) editText).getText().toString(), 0);
dba.close();
((TextView) editText).setText("");
}
}

Here is MyDB class code:

public class MyDB {
private static final String TABLE_NAME = null;
private static final String KEY_ID = null;
private SQLiteDatabase db;
private final Context context;
private final MyDBhelper dbhelper;



// Initializes MyDBHelper instance
public MyDB(Context c){

    context = c;
    dbhelper = new MyDBhelper(context, Constants.DATABASE_NAME, null,
                                        Constants.DATABASE_VERSION);

}


// Closes the database connection
public void close()
{
    db.close();

}

// Initializes a SQLiteDatabase instance using MyDBhelper
public void open() throws SQLiteException
{

    try {
        db = dbhelper.getWritableDatabase();
    } catch(SQLiteException ex) {
        Log.v("Open database exception caught", ex.getMessage());
        db = dbhelper.getReadableDatabase();
    }

}

// updates a diary entry (existing row)
    public boolean updateDiaryEntry(String title, long rowId)
    {

        ContentValues newValue = new ContentValues();
        newValue.put(Constants.TITLE_NAME, title);

        return db.update(Constants.TABLE_NAME, newValue, Constants.KEY_ID + "=" + rowId, null)>0;

    }

// Reads the diary entries from database, saves them in a Cursor class and returns it from the method
public Cursor getdiaries()
{
    Cursor c = db.query(Constants.TABLE_NAME, null, null,
                        null, null, null, null);
    return c;
}


}
mikdiet
  • 9,859
  • 8
  • 59
  • 68

1 Answers1

0

Your EditListItemDialog has two constructors, out of which only the other initialized dba. In the method causing the NPE you're calling methods on dba.

Why it doesn't crash is because you've wrapped the call to a try-catch.

To fix it, initialize dba in all constructors.

laalto
  • 150,114
  • 66
  • 286
  • 303