dbString =“”;

时间:2016-04-07 22:24:42

标签: java android sql database sqlite

我经常遇到null异常错误,我总是最终解决它们,除非这次我几天都遇到这个错误。

这是:

在我的mainActivity中,我需要通过此方法从数据库中打印一些数据

  public void printDB(){

    String dbString= WinToDB.dbToString();
    wordDB.setText(dbString);
    word.setText("");

    meaningDB.setText(dbString);
    meaning.setText("");
}

dbString应该携带此方法从我的数据库处理程序返回的内容

 public String dbToString(){
    dbString="";
    SQLiteDatabase db = getWritableDatabase();
    String query= "SELECT * FROM  " + TABLE_NAME +  " WHERE 1 ";  // * star means all 'columns' 1 means all 'row'

    Cursor c = db.rawQuery(query, null);
    //move to first location
    c.moveToFirst();

    while (!c.isAfterLast()){
        if(c.getString(c.getColumnIndex("wishe")) != null){
            dbString += c.getString(c.getColumnIndex("wishe"));
            dbString += "\n";

        }
    }
    db.close();
   // db.releaseReference();

    return dbString;
}

但它抛出尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'

这是logcat:

  

.RuntimeException:无法启动活动ComponentInfo {com.mirdox.myapplication / com.mirdox.myapplication.MainActivity}:java.lang.NullPointerException:尝试调用虚方法'void android.widget.TextView.setText(java。 lang.CharSequence)'对空对象引用                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2362)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)                                                        在android.app.ActivityThread.access $ 900(ActivityThread.java:155)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1323)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:139)                                                        在android.app.ActivityThread.main(ActivityThread.java:5298)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在java.lang.reflect.Method.invoke(Method.java:372)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:950)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)                                                     引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'                                                        在com.mirdox.myapplication.MainActivity.printDB(MainActivity.java:79)                                                        在com.mirdox.myapplication.MainActivity.onCreate(MainActivity.java:29)                                                        在android.app.Activity.performCreate(Activity.java:6072)                                                        在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)                                                        在android.app.ActivityThread.access $ 900(ActivityThread.java:155)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1323)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:139)                                                        在android.app.ActivityThread.main(ActivityThread.java:5298)                                                        at java.lang.reflect.Method.invoke(Native Method)

出了什么问题,我该怎么办? 提前谢谢!

编辑 这是我的mainActivity的公共类的onCreate MainActivity扩展了AppCompatActivity {

EditText word, meaning;
TextView wordDB, meaningDB;
DBhandler WinToDB;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    WinToDB= new DBhandler(this, null, null, 1);
    printDB();

    //_________input________________________
    word= (EditText)findViewById(R.id.word);
    meaning= (EditText)findViewById(R.id.meaning);

    //_________output_______________________
    wordDB= (TextView)findViewById(R.id.textView);
    meaningDB= (TextView)findViewById(R.id.textView2);

****编辑2这是我的content_main.xml:我在这里添加代码时遇到了问题;这是evernote content_main.xml

上代码的链接

ps:我有printDb();在mainAcitvity中写在onCreate上。

1 个答案:

答案 0 :(得分:0)

wordDBwordmeaningDBmeaningnull,以第79行为准。