0

Please could you help me with the error "java.lang.NullPointerException". I am trying to save some data(name and email) in the database and then load it up. My application crashes in the emulator when I try to start it. I looked for answers and bumped into this thread: Error java.lang.NullPointerException on method getReadableDatabase() but it did not solve the issue.

MainActivity.java

package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
Button save, load;
EditText name, email;
DataHandler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    save = (Button)findViewById(R.id.save);
    load = (Button)findViewById(R.id.load);
    name = (EditText)findViewById(R.id.name);
    email = (EditText)findViewById(R.id.email);

    save.setOnTouchListener(new OnTouchListener(){
        public void onClick(View v){

        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            String getName = name.getText().toString();
            String getEmail = email.getText().toString();
            handler = new DataHandler(getBaseContext());
            handler.open();
            long id = handler.insertData(getName, getEmail);
            Toast.makeText(getBaseContext(), "Data inserted", Toast.LENGTH_LONG).show();
            handler.close();
            return false;
        }
    });

    load.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            String getName, getEmail;
            getName = "";
            getEmail = "";
            handler = new DataHandler(getBaseContext());
            handler.open();
            Cursor C = handler.returnData();
            if(C.moveToFirst())
            {
                do
                {
                    getName = C.getString(0);
                    getEmail = C.getString(1);
                }while(C.moveToNext());
            }
            handler.close();
            Toast.makeText(getBaseContext(), "Name : "+getName+" and email : "+getEmail, Toast.LENGTH_LONG).show();
            return false;
        }

    });

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

DataHandler.java

package com.example.test;

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.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataHandler {
public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String TABLE_NAME = "mytable";
public static final String DATA_BASE_NAME = "mydatabase";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_CREATE = "create table  mytable (name text not null, email text not null);";

DataBaseHelper dbhelper;
private static Context ctx;
SQLiteDatabase db;
public DataHandler(Context ctx)
{
    this.ctx = ctx;
    dbhelper = new DataBaseHelper(ctx);
}

private static class DataBaseHelper extends SQLiteOpenHelper
{
    public DataBaseHelper(Context ctx)
    {
        super(ctx, DATA_BASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        try{
        db.execSQL("TABLE_CREATE");
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS mytable");
        onCreate(db);

    }

}

public DataHandler open()
{
    db = dbhelper.getWritableDatabase();
    return this;
}

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

public long insertData(String name, String email)
{
    ContentValues content =  new ContentValues();
    content.put(NAME, name);
    content.put(EMAIL, email);
    return db.insertOrThrow(TABLE_NAME, null, content);
}

public Cursor returnData()
{
    return db.query(TABLE_NAME, new String[] {NAME, EMAIL}, null, null, null, null, null);
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity"   
tools:ignore="MergeRootFrame" > 

<EditText
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="36dp"
    android:ems="10"
    android:hint="Enter name here" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/load"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/save"
    android:layout_marginBottom="81dp"
    android:text="LOAD DATA" />

<EditText
    android:id="@+id/email"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/name"
    android:layout_below="@+id/name"
    android:layout_marginTop="41dp"
    android:ems="10"
    android:hint="Enter email here" />

<Button
    android:id="@+id/save"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/load"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="42dp"
    android:text="SAVE DATA" />

</RelativeLayout>

LogCat

05-11 14:09:06.363: E/AndroidRuntime(2259): FATAL EXCEPTION: main
05-11 14:09:06.363: E/AndroidRuntime(2259): Process: com.example.test, PID: 2259
05-11 14:09:06.363: E/AndroidRuntime(2259): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
05-11 14:09:06.363: E/AndroidRuntime(2259):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at android.os.Looper.loop(Looper.java:136)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at java.lang.reflect.Method.invoke(Method.java:515)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at dalvik.system.NativeStart.main(Native Method)
05-11 14:09:06.363: E/AndroidRuntime(2259): Caused by: java.lang.NullPointerException
05-11 14:09:06.363: E/AndroidRuntime(2259):     at com.example.test.MainActivity.onCreate(MainActivity.java:34)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at android.app.Activity.performCreate(Activity.java:5231)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-11 14:09:06.363: E/AndroidRuntime(2259):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-11 14:09:06.363: E/AndroidRuntime(2259):     ... 11 more

Reposting DataHandler.java after edit

package com.example.test;

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.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataHandler {
public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String TABLE_NAME = "mytable";
public static final String DATA_BASE_NAME = "mydatabase";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_CREATE = "create table  mytable (name text not null, email text not null)";

DataBaseHelper dbhelper;
Context ctx;
SQLiteDatabase db;
public DataHandler(Context ctx)
{
    this.ctx = ctx;
    dbhelper = new DataBaseHelper(ctx);
}

private static class DataBaseHelper extends SQLiteOpenHelper
{
    public DataBaseHelper(Context ctx)
    {
        super(ctx, DATA_BASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        try{
        db.execSQL(TABLE_CREATE);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS mytable");
        onCreate(db);

    }

}

public DataHandler open()
{
    db = dbhelper.getWritableDatabase();
    return this;
}

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

public long insertData(String name, String email)
{
    ContentValues content =  new ContentValues();
    content.put(NAME, name);
    content.put(EMAIL, email);
    return db.insertOrThrow(TABLE_NAME, null, content);
}

public Cursor returnData()
{
    return db.query(TABLE_NAME, new String[] {NAME, EMAIL}, null, null, null, null, null);
}
}
4
  • I guess it is missplace of } bracket in onCreate() method Commented May 12, 2014 at 3:21
  • Check whether activity_main layout contains save button. Also use return true; within your OnTouchListener. Finally remove public void onClick(View v){ } from your save.setOnTouchListener Commented May 12, 2014 at 3:52
  • @SathishKumar This does help starting up the app. But as soon as I enter name and email and hit save, it crashes. On LogCat it says 05-12 00:19:47.393: E/AndroidRuntime(3048): android.database.sqlite.SQLiteException: no such table: mytable (code 1): , while compiling: INSERT INTO mytable(email,name) VALUES (?,?) Commented May 12, 2014 at 4:23
  • @rde post your activity_main.xml file. Commented May 12, 2014 at 4:25

3 Answers 3

1

It does not seem a problem related to SQLite.

The trace you posted says the error is happening at line 34:

save.setOnTouchListener(new OnTouchListener(){

So your save button must be null when you are trying to apply the event.

Check if line 29 is actually returning you and instance:

save = (Button)findViewById(R.id.save);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I am not sure I understand what you are suggesting.
@rde From the code you posted I did think that the Save button is null when you try to apply the event, on line 34. What I am suggesting is that you make sure that the Save button is not null when you do apply the event, since the trace is suggesting it, and the button is dinamically referenced.
Actually there was something wrong with my activity_main.xml that's why the Save button became null. I edited it.
0

All the things you have did correctly, but missed one small thing within your onCreate() of DataHandler.java.

 @Override
        public void onCreate(SQLiteDatabase db)
        {
            try{
            //db.execSQL("TABLE_CREATE");
             // Here you passing `TABLE_CREATE` string instead of passing `TABLE_CREATE` variable's string. So kindly remove quotes surrounded by 'TABLE_CREATE' and try.
              db.execSQL(TABLE_CREATE);
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }

And change this,

public static final String TABLE_CREATE = "create table mytable (name text not null, email text not null);";

to

public static final String TABLE_CREATE = "create table mytable (name text not null, email text not null)";

3 Comments

Remove ; at the end of TABLE_CREATE
I removed the ; at the end of the TABLE_CREATE query, yet I get the same error: 05-12 00:51:48.673: E/AndroidRuntime(3225): android.database.sqlite.SQLiteException: no such table: mytable (code 1): , while compiling: INSERT INTO mytable(email,name) VALUES (?,?)
pls Uninstall your application from device and try
0

please use this ..

save.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        String getName = name.getText().toString();
        String getEmail = email.getText().toString();
        handler = new DataHandler(getBaseContext());
        handler.open();
        long id = handler.insertData(getName, getEmail);
        Toast.makeText(getBaseContext(), "Data inserted", Toast.LENGTH_LONG).show();
        handler.close();
        return false;
            }
        });

i think your ontouch() seems problem.please put my code instead of your code..its may be work..

1 Comment

Thanks. But it does not seem to help. I still get the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.