尝试更新sql行时出现NullPointer异常

时间:2014-04-25 12:11:33

标签: android sqlite nullpointerexception

我在logcat中以黄色显示此错误,应用程序不会冻结。这是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)

这是用于更新行的文件:

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;
}

一般情况下,我无法获取更新sql中的行的代码。任何帮助将不胜感激。

@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("");
}
}

这是MyDB类代码:

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;
}


}

1 个答案:

答案 0 :(得分:0)

您的EditListItemDialog有两个构造函数,其中只有另一个构造函数初始化dba。在导致NPE的方法中,您在dba上调用方法。

为什么它不会崩溃是因为您已将呼叫包裹到try-catch

要修复它,请在所有构造函数中初始化dba