gedemagt
gedemagt

Reputation: 697

Android: Error with cursor in a SQLite database

I've made some code which worked perfectly, but after a minor clean-up in some code i get this error, and i've traced it down to the curser-action in the code. It logs the Log.d("Start-up",name) but not the Log.d("Start-up","Efter"). I really cant figure out what went wrong, i hope you can help me!

db.execSQL("CREATE TABLE " + DATABASE_TABLE2 + " (" + 
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            NAME + " TEXT NOT NULL, " +
            PLAYER_1 + " TEXT NOT NULL, " +
            PLAYER_2 + " TEXT NOT NULL, " +
            PLAYER_3 + " TEXT NOT NULL, " +
            PLAYER_4 + " TEXT NOT NULL, " +
            PLAYER_5 + " TEXT NOT NULL, " +
            ANTAL + " TEXT NOT NULL, " +
            OVERSIDDER + " TEXT NOT NULL, " +
            G + " TEXT NOT NULL, " +
            LOFT + " TEXT NOT NULL);"
            );




    public boolean doesNameExist(String name){
    String[] columns = new String[]{NAME};
    Log.d("Start-up",name);
    Cursor c = ourDatabase.query(DATABASE_TABLE2, columns, NAME + " = '"+ name + "'", null, null, null, null);
    Log.d("Start-up","Efter");
    return c.moveToFirst();
}

Here is my logcat:

05-02 16:20:58.168: W/dalvikvm(22533): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
05-02 16:20:58.178: E/AndroidRuntime(22533): FATAL EXCEPTION: main
05-02 16:20:58.178: E/AndroidRuntime(22533): java.lang.NullPointerException
05-02 16:20:58.178: E/AndroidRuntime(22533):    at com.jesper.whist.DBHelpeR.doesNameExist(DBHelpeR.java:217)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at com.jesper.whist.Indstillinger$2.onClick(Indstillinger.java:87)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at android.view.View.performClick(View.java:2408)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at android.view.View$PerformClick.run(View.java:8817)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at android.os.Handler.handleCallback(Handler.java:587)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at android.os.Looper.loop(Looper.java:144)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at android.app.ActivityThread.main(ActivityThread.java:4937)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at java.lang.reflect.Method.invokeNative(Native Method)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at java.lang.reflect.Method.invoke(Method.java:521)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at dalvik.system.NativeStart.main(Native Method)

EDIT: Entire class

package com.jesper.whist;

public class Indstillinger extends Activity{

//Variables
public static final String MY_SETTINGS = "MySettings";
EditText name,G,loft,player1,player2,player3,player4,player5;
String antal;
TextView playertext5, overskrift;
Typeface tf;
CheckBox femSpiller;
SharedPreferences settings;
Toast display;
int id;
Button ok_inst;
DBHelpeR entry;

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

    // Remove app-name
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    // Set heading font
    setContentView(R.layout.indstillinger);
    tf = Typeface.createFromAsset(getAssets(), "FargoFaroNF.ttf");
    overskrift = (TextView) findViewById(R.id.indstillinger);
    overskrift.setTypeface(tf);

    // Setting up Shared prefenceres and database
    settings = getSharedPreferences(MY_SETTINGS, 0);
    entry = new DBHelpeR(Indstillinger.this);

    // More variables
    G = (EditText) findViewById(R.id.etG);
    G.setText("15");
    loft = (EditText) findViewById(R.id.etLoft);
    loft.setText("10000");
    name = (EditText) findViewById(R.id.etName);
    player1 = (EditText) findViewById(R.id.etPlayer1);
    player2 = (EditText) findViewById(R.id.etPlayer2);
    player3 = (EditText) findViewById(R.id.etPlayer3);
    player4 = (EditText) findViewById(R.id.etPlayer4);
    player5 = (EditText) findViewById(R.id.etPlayer5);
    playertext5 = (TextView) findViewById(R.id.tvPlayer5);
    femSpiller = (CheckBox) findViewById(R.id.cb_fem);
    ok_inst = (Button) findViewById(R.id.bInst);

    // Toast
    display = Toast.makeText(this, "Navnet er optaget! Vælg et nyt!", Toast.LENGTH_SHORT);

    // Get id from Shared Preferences
    id = settings.getInt("game_id", 0);

    // Hvis 5-spillere-checkboxen checkes, vis indstillingsmuligheder
    femSpiller.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
            playertext5.setVisibility(View.VISIBLE);    // Vis TextView
            player5.setVisibility(View.VISIBLE);    // Vis EditText
        }
        });

    // Ved klik på ok-knappen
    ok_inst.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            // Check if name exist
            if(entry.doesNameExist(name.getText().toString())==true){
                display.show();     // Vis toast
            }
            // Hvis navnet ikke eksistere
            else{
                if(femSpiller.isChecked()){antal = "5";}    // Hvis der er 5 spillere
                else{antal = "4";}      // Hvis der er 4 spillere


                entry.open();

                // Lav entry i Spil_table: spil navn, spillernavne, antal, oversidder=5 så sidste man sidder over i første runde, grundtakst, loft
                entry.createEntrySpil(name.getText().toString(), player1.getText().toString(), player2.getText().toString(), player3.getText().toString(), player4.getText().toString(), player5.getText().toString(), antal,"5", G.getText().toString(), loft.getText().toString());
                entry.createEntryPenge(id,"0","0","0","0","0"); // Lav en nul-entry så det virker
                entry.close();

                // Start WHIST! og afslut denne proces
                startActivity(new Intent("com.jesper.whist.WHIST"));
                finish();
                }


        }
    });
}

}

Upvotes: 0

Views: 767

Answers (2)

Barak
Barak

Reputation: 16393

Well, from your logcat I would look in two places...

Indstillinger around line 87

and

DBHelpeR around line 217

Post those sections of code and maybe we can be a bit more helpful.

EDIT

Ok, I see that the cursor bit is posted (just waking up, sorry). I really don't see anything wrong with that query (assuming you have defined ourDatabase since you don't show that in the code you posted), so I'd look at the Indstillinger at line 87. That seems to be the first reference to your code in the error log.

2ND EDIT

You are using a method that requires the DB to be open before you open it. Do entry.open() before you call doesNameExist.

Upvotes: 1

Sam
Sam

Reputation: 86948

Without a specific error this is just a guess, but:

after a minor clean-up in some code i get this error

Did you change the table structure? If so you need to rebuild the database, the easiest way to do this is to increment the DATABASE_VERSION.

Addition

A null pointer exception means a variable, probably ourDatabase, hasn't been defined.

Upvotes: 0

Related Questions