Sheep
Sheep

Reputation: 1

Getting Errors while querying DB in Android

I'm new to Android programming, and rusty in java! I've been working on a small program that ask users to input some stuff to EditText's and once the time stops or the user click's Finish button, it searches the database for the values the user set. That said, I'm getting errors in that part. :(

public class UserScreen extends Activity implements View.OnClickListener {

    public TextView tvCounter;
    public EditText input;
    public Button finish;

    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamescreen);

        tvCounter = (TextView) findViewById(R.id.tvCounter);
        finish = (Button) findViewById(R.id.bFinish);
        finish.setOnClickListener(this);

        new CountDownTimer(30000, 1000) {

            public void onTick(long millisUntilFinished) {
                tvCounter.setText("seconds remaining: "
                        + Long.toString(millisUntilFinished / 1000));
            }

            public void onFinish() {
                tvCounter.setText("done!");
                searchDB();
            }
        }.start();
    }

    private SQLiteDatabase database;
    private DatabaseHelper dbHelper;

    public void searchDB() {
        Object tableList[] = { "table1", "table2", "table3", "table4",
                "table5", "table6" };
        Object columnList[] = { "column1", "column2", "column3", "column4", "column5", "column6" };
        int pList[] = { R.id.et1, R.id.et2, R.id.et3,
                R.id.et4, R.id.et5, R.id.et6 };

        database = dbHelper.getReadableDatabase();
        Cursor cursor = null;

        for (int i = 0; i < tableList.length; i++) {

            input = (EditText) findViewById(pList[i]);
            String data = input.getText().toString();

            cursor = database.rawQuery("SELECT * FROM " + tableList[i]
                    + " WHERE " + pList[i] + " = " + columnList[i], null);

            String dbStuff = cursor.toString();
                        if (dbStuff == data) { 
                            tvCounter.setText("YEAH");
                        } 
                        else { 
                        tvCounter.setText("DAMN"); 
                        }
        }
        cursor.close();
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        searchDB();
    }
}

And the Errors are:

07-28 17:43:59.385: E/AndroidRuntime(920): FATAL EXCEPTION: main
07-28 17:43:59.385: E/AndroidRuntime(920): java.lang.NullPointerException
07-28 17:43:59.385: E/AndroidRuntime(920): at com.example.androidgame.GameScreen.searchDB(GameScreen.java:56)
07-28 17:43:59.385: E/AndroidRuntime(920): at com.example.androidgame.GameScreen.onClick(GameScreen.java:84)
07-28 17:43:59.385: E/AndroidRuntime(920): at android.view.View.performClick(View.java:3511)
07-28 17:43:59.385: E/AndroidRuntime(920): at android.view.View$PerformClick.run(View.java:14105)
07-28 17:43:59.385: E/AndroidRuntime(920): at android.os.Handler.handleCallback(Handler.java:605)
07-28 17:43:59.385: E/AndroidRuntime(920): at android.os.Handler.dispatchMessage(Handler.java:92)
07-28 17:43:59.385: E/AndroidRuntime(920): at android.os.Looper.loop(Looper.java:137)
07-28 17:43:59.385: E/AndroidRuntime(920): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-28 17:43:59.385: E/AndroidRuntime(920): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 17:43:59.385: E/AndroidRuntime(920): at java.lang.reflect.Method.invoke(Method.java:511)
07-28 17:43:59.385: E/AndroidRuntime(920): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-28 17:43:59.385: E/AndroidRuntime(920): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-28 17:43:59.385: E/AndroidRuntime(920): at dalvik.system.NativeStart.main(Native Method)

Creation or acess to DB are in another class.

public class DBAccess extends Activity {

private DatabaseHelper databaseHelper;
private SQLiteDatabase db;


public void onCreate(Context context){
    databaseHelper = new DatabaseHelper(context);
    searchDB();
}

public void openDB(){
    db = databaseHelper.getWritableDatabase();
}
public void closeDB(){
    db.close();
}
public void searchDB(){
    Intent Scoring = getIntent();
    String rawdata[] = Scoring.getStringArrayExtra("data");
    openDB();
    Object tableList[] = { "table1", "table2", "table3", "table4", "table5", "table6" };
    Object columnList[] = { "KEY_COLUMN1", "KEY_COLUMN2", "KEY_COLUMN3", "KEY_COLUMN4", "KEY_COLUMN5",
    "KEY_COLUMN6" };

    int downScore = 0;
    int upScore = 0;
    String dbStuff;

    Cursor cursor = null;

    for (int i = 0; i < tableList.length; i++) {
        if(rawdata[i] == "niente")
            downScore = downScore + 5;
        else {
            cursor = db.rawQuery("SELECT * FROM " + tableList[i] + " WHERE " + rawdata[i] + " = " + columnList[i] , null);
        dbStuff = cursor.toString();
        if (i == 0)
             cursor.moveToFirst();
        else
            cursor.moveToNext();
        if(dbStuff == rawdata[i])
            upScore = upScore + 10;
        else
            downScore = downScore + 5;
        }
    }
    closeDB();
    cursor.close();
    Intent i = new Intent(this, ScoreScreen.class);
    i.putExtra("uScore", upScore);
    i.putExtra("dScore", downScore);
    }

}

Upvotes: 0

Views: 130

Answers (2)

CEO
CEO

Reputation: 270

Design-wise, separate DB construction and maintenance aspects, from the DB access layer itself.

For this, have 2 classes. 1) Put in DBHelper.java all the construction and maintenance, and 2) DBAccess.java have the methods to access the DB.

Below are code snippets for this... Note that the application (activities) themselves should know nothing and have no visibility about the construction of the DB itself (see visibility of DBHelper class itself), but instead only have full visibility to the simple access methods such as open, close, CRUD methods...


:

class DBHelper extends SQLiteOpenHelper {

    :
    :

    private SQLiteDatabase db;
    private static final int DATABASE_VERSION = 1;
    private static final String DB_NAME = "dbname.db";
    :
    :

}

public class DBAccess {

    :
    :

    private DBHelper dbHelper;
    private SQLiteDatabase db;

    :
    :

    public DBAccess(Context context) {
        dbHelper = new DBHelper(context);
    }

    /**
     * Open the database
     */
    public void openDB() {
        db = dbHelper.getWritableDatabase();
    }

    /**
     * Close the database
     */
    public void closeDB() {
        db.close();
    }

    :
    :
}

The activity the uses the DB, you can have a method called setupDB(), method that is called during the creation of the activity:

private void setupDB() {
    dbAccess = new DBAccess(this);
    dbAccess.openDB();
}

:

private void searchDB() {
    :

    mStatusList = dbAccess.selectAllStatus();

    :
}

:
:

Upvotes: 1

Todd Davies
Todd Davies

Reputation: 5522

You need to initialise your database and it's helper in onCreate():

  database = new SQLiteDatabase();
  dbHelper = new DatabaseHelper();

Upvotes: 0

Related Questions