2011-08-12 18 views
0

便宜上このDBmanagerクラスを作成しましたが、getAllRows()メソッドはnullpointerexceptionsについて騒がしくなっています。フィルターをかけずにすべての行を取得するだけです。私は間違って何をしていますか?Android SQLiteクエリが機能しません

package com.com.com; 

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

public class DBManager { 

    private SQLiteDatabase db; // a reference to the database manager class. 
    private final String DB_NAME = "calls.db"; // the name of our database 
    private final int DB_VERSION = 1; // the version of the database 

    // the names for our database columns 
    private final String TABLE_NAME = "calls"; 
    private final String TABLE_ROW_ID = "id"; 
    public final String TABLE_ROW_ONE = "number"; 
    public final String TABLE_ROW_TWO = "date"; 

    public void addRow(String rowStringOne, String rowStringTwo){ 
     // this is a key value pair holder used by android's SQLite functions 
     ContentValues values = new ContentValues(); 

     // this is how you add a value to a ContentValues object 
     // we are passing in a key string and a value string each time 
     values.put(TABLE_ROW_ONE, rowStringOne); 
     values.put(TABLE_ROW_TWO, rowStringTwo); 

     // ask the database object to insert the new data 
     try 
     { 
      db.insert(TABLE_NAME, null, values); 
     } 
     catch(Exception e) 
     { 
      Log.e("DB ERROR", e.toString()); // prints the error message to the log 
      e.printStackTrace(); // prints the stack trace to the log 
     } 
    } 

    public Cursor getAllRows(){ 
     Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null, null); 
     //Cursor cursor = db.query(TABLE_NAME, new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO }, null, null, null, null, null); 
     return cursor; 
    } 

    /* 
    * SQLiteHelper Class 
    */ 
    private class CustomHelper extends SQLiteOpenHelper{ 

     public CustomHelper(Context context){ 
      super(context, DB_NAME, null, DB_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db){ 
      // the SQLite query string that will create our 3 column database table. 
      String newTableQueryString =  
        "create table " + 
          TABLE_NAME + 
          " (" + 
          TABLE_ROW_ID + " integer primary key autoincrement not null," + 
          TABLE_ROW_ONE + " text," + 
          TABLE_ROW_TWO + " text" + 
          ");"; 

      // execute the query string to the database. 
      db.execSQL(newTableQueryString); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ 
      // NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION. 
      // OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE 
      // FROM OLDER VERSIONS. 
     } 

    } 

} 

答えて

2

まあ、「ヌサプレサスについての誤解」は、ほとんど問題の詳細な説明ではありません。スタックトレースを送信すると役立ちます。あなたの場合、db変数を初期化していません。 これを行うにはgetWritableDatabase()に電話する必要があります。

+0

詳細な説明が不明なことをお詫び申し上げます。確かにあなたは正しいです、dbは初期化されていません。ありがとう! – fred

1

ここでは、NULLポインタ例外がヒントです。 dbはインスタンス化されていますか?それとも初期化されましたか?

+0

いいえ、それはありませんでした。ありがとう。 – fred

0

これは小さなことかもしれませんが、私はprivate final String TABLE_ROW_ID = "id"にあなたのIDがアンダースコアで始まらないことに気付きました。私はAndroidがこれを_idにする必要があると確信しています。ちょうど私の2セント。

関連する問題