Question

I'm trying to get data entered into a registration form to be saved into a database. Here is my database: package com.NebulaNewsCo.gameofwin;

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 gowDbAdapter {

    public static final String KEY_USERNAME = "username";
    public static final String KEY_PASSWORD = "password";
    public static final String KEY_ROWID = "_id";

    private static final String TAG = "gowDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    /**
     * Database creation sql statement
     */
    private static final String DATABASE_CREATE =
        "create table notes (_id integer primary key autoincrement, "
        + "title text not null, body text not null);";

    private static final String DATABASE_NAME = "gow";
    private static final String DATABASE_TABLE = "users";
    private static final int DATABASE_VERSION = 2;

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

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public gowDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    /**
     * Open the notes database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public gowDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }


    /**
     * @param username
     * @param password
     * @return rowId or -1 if failed
     */
    public long createUser(String username, String password) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_USERNAME, username);
        initialValues.put(KEY_PASSWORD, password);

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    /**
     * Delete the user with the given rowId
     * 
     * @param rowId id of user to delete
     * @return true if deleted, false otherwise
     */
    public boolean deleteUser(long rowId) {

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

    /**
     * Return a Cursor over the list of all users in the database
     * 
     * @return Cursor over all users
     */
    public Cursor fetchAllUsers() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME,
                KEY_PASSWORD}, null, null, null, null, null);
    }

    /**
     * Return a Cursor positioned at the user that matches the given rowId
     * 
     * @param rowId id of user to retrieve
     * @return Cursor positioned to matching user, if found
     * @throws SQLException if note could not be found/retrieved
     */
    public Cursor fetchUser(long rowId) throws SQLException {

        Cursor mCursor =

            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                    KEY_USERNAME, KEY_USERNAME}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    /**
     * Update the user using the details provided. The user to be updated is
     * specified using the rowId, and it is altered to use the username and password
     * values passed in
     * 
     * @param rowId id of user to update
     * @param username value to set users username to
     * @param password value to set users password to
     * @return true if the user was successfully updated, false otherwise
     */
    public boolean updateUser(long rowId, String username, String password) {
        ContentValues args = new ContentValues();
        args.put(KEY_USERNAME, username);
        args.put(KEY_PASSWORD, password);

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

and here is my registration form java package com.NebulaNewsCo.gameofwin;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Register extends Activity {
private EditText mUsernameText;
private EditText mPasswordText;
private Long mRowId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mUsernameText = (EditText) findViewById(R.id.reg_user_edit);
    mPasswordText = (EditText) findViewById(R.id.reg_pass_edit);

    Button confirmButton = (Button) findViewById(R.id.reg_submit_btn);

    mRowId = null;
    Bundle extras = getIntent() .getExtras();
    if (extras != null) {
        String title = extras.getString(gowDbAdapter.KEY_USERNAME);
        String body = extras.getString(gowDbAdapter.KEY_PASSWORD);
        mRowId = extras.getLong(gowDbAdapter.KEY_ROWID);

        if (title != null) {
            mUsernameText.setText(title);
        }
        if (body != null) {
            mPasswordText.setText(body);
        }
    }

    confirmButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Bundle bundle = new Bundle();

            bundle.putString(gowDbAdapter.KEY_USERNAME, mUsernameText.getText().toString());
            bundle.putString(gowDbAdapter.KEY_PASSWORD, mPasswordText.getText().toString());
            if (mRowId != null) {
                bundle.putLong(gowDbAdapter.KEY_ROWID, mRowId);
            }
            Intent mIntent = new Intent();
            mIntent.putExtras(bundle);
            setResult(RESULT_OK, mIntent);
            finish();
        }
    });
}


/** Called when the user clicks the Go Back button */
public void goBack(View view) {
    Intent intent = new Intent(Register.this, Home.class);
    startActivity(intent);
    finish();
}

/** Called when the user clicks the Submit button */
public void regSubmit(View view) { 
    Intent intent = new Intent(this, RegisterComplete.class);       
    startActivity(intent);
    finish();
}

}

When I open the registration activity when testing it crashes. If you need anymore info please let me know.

EDIT:

Here is the error log:

04-06 13:58:59.102: E/Trace(4540): error opening trace file: No such file or directory (2)
04-06 13:58:59.540: I/dalvikvm-heap(4540): Grow heap (frag case) to 11.726MB for 5644816-byte allocation
04-06 13:58:59.842: I/dalvikvm-heap(4540): Grow heap (frag case) to 14.118MB for 2508816-byte allocation
04-06 13:59:00.417: W/MMUMapper(4540): fail to register MVA, unsupported format(0x5)
04-06 13:59:00.644: W/MMUMapper(4540): fail to register MVA, unsupported format(0x5)
04-06 13:59:00.977: W/MMUMapper(4540): fail to register MVA, unsupported format(0x5)
04-06 13:59:01.474: I/SurfaceTextureClient(4540): [0x51768fa8] frames:3,   duration:1.008000, fps:2.975705
04-06 13:59:02.975: I/SurfaceTextureClient(4540): [0x51768fa8] frames:3,   duration:1.500000, fps:1.999203
04-06 13:59:03.967: W/dalvikvm(4540): threadid=1: thread exiting with uncaught exception (group=0x40f3b908)
04-06 13:59:03.983: E/AndroidRuntime(4540): FATAL EXCEPTION: main
04-06 13:59:03.983: E/AndroidRuntime(4540): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.NebulaNewsCo.gameofwin/com.NebulaNewsCo.gameofwin.Register}:  java.lang.NullPointerException
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2203)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.ActivityThread.access$600(ActivityThread.java:150)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.os.Looper.loop(Looper.java:153)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.ActivityThread.main(ActivityThread.java:5006)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at java.lang.reflect.Method.invokeNative(Native Method)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at java.lang.reflect.Method.invoke(Method.java:511)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at dalvik.system.NativeStart.main(Native Method)
04-06 13:59:03.983: E/AndroidRuntime(4540): Caused by: java.lang.NullPointerException
04-06 13:59:03.983: E/AndroidRuntime(4540):     at com.NebulaNewsCo.gameofwin.Register.onCreate(Register.java:39)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.Activity.performCreate(Activity.java:5076)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2167)
04-06 13:59:03.983: E/AndroidRuntime(4540):     ... 11 more
Was it helpful?

Solution 2

From the log, the line 39 of Register.java contains some null value that you operate on.

In the source, this seems to be the line

confirmButton.setOnClickListener(new View.OnClickListener() {

It looks like confirmButton is null, causing the NullPointerException to be thrown because you try to access that object. The reason for findViewById to return null is that the requested view was not found.

Your code does not seem to set any content view (eg. using setContentView), thus, you cannot find any view using findViewById.

OTHER TIPS

Open up log cat and find the error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top