0

The aim is to retrieve the car park names from the car park tables columns 'CPNAME' and put those rows of names in another class' arraylist which a spinner will display. To be honest Im not even sure if this way will even show the rows in the spinner...

The problem at the moment is that when I go into the activity where the spinner accesses the arraylist that calls getCpnames(). I really cannot work out which part of my code is making other calls to the database to lock it other than getCpnames(). Thank you for the help in advance, I am pretty new to programming.

Error messages:

Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:256)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
atandroid.database.sqlite.SQLiteOpenHelper.getWritableDatabase
(SQLiteOpenHelper.java:164)
at com.example.parkangel.DbHelper.getCpnames(DbHelper.java:74)
at com.example.parkangel.BookTicket.<init>(BookTicket.java:19)

*My database class code: *

package com.example.parkangel;

import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper
{
private DbHelper dbHelper;
private Context ourContext;
private static DbHelper instance;
private SQLiteDatabase ourDatabase;

private static final String DATABASE_NAME = "CPDB.db";
private static final String DATABASE_TABLE = "CPTable";
private static final int DATABASE_VERSION = 1;

public static final String KEY_ID = "_id";
public static final String KEY_CPNAME = "cpname";
public static final String KEY_COST = "cost";

    public DbHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub 
    }

    public static DbHelper getInstance(Context context)
    {
        if (instance == null)
        {
            instance = new DbHelper(context);   
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_CPNAME + " TEXT NOT NULL, " + KEY_COST + " TEXT 
                                    NOT NULL);");

        db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values ('1','Learning 
                    Resource Center','2');");
        db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values ('2','Park and 
                    Ride','1');");
        db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values ('3','de 
                    Havilland Campus','2');");
        db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values ('4','Multi 
                    Storey Building','2');");
        db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values 
                    ('5','Reception','2');");   
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

    public synchronized DbHelper open() throws SQLException
    {
        System.out.println ("running open");
        if(ourDatabase == null || !ourDatabase.isOpen())
        ourDatabase = getWritableDatabase();
        return this;
    }   

    public String[] getCpnames()
    {
        if(ourDatabase == null || !ourDatabase.isOpen())
        ourDatabase = getWritableDatabase();
        String[] columns = new String[] {KEY_ID, KEY_CPNAME, KEY_COST};
        Cursor  c = ourDatabase.query(DATABASE_TABLE, columns, KEY_CPNAME,     
                    null, null, null, null);
        ArrayList<String> list = new ArrayList<String>();

        if (c != null)
        {
            c.moveToFirst();
            do
            {
                list.add(c.getString(0));
            }           
            while (c.moveToNext());
        }
        if (ourDatabase == null) System.out.println ("is null");

        return list.toArray(new String[]{});
    }   
}

This is the activity class where an araylist will store the rows retrieved rows:

package com.example.parkangel;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class BookTicket extends Activity implements OnClickListener{

Spinner spinner, spinner2;
String[] carParks = DbHelper.getInstance(this).getCpnames();
user3408604
  • 15
  • 1
  • 7
  • In getCpnames() method the our Database instance will be null first time so the if statement our Database.isOpen() Is giving a null pointer. Try to fix that condition. – Skd_2014 Mar 15 '14 at 03:35
  • 1
    `DbHelper.getInstance(this).getCpnames();` should be in onCreate as context is available once Activity is created – Raghunandan Mar 15 '14 at 05:00
  • Thank you so much guys, Ive got it past that stage but my spinner is displaying any rows, so not sure whats going on there but at least I can actually get onto that Activity without the application crashing – user3408604 Mar 15 '14 at 17:14
  • possible duplicate of [Android NullPointerException and GetDatabaseLocked](http://stackoverflow.com/questions/22340551/android-nullpointerexception-and-getdatabaselocked) – laalto Apr 07 '14 at 17:46
  • @ laalto, go through the question properly before referring it as duplicate.....!!!!!! – Giridhar Karnik Dec 26 '14 at 15:11

0 Answers0