传递Long时出现空指针异常

时间:2014-03-30 06:03:47

标签: java android oop

我初始化了DBAdapter对象,但仍然在DBAdapter类的第166行Cursor c = db.query(true,TABLE_NAME,null, where, null, null, null, null, null);上获得空指针异常

03-30 07:47:19.519: E/AndroidRuntime(4349): FATAL EXCEPTION: main
03-30 07:47:19.519: E/AndroidRuntime(4349): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.assignment1/com.example.assignment1.controller.TripDetailsActivity}: java.lang.NullPointerException
03-30 07:47:19.519: E/AndroidRuntime(4349):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at android.os.Looper.loop(Looper.java:137)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at java.lang.reflect.Method.invokeNative(Native Method)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at java.lang.reflect.Method.invoke(Method.java:511)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at dalvik.system.NativeStart.main(Native Method)
03-30 07:47:19.519: E/AndroidRuntime(4349): Caused by: java.lang.NullPointerException
03-30 07:47:19.519: E/AndroidRuntime(4349):     at com.example.assignment1.controller.DBAdapter.getRow(DBAdapter.java:166)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at com.example.assignment1.controller.TripDetailsActivity.message(TripDetailsActivity.java:29)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at com.example.assignment1.controller.TripDetailsActivity.onCreate(TripDetailsActivity.java:25)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at android.app.Activity.performCreate(Activity.java:5104)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-30 07:47:19.519: E/AndroidRuntime(4349):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-30 07:47:19.519: E/AndroidRuntime(4349):     ... 11 more

TripDetailsActivity.java

package com.example.assignment1.controller;

import com.example.assignment1.R;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class TripDetailsActivity extends Activity {
    public static final String LOG="log";
    TextView mess;
    DBAdapter db;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(LOG,"Trip Details..");
        db=new DBAdapter(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);
        mess=(TextView)findViewById(R.id.message);
        Bundle b=getIntent().getExtras();
        Log.i(LOG,"------------------"+b.getString("ID"));
        String s=b.getString("ID");
        message(s);
    }

    public void message(String idInDB){
        Cursor cursor=db.getRow(idInDB);
        String row = "";
        if(cursor!=null){
    if (cursor.moveToFirst()) {
        int idDB = cursor.getInt(0);
        String name = cursor.getString(1);
        String studentNum = cursor.getString(2);
        String favColour = cursor.getString(3);
        row = "ID: " + idDB + "\n" 
                + "Name: " + name + "\n"
                + "Std#: " + studentNum + "\n"
                + "FavColour: " + favColour;
        Log.i("LOG",row);
    }
    cursor.close();
    mess.setText(row);
    }
        else {
            mess.setText("Cursor is null");
    }

    }
}

DBAdapter.java ---只是从TripDetaisActivity.java调用的方法

public Cursor getRow(Long idInDB) {
        String where = TRIP_ID + "=" + idInDB;
    Cursor c = db.query(true, TABLE_NAME, null, where, null, null, null, null, null);
    if (c != null) {
    c.moveToFirst();
        }
    return c;
}

2 个答案:

答案 0 :(得分:2)

您忘了初始化DBAdapter db;

答案 1 :(得分:1)

在以下一行......

Cursor cursor=db.getRow(idInDB);

db的对象DBAdapternull,因为您刚刚声明了它,但您没有初始化它。

db方法之前初始化message()对象...您的问题将得到解决。