8 Replies - 3443 Views - Last Post: 28 February 2014 - 02:58 PM

#1 Yathushan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-February 14

Unable to insert data into SQLite database

Posted 28 February 2014 - 05:04 AM

Hi, I am having issues trying to insert data into my Database. I have ran the code on an emulator and confirmed that the database existed and that all the columns were present. I have also tried many different attempts a debuging the code and I think that the insertCards method is the main issue.

I have included my code below.

Database Handler
package yath.proto.cardgame;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import android.widget.Toast;

public class CardsDB extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "cards";
    public static final int DATABASE_VERSION = 1;
    public static final String TABLE_NAME = "nations";
    public static final String ID = "_id";
    public static final String NAME = "name";
    public static final String AREA = "areakm2";
    public static final String FOREST = "forestedareakm2";
    public static final String WATER = "totalrenewablewaterresourcescukm";
    public static final String POPULATION = "population";
    public static final String GDP = "gdp$gdppercapita";
    //Military Expenditure changed to mils
    public static final String MILITARY = "militaryexpenditure$";
    private SQLiteDatabase database;
    private String INFO;
    private Card crd;
    Context context;

    public CardsDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        Log.i(INFO, "Database being created");

        db.execSQL("CREATE TABLE " +
                TABLE_NAME + " (" +
                ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                NAME + " TEXT, " +
                AREA + " NUMERIC, " +
                FOREST + " NUMERIC, " +
                WATER + " NUMERIC, " +
                POPULATION + " NUMERIC, " +
                GDP + " NUMERIC, " +
                MILITARY + " NUMERIC);");

        Log.i(INFO, "Database created");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i(INFO, "Database being updated");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);

        Log.i(INFO, "Database updated");
    }

    public void open() {
        this.getWritableDatabase();
    }

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

    // Inserting a card
    public void insertCards(Card card) {
        open();

        try {

            ContentValues cv = new ContentValues();

            Log.i(INFO, "Begin inserting card data");

            //Australia
            cv.put(NAME, card.getName());
            cv.put(AREA, card.getArea());
            cv.put(FOREST, card.getForest());
            cv.put(WATER, card.getWater());
            cv.put(POPULATION, card.getPopulation());
            cv.put(GDP, card.getGDP());
            cv.put(MILITARY, card.getMilitary());

            if(database.insert(TABLE_NAME, null, cv) !=1) {
                Log.d("Database", "Card Added");
                Toast.makeText(context, "Cards have been added", Toast.LENGTH_SHORT).show();
        } else {
                Log.d("Database", "Card Not Added");
                Toast.makeText(context, "No cards have been added", Toast.LENGTH_SHORT).show();
            }
        }

        catch (Exception e) {
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
        }

        close();
    }

    // Adding more than one card
    public void populateCards() {

        Log.i(INFO, "Australia");

        this.insertCards(new Card("AUSTRALIA", 7692024, 1470832, 492, 23375180, 42640, 26116));
    }

    // Retrieving a card
    public Card getCard(int _ID) {

        Log.i(INFO, "Fetch card");

        String selectQuery = "SELECT  * FROM " + TABLE_NAME + " WHERE "
                + ID + " = " + _ID;

        Cursor c = database.rawQuery(selectQuery, null);

        if (c != null)
            c.moveToFirst();

        Card cd = new Card();
        cd.setID(c.getInt(c.getColumnIndex(ID)));
        cd.setName((c.getString(c.getColumnIndex(NAME))));
        cd.setArea(c.getInt(c.getColumnIndex(AREA)));
        cd.setForest(c.getInt(c.getColumnIndex(FOREST)));
        cd.setWater(c.getInt(c.getColumnIndex(WATER)));
        cd.setPopulation(c.getInt(c.getColumnIndex(POPULATION)));
        cd.setGDP(c.getInt(c.getColumnIndex(GDP)));
        cd.setMilitary(c.getInt(c.getColumnIndex(MILITARY)));

        Log.i(INFO, "Card data obtained");

        return cd;
    }
}




The logcat I got was as follows:

02-28 11:58:33.784  28032-28032/yath.proto.toptrumps I/﹕ Button pressed in start screen
02-28 11:59:06.604  28032-28032/yath.proto.toptrumps I/﹕ Button pressed in play screen
02-28 11:59:07.604  28032-28032/yath.proto.toptrumps I/﹕ Australia
02-28 11:59:07.664  28032-28032/yath.proto.toptrumps I/﹕ Database being created
02-28 11:59:07.664  28032-28032/yath.proto.toptrumps I/﹕ Database created
02-28 11:59:07.684  28032-28032/yath.proto.toptrumps I/﹕ Begin inserting card data
02-28 11:59:07.704  28032-28032/yath.proto.toptrumps W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x419f9ba8)
02-28 11:59:07.724  28032-28032/yath.proto.toptrumps E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: yath.proto.toptrumps, PID: 28032
    java.lang.RuntimeException: Unable to start activity ComponentInfo{yath.proto.toptrumps/yath.proto.toptrumps.SP_game}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.widget.Toast.<init>(Toast.java:93)
            at android.widget.Toast.makeText(Toast.java:241)
            at yath.proto.toptrumps.CardsDB.insertCards(CardsDB.java:99)
            at yath.proto.toptrumps.CardsDB.populateCards(CardsDB.java:110)
            at yath.proto.toptrumps.SP_game.onCreate(SP_game.java:22)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
02-28 11:59:09.314  28032-28032/yath.proto.toptrumps I/Process﹕ Sending signal. PID: 28032 SIG: 9



I have a feeling that I must have made a mistake but for the life of me cannot work out where. Any help would be much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Unable to insert data into SQLite database

#2 Apokio   User is offline

  • D.I.C Addict

Reputation: 135
  • View blog
  • Posts: 511
  • Joined: 14-August 09

Re: Unable to insert data into SQLite database

Posted 28 February 2014 - 06:44 AM

Your null pointer is in your toast on line 99. So you are throwing an exception and it is catching it and something is null in the Toast.makeText.
Was This Post Helpful? 0
  • +
  • -

#3 Yathushan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-February 14

Re: Unable to insert data into SQLite database

Posted 28 February 2014 - 09:52 AM

Right so I only just added the Toast.makeText. Before it was pointing at :

database.insert(TABLE_NAME, null, cv)



On line 89. Not sure if that helps?
Was This Post Helpful? 0
  • +
  • -

#4 Apokio   User is offline

  • D.I.C Addict

Reputation: 135
  • View blog
  • Posts: 511
  • Joined: 14-August 09

Re: Unable to insert data into SQLite database

Posted 28 February 2014 - 11:51 AM

This line
if(database.insert(TABLE_NAME, null, cv) !=1)

should be
if(database.insert(TABLE_NAME, null, cv) !=-1)

because insert returns -1 if nothing was inserted and the column id if it is successful. I don't know if that will fix your error but you can try it.
Was This Post Helpful? 0
  • +
  • -

#5 Yathushan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-February 14

Re: Unable to insert data into SQLite database

Posted 28 February 2014 - 01:42 PM

So I modified it with no avail so I tried to go back to the original code and I got this message.

02-28 20:37:59.031    5723-5723/yath.proto.toptrumps E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: yath.proto.toptrumps, PID: 5723
    java.lang.RuntimeException: Unable to start activity ComponentInfo{yath.proto.toptrumps/yath.proto.toptrumps.SP_game}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at yath.proto.toptrumps.CardsDB.insertCards(CardsDB.java:86)
            at yath.proto.toptrumps.CardsDB.populateCards(CardsDB.java:131)
            at yath.proto.toptrumps.SP_game.onCreate(SP_game.java:22)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
02-28 20:38:00.601    5723-5723/yath.proto.toptrumps I/Process﹕ Sending signal. PID: 5723 SIG: 9



Line 86 - Database Handler
database.insert(TABLE_NAME, null, cv);


Line 131 - Database Handler
this.insertCards(new Card("AUSTRALIA", 7692024, 1470832, 492, 23375180, 42640, 26116));


Line 22 - Main Activity
db.populateCards();



Sorry to pester you so much. It just seem to irritate me that I can't seem to work out what is going wrong.
Was This Post Helpful? 0
  • +
  • -

#6 Apokio   User is offline

  • D.I.C Addict

Reputation: 135
  • View blog
  • Posts: 511
  • Joined: 14-August 09

Re: Unable to insert data into SQLite database

Posted 28 February 2014 - 02:01 PM

Set a break point for line 86 and use the debugger to step through the code. See what is being passed to that insert method. I am thinking that maybe the cv is null and causing issues. Use the debugger to be sure.
Was This Post Helpful? 0
  • +
  • -

#7 Yathushan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-February 14

Re: Unable to insert data into SQLite database

Posted 28 February 2014 - 02:23 PM

When the debugger appears, I am not sure how to interpret the results so I have included a screenshot.

Attached Image

From what I can see the database seems null and the cv has captured the data that was send to be inserted.
Was This Post Helpful? 0
  • +
  • -

#8 Apokio   User is offline

  • D.I.C Addict

Reputation: 135
  • View blog
  • Posts: 511
  • Joined: 14-August 09

Re: Unable to insert data into SQLite database

Posted 28 February 2014 - 02:46 PM

You are never initializing the SQLiteDatabase database variable.
Change you open method to this
public void open() {
        database = this.getWritableDatabase();
    }



That should fix your issues with the null database.
Was This Post Helpful? 1
  • +
  • -

#9 Yathushan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-February 14

Re: Unable to insert data into SQLite database

Posted 28 February 2014 - 02:58 PM

That worked. Thank you sooo much!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1