我在检查表中的记录并将其与所说的单词进行比较时遇到问题。我正在开发一个类似文字扭曲的游戏。
该变量randomWord
是该random()
方法的结果。数据库中的单词必须在显示给用户之前随机化,并且不应在会话中重复。
顺便说一句,我的问题是exists()
方法;aNullPointerException
发生,看起来是因为表名是null
.
我该怎么办?
exists()
和random()
方法
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
实施
@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);
}
}
}
}