Note that there are some explanatory texts on larger screens.

plurals
  1. POAttempting to add entries to SQLite Database stops app from working
    primarykey
    data
    text
    <p>I'm trying to get started on creating my first real practice app on android and I've run into a bit of trouble. It's an app that will contain "ideas" in a sort of to do list style format. I finished making the activity where it will be displayed and the activity that will add the idea but upon clicking the button the application stops working. After debugging it I found out that the reason is in the onClick method, specifically the portion that is:</p> <pre><code>06-26 22:44:26.590 1136-1136/com.ivywire.ideastem E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.ivywire.ideastem.IdeasDbAdapter.createIdea(IdeasDbAdapter.java:78) at com.ivywire.ideastem.IdeaAddActivity$1.onClick(IdeaAddActivity.java:44) at android.view.View.performClick(View.java:4204) at android.view.View$PerformClick.run(View.java:17355) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>So I tried taking out some parts</p> <pre><code>Idea idea = new Idea(name, summary); databaseHandler.createIdea(idea); </code></pre> <p>when taken out it works. This is the full activity code. </p> <pre><code>import android.content.Intent; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.view.View.OnClickListener; public class IdeaAddActivity extends Activity { IdeasDbAdapter databaseHandler; private EditText ideaName; private EditText ideaSummary; private Button addButton; String name; String summary; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ideaadd); ideaName = (EditText) findViewById(R.id.ideaName); ideaSummary = (EditText) findViewById(R.id.ideaSummary); addButton = (Button) findViewById(R.id.addIdeaButton); name = ideaName.getText().toString(); summary = ideaSummary.getText().toString(); databaseHandler = new IdeasDbAdapter(this); addButton.setOnClickListener(addListener); } private OnClickListener addListener = new OnClickListener(){ public void onClick(View view){ Idea idea = new Idea(name, summary); databaseHandler.createIdea(idea); sendToMain(view); } }; public void sendToMain(View view){ Intent intent = new Intent(this, MainActivity.class); startActivity(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.idea_add, menu); return true; } } </code></pre> <p>This is the IdeasDbAdapter class:</p> <pre><code>import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class IdeasDbAdapter { public static final String KEY_ROWID = "_id"; public static final String KEY_NAME = "name"; public static final String KEY_SUMMARY = "summary"; private static final String TAG = "IdeasDbAdapter"; private DatabaseHelper mDbHelper; private SQLiteDatabase mDb; private static final String DATABASE_NAME = "Brainstorm"; private static final String SQLITE_TABLE = "Ideas"; private static final int DATABASE_VERSION = 1; private final Context mCtx; private static final String DATABASE_CREATE = "CREATE TABLE if not exists " + SQLITE_TABLE + " (" + KEY_ROWID + " integer PRIMARY KEY autoincrement," + KEY_NAME + "," + KEY_SUMMARY + ");"; private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { Log.w(TAG, DATABASE_CREATE); db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE); onCreate(db); } } public IdeasDbAdapter(Context ctx) { this.mCtx = ctx; } public IdeasDbAdapter open() throws SQLException { mDbHelper = new DatabaseHelper(mCtx); mDb = mDbHelper.getWritableDatabase(); return this; } public void close() { if (mDbHelper != null) { mDbHelper.close(); } } public long createIdea(Idea idea) { String name = idea.getName(); String summary = idea.getSummary(); ContentValues initialValues = new ContentValues(); initialValues.put(KEY_NAME, name); initialValues.put(KEY_SUMMARY, summary); return mDb.insert(SQLITE_TABLE, null, initialValues); } public boolean deleteAllIdeas() { int doneDelete = 0; doneDelete = mDb.delete(SQLITE_TABLE, null , null); Log.w(TAG, Integer.toString(doneDelete)); return doneDelete &gt; 0; } public Cursor fetchIdeasByName(String inputText) throws SQLException { Log.w(TAG, inputText); Cursor mCursor = null; if (inputText == null || inputText.length () == 0) { mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_SUMMARY}, null, null, null, null, null); } else { mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_SUMMARY}, KEY_NAME + " like '%" + inputText + "%'", null, null, null, null, null); } if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; } public Cursor fetchAllIdeas() { Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_SUMMARY}, null, null, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; } } </code></pre> <p>Line 78 that was referred in the nullpointer exception was </p> <pre><code>return mDb.insert(SQLITE_TABLE, null, initialValues); </code></pre> <p>If anybody could give me tips since I'm fairly confused and searching as best as I can, I'd appreciate it.</p>
    singulars
    1. This table or related slice is empty.
    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