2011-08-12 6 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

글쎄, 'null pointerexceptions에 대한 불만'은 거의 문제에 대한 자세한 설명이 아닙니다. stacktrace를 게시하면 도움이됩니다. 귀하의 경우 db 변수를 초기화하지 않습니다. 이렇게하려면 getWritableDatabase()으로 전화해야합니다.

+0

자세한 설명이 부족하여 죄송합니다. 실제로 당신은 맞습니다, DB는 초기화되지 않았습니다. 감사! – fred

1

null 포인터 예외가 여기에 있습니다. db이 인스턴스화 되었습니까? 또는 그 문제에 대해 초기화 되었습니까?

+0

아니요, 그렇지 않았습니다. 감사. – fred

0

작은 문제 일 수도 있지만 private final String TABLE_ROW_ID = "id"에 ID가 밑줄로 시작되지 않는 것으로 나타났습니다. 나는 안드로이드가 _id이 필요하다고 확신한다. 그냥 내 두 센트.

관련 문제