android运行报错求教 空指针异常

报错日志 显示空指针异常
上日志
06-20 19:35:24.264 3122-3122/com.example.wangzhe.ssapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.wangzhe.ssapp, PID: 3122
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wangzhe.ssapp/com.example.wangzhe.ssapp.Drawer.Device_managment}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.lidroid.xutils.DbUtils com.example.wangzhe.ssapp.My.getDb()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.lidroid.xutils.DbUtils com.example.wangzhe.ssapp.My.getDb()' on a null object reference
at com.example.wangzhe.ssapp.Drawer.Device_managment.init(Device_managment.java:53)
at com.example.wangzhe.ssapp.Drawer.Device_managment.onCreate(Device_managment.java:40)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

日志提示位置

getDb();

package com.example.wangzhe.ssapp;

import android.app.Application;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.DbUtils.DaoConfig;

public class My extends Application {
private static My mInstance;
private DbUtils db;

public static My getInstance() {
    return mInstance;
}

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}


public DbUtils getDb() {
    if (db == null) {
        DaoConfig daoConfig = new DaoConfig(this);
        daoConfig.setDbName("data.db");
        daoConfig.setDbVersion(2);
        db = DbUtils.create(daoConfig);
    }
    return db;
}

}

1个回答

getInstance();
getDb();
都是 My的方法
my.getInstance().getDb();,同时调用两个方法不对,前一个方法已经返回了结果,不就结束了;
两个方法要分开写,或者用不同对象来调用,调用时也要分开。

yeziyfx
yeziyfx 哥们,什么叫前一个方法以及返回了结果就结束了。。。。怎么就结束了呢??getInstance()返回的是My类 的一个实例对象,对象再调getDb方法,这根本就没问题。楼主的问题所在应该是返回的mInstance现在是null了。
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问