Android Null指针异常不确定如何修复它

时间:2014-03-18 09:55:07

标签: java android nullpointerexception

我的代码中的一行上出现Null Pointer Exception的错误,我不知道如何修复它。它基本上只是另一种方法的副本,所以我不知道它为什么会给我错误。任何帮助将不胜感激:

import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class CurrentLesson extends Activity {

TabHost th;
TabSpec specs;
TimingsDb tDb = new TimingsDb(this);

Calendar c;
String lesson;
String week;
String day;

TextView tvTab1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.current_lesson_tabs);
    c = Calendar.getInstance();
    tvTab1 = (TextView) findViewById(R.id.textViewCLW1tv1);
    setupTabs();

    int h = c.get(Calendar.HOUR_OF_DAY);
    int m = c.get(Calendar.MINUTE);
    int w = c.get(Calendar.DAY_OF_WEEK);

    if ((h == 9) && (m <= 50)) {
        lesson = "1";
    } else if (((h == 9)&&((m > 50)&&(m <=59))) || ((h == 10)&&(m<=40))) {
        lesson = "2";
    } else if ((h == 11)&&(m<=50)) {
        lesson = "3";
    } else if (((h == 12)&&((m > 50)&&(m <=59))) || ((h == 13)&&(m<=40))) {
        lesson = "4";
    } else if (((h == 13)&&((m > 40)&&(m <=59))) || ((h == 14)&&(m<=30))) {
        lesson = "5";
    } else if (((h == 14)&&((m > 30)&&(m <=59))) || ((h == 15)&&(m<=20))) {
        lesson = "6";
    } else {
        lesson = "0";
    }

    switch(w){

    case 2:
        day = "Monday";
        break;
    case 3:
        day = "Tuesday";
        break;
    case 4:
        day = "Wednesday";
        break;
    case 5:
        day = "Thursday";
        break;
    case 6:
        day = "Friday";
        break;
    default:
        day = "";
    }//end switch

    week = "1";

    String temp;
    temp = tDb.getCurrent(day,week,lesson); //THIS IS WHERE I GET THE EXCEPTION

    if(!(temp == null)){
        tvTab1.setText(temp);
    }
}// end onCreate

public void setupTabs() {
    th = (TabHost) findViewById(R.id.tabhost2);
    th.setup();
    specs = th.newTabSpec("tag1");
    specs.setContent(R.id.tabCLWeek1);
    specs.setIndicator("Week One");
    th.addTab(specs);
    specs = th.newTabSpec("tag2");
    specs.setContent(R.id.tabCLWeek2);
    specs.setIndicator("Week Two");
    th.addTab(specs);
}

}// endClass

这是我认为导致错误的getCurrent()方法:

public String getCurrent(String day, String week, String lesson) {
    // TODO Auto-generated method stub

    String[] column = new String[] { KEY_SUBJECT };
    String selection = KEY_DAY + " = ? AND " + KEY_WEEK + " = ? AND "
            + KEY_LESSON + " = ?";
    String[] selectionArgs = new String[] { day, week, lesson };
    Cursor c = ourDatabase.query(DATABASE_TABLE, column, selection,
            selectionArgs, null, null, null);
    ;
    String result = "";
    if (c.moveToFirst()) {
        int iSubject = c.getColumnIndex(KEY_SUBJECT);
        result = c.getString(iSubject);
    }
    c.close();
    if (result == null) {
        result = "";
    }
    return result;

}

这就是logcat:

03-18 09:46:06.259: E/AndroidRuntime(20000): FATAL EXCEPTION: main
03-18 09:46:06.259: E/AndroidRuntime(20000): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shearer.schooltimetable/com.shearer.schooltimetable.CurrentLesson}: java.lang.NullPointerException
03-18 09:46:06.259: E/AndroidRuntime(20000):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at android.app.ActivityThread.access$600(ActivityThread.java:146)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at android.os.Looper.loop(Looper.java:137)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at android.app.ActivityThread.main(ActivityThread.java:5171)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at java.lang.reflect.Method.invokeNative(Native Method)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at java.lang.reflect.Method.invoke(Method.java:511)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at dalvik.system.NativeStart.main(Native Method)
03-18 09:46:06.259: E/AndroidRuntime(20000): Caused by: java.lang.NullPointerException
03-18 09:46:06.259: E/AndroidRuntime(20000):    at com.shearer.schooltimetable.TimingsDb.getCurrent(TimingsDb.java:132)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at com.shearer.schooltimetable.CurrentLesson.onCreate(CurrentLesson.java:77)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at android.app.Activity.performCreate(Activity.java:5200)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-18 09:46:06.259: E/AndroidRuntime(20000):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
03-18 09:46:06.259: E/AndroidRuntime(20000):    ... 11 more

2 个答案:

答案 0 :(得分:4)

似乎ourDatabasenull。请确保正确启动它。或者您可以尝试替换它(假设TimingsDb是打开帮助程序的包装器):

Cursor c = ourDatabase.query(DATABASE_TABLE, column, selection,
        selectionArgs, null, null, null);

用这个:

Cursor c = this.getWritableDatabase().query(DATABASE_TABLE, column, selection,
        selectionArgs, null, null, null);

答案 1 :(得分:1)

我想Android代码中也存在异常处理(对我来说看起来像Java代码)。如果是这样的话那么为什么不将你的代码放在try-catch块中并获取stacktrace并确定异常。它也是在Java中进行适当异常处理的编码标准。

从这里开始,由于数据库问题或代码中的逻辑是否存在异常,因此非常困难。