java null指针异常

时间:2016-07-01 13:59:36

标签: java android

基于不同的SO问题我理解java.null.pointer异常是什么我不知道为什么它会在这种情况下发生。总之,对声明为尚未定义的引用类型的变量的引用。

我有2个java文件RegisterActivity.java和AppController.java。

RegisterActivity.java文件中的以下调用会导致java.null.pointer异常:

  

AppController.getInstance()。addToRequestQueue(strReq,tag_string_req);

这是AppController文件中的方法:

  

public static synchronized AppController getInstance(){           return mInstance;       }

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static AppController mInstance;

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

    public static synchronized AppController getInstance() {
        return mInstance;
    }


//RegisterActivity.java
 public class RegisterActivity extends Activity {
 ...
     private void registerUser(final String name, final String email,
                          final String password) {
          ...
          AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
     }

 ...
 }
  

07-01 16:33:17.757 995-995 / mobi.blessd.user.blessd E / AndroidRuntime:FATAL EXCEPTION:main                                                                        处理:mobi.blessd.user.blessd,PID:995                                                                        显示java.lang.NullPointerException                                                                            在mobi.blessd.user.blessd.activity.RegisterActivity.registerUser(RegisterActivity.java:189)                                                                            在mobi.blessd.user.blessd.activity.RegisterActivity.access $ 300(RegisterActivity.java:35)                                                                            在mobi.blessd.user.blessd.activity.RegisterActivity $ 1.onClick(RegisterActivity.java:84)                                                                            在android.view.View.performClick(View.java:4508)                                                                            在android.view.View $ PerformClick.run(View.java:18675)                                                                            在android.os.Handler.handleCallback(Handler.java:733)                                                                            在android.os.Handler.dispatchMessage(Handler.java:95)                                                                            在android.os.Looper.loop(Looper.java:136)                                                                            在android.app.ActivityThread.main(ActivityThread.java:5584)                                                                            at java.lang.reflect.Method.invokeNative(Native Method)                                                                            在java.lang.reflect.Method.invoke(Method.java:515)                                                                            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1268)                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)                                                                            在dalvik.system.NativeStart.main(本地方法)

我的源代码来自以下教程http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

1 个答案:

答案 0 :(得分:2)

您是否将AppControler添加到了清单?

<application
        android:name=".AppController" //this one
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">