将数据添加到SQL数据库可得到空对象引用

时间:2018-12-08 00:12:20

标签: android sqlite android-studio

我一直在处理一个问题,由于我遇到错误,导致无法将编辑文本和整数中的数据添加到SQL数据库中,导致无法添加数据并使崩溃应用。

我很感谢大家的投入或任何解决方案,因为我不确定我在这里做错了什么,并且我尝试了许多方法来解决它。

记录错误消息:

> java.lang.NullPointerException: Attempt to invoke virtual method 'boolean
com.infined.needomvp.GoalDatabaseHelper.addData(java.lang.String, int)' on a null object reference
        at com.infined.needomvp.GoalActivity.AddData(GoalActivity.java:99)
        at com.infined.needomvp.GoalActivity.onOptionsItemSelected(GoalActivity.java:74)
        at android.app.Activity.onMenuItemSelected(Activity.java:3450)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:407)
        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
        at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:63)
        at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:203)
        at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:780)
        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963)
        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624)
        at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150)

GoalDatabaseHelper:

public class  GoalDatabaseHelper extends SQLiteOpenHelper {
    public static final String TAG = "GoalDatabaseHelper";
    public static final String TABLE_NAME_G = "goal_table";
    public static final String COL1 = "_id";
    public static final String COL2 = "name";
    public static final String COL3 = "priority";

    public GoalDatabaseHelper(Context context) {
        super(context, TABLE_NAME_G, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME_G + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, '" +
                COL2 + "' TEXT ,'" + COL3 + "' INTEGER)";
        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_G);
        onCreate(db);
    }
    public boolean addData(String name, int pri) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, name);
        contentValues.put(COL3, pri);
        Log.d(TAG, "addData: Adding " + name + " to " + TABLE_NAME_G);
        long result = db.insert(TABLE_NAME_G, null, contentValues);

        //if data as inserted incorrectly it will return -1
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }
    public Cursor getItemID(String name){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT " + COL1 + " FROM " + TABLE_NAME_G +
                " WHERE " + COL2 + " = '" + name + "'";
        Cursor data = db.rawQuery(query, null);
        return data;
    }
    public void deleteName(int id, String name){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "DELETE FROM " + TABLE_NAME_G + " WHERE "
                + COL1 + " = '" + id + "'" +
                " AND " + COL2 + " = '" + name + "'";
        Log.d(TAG, "deleteName: query: " + query);
        Log.d(TAG, "deleteName: Deleting " + name + " from database.");
        db.execSQL(query);
    }
}

GoalActivity:

public class GoalActivity extends AppCompatActivity {
public static final String TAG = "GoalActivity";
private TextView title;
private EditText goalInput;
GoalDatabaseHelper mDatabaseHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_goal);
    Toolbar toolbar = (Toolbar) findViewById(R.id.goalBar);
    title = (TextView) findViewById(R.id.titleG);
    goalInput = (EditText) findViewById(R.id.goalEnter);
    Button DISPLAY = (Button)findViewById(R.id.DISPLAY);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("");

    final Typeface face = Typeface.createFromAsset(getAssets(),
            "fonts/SourceSansPro-SemiBold.ttf");
    final Typeface regface = Typeface.createFromAsset(getAssets(),
            "fonts/SourceSansPro-Regular.ttf");
    final Typeface smallface = Typeface.createFromAsset(getAssets(),
            "fonts/SourceSansPro-Light.ttf");

    goalInput.setText("");

    title.setTypeface(regface);

    DISPLAY.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG,"STAGE 0/1 addData: Adding ");
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.goal_bar, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_done_goal) {

        String Name = goalInput.getText().toString();

        int Pri = 100;

        if (goalInput.length() != 0) {
            AddData(Name, Pri);
            toastMessage("Task Name is" + Name);
            Intent i = new Intent(GoalActivity.this, GoalDisplayActivity.class);
            Log.d(TAG,"STAGE 2 addData: Adding " + Name);
            startActivity(i);
            overridePendingTransition(R.anim.fade_in_q, R.anim.fade_out_q);
            finish();
        } else {
            toastMessage("You must put something in the text field!");
        }
    }

    if (id == R.id.action_back_goal) {
        Toast.makeText(GoalActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
        Intent i = new Intent(GoalActivity.this, ListDataActivity.class);
        startActivity(i);

        return true;
    }

    return super.onOptionsItemSelected(item);

}

public void AddData(String name, int pri) {
    boolean insertData = mDatabaseHelper.addData(name, pri);
    Log.d(TAG,"STAGE 1 addData: Adding " + name);

    if (insertData) {
        toastMessage("Data Successfully Inserted!");
    } else {
        toastMessage("Something went wrong");
    }
}
private void toastMessage(String message){
    Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
}

}

我很感激!

0 个答案:

没有答案