1

OK, please help me, I tried everything I have found online and still get this error. I have imported in assets folder prepopulated read only sqlite database with 100 rows, with columns _ID, QUESTION, ANSWER, OPTION1, OPTION2, OPTION3. I have 3 tables in this db with the same structure. I draw random question from database, and if user answer correctly I then ask him two more extra questions, not randomly but the question will be with the same ID of the first question, but from the table 2. For example: I ask to guess the flag with 4 possible answers, if the answer is correct I then use that question ID and ask him the question from Cities table "what's the capital of...", and if that is correct I ask him from the Currencies table "What's the currency used in..." using the same ID. So, here's the crash report:

java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.e(Log.java:293)
at rs.androidaplikacije.zastaveigradovi.Kviz20Hard.nextQuestionGrad(Kviz20Hard.java:378)
at rs.androidaplikacije.zastaveigradovi.Kviz20Hard.access$1(Kviz20Hard.java:321)
at rs.androidaplikacije.zastaveigradovi.Kviz20Hard$2.run(Kviz20Hard.java:69)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3744)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)

And here's the game class, part of it, method where I get the error:

private void nextQuestionCities() {

         flag.setVisibility(View.GONE);
         dodatnoPitanje.setVisibility(View.VISIBLE);

         TestAdapter mDbHelper = new TestAdapter(this);
            DataBaseHelper myDbHelper = new DataBaseHelper(this);

            if(!myDbHelper.checkDataBase()){
            mDbHelper.createDatabase();
            }

            try{  

                mDbHelper.open(); 

                Cursor c = mDbHelper.getTestDataGradovi(mCurrentID);
                if (c != null) {
                       if (c.moveToFirst()) {

                List<Answer> labels = new ArrayList<Answer>();



                labels.add(new Answer(c.getString(2), true));
                labels.add(new Answer(c.getString(3), false));
                labels.add(new Answer(c.getString(4), false));
                labels.add(new Answer(c.getString(5), false));

                tacanOdg = c.getString(2);

                Collections.shuffle(labels);

                dodatnoPitanje.setText(c.getString(1));

                bOdgovor1.setText(labels.get(0).option);
                bOdgovor1.setTag(labels.get(0));
                bOdgovor1.setOnClickListener(clickListenerGrad);

                bOdgovor2.setText(labels.get(1).option);
                bOdgovor2.setTag(labels.get(1));
                bOdgovor2.setOnClickListener(clickListenerGrad);

                bOdgovor3.setText(labels.get(2).option);
                bOdgovor3.setTag(labels.get(2));
                bOdgovor3.setOnClickListener(clickListenerGrad);

                bOdgovor4.setText(labels.get(3).option);
                bOdgovor4.setTag(labels.get(3));
                bOdgovor4.setOnClickListener(clickListenerGrad);

                score.setText("Score: " + brojacTacnihOdgovora);

                       }
                       else {
                           Log.e("empty cursor", null);
                       }
                    }
                    else {
                        Log.e("null cursor", null);
                    }

            }
            finally{   
                mDbHelper.close();
            }
     }

Error is on the line:

Log.e("empty cursor", null);

And here's my adapter class:

public class TestAdapter 
{
    protected static final String TAG = "DataAdapter";

    private final Context mContext;
    private SQLiteDatabase mDb;
    private DataBaseHelper mDbHelper;

    public TestAdapter(Context context) 
    {
        this.mContext = context;
        mDbHelper = new DataBaseHelper(mContext);
    }

    public TestAdapter createDatabase() throws SQLException 
    {
        try 
        {
            mDbHelper.createDataBase();
        } 
        catch (IOException mIOException) 
        {
            Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
            throw new Error("UnableToCreateDatabase");
        }
        return this;
    }

    public TestAdapter open() throws SQLException 
    {
        try 
        {
            mDbHelper.openDataBase();
            mDbHelper.close();
            mDb = mDbHelper.getReadableDatabase();
        } 
        catch (SQLException mSQLException) 
        {
            Log.e(TAG, "open >>"+ mSQLException.toString());
            throw mSQLException;
        }
        return this;
    }

    public void close() 
    {
        mDbHelper.close();
    }

     public Cursor getTestData(String whereClause){
             String sql ="SELECT * FROM tblPitanja WHERE 1 = 1 " + whereClause + " ORDER BY RANDOM() LIMIT 1";
             return mDb.rawQuery(sql, null);
         }

     public Cursor getTestDataGradovi(long id){
            String sql ="SELECT * FROM tblGradovi WHERE _ID = " + id;
            return  mDb.rawQuery(sql, null);
        }

     public Cursor getTestDataValute(long id){
             String sql ="SELECT * FROM tblValute WHERE _ID = " + id;
             return mDb.rawQuery(sql, null);
         }

}
4

1 回答 1

4

当您为此调用 Log.e 时进行更改:

Log.e("empty cursor", "");
Log.e("null cursor", "");

问题似乎是您向该方法发送 null 而他不接受 null ...

于 2013-05-23T12:11:46.090 回答