简体   繁体   中英

table name = null; NullPointerException Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent

I am having a problem with regards to checking records from the table and compare it to the word spoken. I am developing a text twist-like game.

The variable randomWord is the result of the random() method.. The words from the database must be randomized before displaying to user and it shouldn't repeat in a session.

BTW my problem is the exists() method; a NullPointerException occurs and it looks like it's because the table name is null .

What should I do with this?

exists() and random() method

 public boolean exists(String word) { 
         Cursor cursor = null;

         String WORD_TABLE = randomWord;
         System.out.println("exists: "+randomWord);
         String [] selectionArgs = {word + "%"};

         cursor = db.rawQuery("SELECT * from "+ WORD_TABLE + "WHERE Word like ?", selectionArgs);

        boolean exists = (cursor.getCount() > 0);

        cursor.close();

        return exists;
    }


 public String random(){


     List<Words> words = getAllWords();      


    for (Words wrd : words) {

        String log = "WORDPOOL: "+ wrd.getWord() +" ID: "+ wrd.getId();
        stringList.add(wrd.getWord());


              // Writing Contacts to log
      Log.d("Name: ", log);
     } 


     selectedWord = randomGenerator.nextInt(stringList.size());

     System.out.println("HEY"+stringList.remove(selectedWord)+" "+selectedWord);
     randomWord = stringList.remove(selectedWord);

        return randomWord;

}

logcat

07-20 16:49:04.171: E/AndroidRuntime(4081): FATAL EXCEPTION: main
07-20 16:49:04.171: E/AndroidRuntime(4081): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.flip/com.flip.main.friend}: java.lang.NullPointerException
07-20 16:49:04.171: E/AndroidRuntime(4081):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at android.os.Looper.loop(Looper.java:130)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at android.app.ActivityThread.main(ActivityThread.java:3687)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at java.lang.reflect.Method.invokeNative(Native Method)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at java.lang.reflect.Method.invoke(Method.java:507)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at dalvik.system.NativeStart.main(Native Method)
07-20 16:49:04.171: E/AndroidRuntime(4081): Caused by: java.lang.NullPointerException
07-20 16:49:04.171: E/AndroidRuntime(4081):     at com.flip.dao.DBHelper.exists(DBHelper.java:107)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at com.flip.main.friend.onActivityResult(friend.java:289)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
07-20 16:49:04.171: E/AndroidRuntime(4081):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
07-20 16:49:04.171: E/AndroidRuntime(4081):     ... 11 more

The implementation

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case RESULT_SPEECH: {
        if (resultCode == RESULT_OK && null != data) {

            ArrayList<String> text = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

            txtText.setText(text.get(0));
            System.out.println(""+text.get(0));
            String wanted = text.get(0);



            if(dbHelp.exists(wanted)){

                gameScore = text.get(0).length()*10;

                TextView score = (TextView) findViewById(R.id.scoreView);

                score.setText(""+gameScore);


                playSound(R.raw.correct);
           }
    }
    }
}

It's my problem mates.. the database is still not open.. For that, I called

db = this.getReadableDatabase

inside the exists method...

Thanks for replies... Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM