Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4084366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:25:51+00:00 2026-05-20T18:25:51+00:00

OK, well I’ve been working on a simple app that allows users to save

  • 0

OK, well I’ve been working on a simple app that allows users to save notes to contacts. It lists the contacts from the phone. User clicks on a contact and the notes entries that are related to the contact based on the contact ID are shown. The user can then add a new note by Menu>Add note. I can get all the way through the app but it wont save the data entered in the form to the database. When I try to save, I get a Null Pointer Exception. Nothing crashes, it just returns to the previous activity and doesn’t do anything. I am kind of new to programming and have been teaching myself android for the past week and a half, so it could be something really simple. I have been digging around the web and frequently come to Stack Overflow to check for answers but cant seem to find anything on this one. Also, I’m not sure what code is needed so I’m just going to post the code for my form activity and my dbadapter. Thanks in advance for any help!

This is the activity used to enter the data:

package com.onyx.formapp23;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.onyx.formapp23.MyDbAdapter;

public class FormsNewNote extends Activity {
    String mIntentString;
    int contactId;
    EditText contactIdEdit, titleEdit, bodyEdit;
    long dateTimeValue;
    Button submitBtn, discardBtn;
    private MyDbAdapter db = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forms_newnote);
        Bundle extras = getIntent().getExtras();
        mIntentString = extras.getString("contactId");
        db = new MyDbAdapter(this);
        contactIdEdit = (EditText) findViewById(R.id.noteFormContactId);
        contactIdEdit.setText(mIntentString);
        contactId = Integer.parseInt(mIntentString);
        titleEdit = (EditText) findViewById(R.id.noteFormTitle);
        bodyEdit = (EditText) findViewById(R.id.noteFormBody);
        dateTimeValue = java.lang.System.currentTimeMillis();
        submitBtn = (Button) findViewById(R.id.noteFormSave);
        discardBtn = (Button) findViewById(R.id.noteFormDiscard);
        submitBtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                try{
                    String titleInsert = titleEdit.getText().toString();
                    String bodyInsert = bodyEdit.getText().toString();
                    db.createNote(contactId, titleInsert, bodyInsert,  dateTimeValue);
                } catch (Exception e) {
                    String titleInsert = titleEdit.getText().toString();
                    String bodyInsert = bodyEdit.getText().toString();
                    e.printStackTrace();
                    Context context = getApplicationContext();
                    CharSequence text = "SQL Exception Thrown: " + e + "\nTitle: " + titleInsert + "\nBody: " + bodyInsert;
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                }
                finish();
            }
        });
    }


}

This is my Db adapter class:

package com.onyx.formapp23;

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 MyDbAdapter {
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    public static final String KEY_TITLE = "title";
    public static final String KEY_BODY = "body";
    public static final String KEY_CONTACTID = "contactId";
    public static final String KEY_ROWID = "_id";
    public static final String KEY_DATETIME = "datetime";
    private static final String TAG = "MyDbAdapter";
    private static final String DATABASE_CREATE =
        "create table contactNotes (_id integer primary key autoincrement, contactId integer, title text not null, body text not null, datetime long);";

    private static final String DATABASE_NAME = "OnyxDatabase";
    private static final String DATABASE_TABLE = "contactNotes";
    private static final int DATABASE_VERSION = 1;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            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 notes");
            onCreate(db);
        }
    }
    public MyDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }
    public MyDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
    public void close() {
        mDbHelper.close();
    }
    public long createNote(int contactId, String title, String body, long datetime) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_CONTACTID, contactId);
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_BODY, body);
        initialValues.put(KEY_DATETIME, datetime);
        return mDb.insertOrThrow(DATABASE_TABLE, null, initialValues);
    }

    public void createNote2(int contactId, String title, String body) {
        mDb.execSQL("insert into " + DATABASE_TABLE + " (" + KEY_CONTACTID + ", " + KEY_TITLE + ", " + KEY_BODY + ", " + KEY_DATETIME + ") values(" + contactId + ", " +
                title + ", " + body + ", " + java.lang.System.currentTimeMillis() + ");");
    }

    public boolean deleteNote(long rowId) {
        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }
    public Cursor fetchAllNotes() {
        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_CONTACTID,
                KEY_BODY, KEY_DATETIME}, null, null, null, null, null);
    }
    public Cursor fetchNotesForContact(String contactId) {
        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_CONTACTID, KEY_TITLE, KEY_BODY, KEY_DATETIME}, KEY_CONTACTID + " = " + contactId, null, null, null, new String (KEY_DATETIME + 
                " COLLATE LOCALIZED ASC"));
    }
    public Cursor fetchNote(long rowId) throws SQLException {
        Cursor mCursor =
            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                    KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    public boolean updateNote(long rowId, String title, String body) {
        ContentValues args = new ContentValues();
        args.put(KEY_TITLE, title);
        args.put(KEY_BODY, body);

        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}

And here is the Trace from the Eclipse Debug:

Thread [<1> main] (Suspended (entry into method createNote in MyDbAdapter)) 
    MyDbAdapter.createNote(int, String, String, long) line: 58  
    FormsNewNote$1.onClick(View) line: 43   
    Button(View).performClick() line: 2447  
    View$PerformClick.run() line: 9025  
    ViewRoot(Handler).handleCallback(Message) line: 587 
    ViewRoot(Handler).dispatchMessage(Message) line: 92 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4628    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 870  
    ZygoteInit.main(String[]) line: 628 
    NativeStart.main(String[]) line: not available [native method]  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:25:51+00:00Added an answer on May 20, 2026 at 6:25 pm

    Doesn’t look like you are calling MyDbAdapter.open() anywhere so in createNote() mDb is null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well behaved windows programs need to allow users to save their work when they
Well... simple question, right? But with no so simple answers. In firefox i use
Well, it seems simple enough, but I can't find a way to add a
Well this is incredibly frustrating. After being nagged by Rails that I need to
Well the subject is the question basically. Are there any version control systems out
Well, i've got a nice WPF book its called Sams Windows Presentation Foundation Unleashed.
Well, after a long time writing .net programs in C# I started to feel
Well, this is my first post here and really enjoying the site. I have
well i have this messages table with sample values like these: msg_id recipient_id read
Well, what is one?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.