NullPointerException addToRequestQueue(com.android

2019-01-17 21:50发布

问题:

I'm using AndroidHive register login and it's working fine in example project of this login-register.

But after many attempts trying it with CardViews and other widgets, this error appears on the LogCat:

java.lang.NullPointerException: Attempt to invoke virtual method 'void client.myproject.app.AppController.addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference
            at client.myproject.RegisterActivity.registerUser(RegisterActivity.java:185)
            at client.myproject.RegisterActivity.access$300(RegisterActivity.java:35)
            at client.myproject.RegisterActivity$1.onClick(RegisterActivity.java:81)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

While these codes works fine in a single app (just with register login).

I'm using Volley library.

回答1:

In your AndroidManifest.xml add

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


回答2:

As N1to says, you need to add your controller in the AndroidManifest.xml, if you don't add it then the onCreate() is never called and when you call AppController.getInstance() the instance is null.

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

It also works for me with:

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


回答3:

In my case, I forgot to initialize the variable rq, please, make sure you did it

    ...
    private RequestQueue rq;   // rq = null (NullPointerException if you use)
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        rq = Volley.newRequestQueue(YourActivity.this);  // rq != null
    }
    ...
    rq.add(request);


回答4:

In menifest file add appcontroller as shown

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


回答5:

You didn't pass any data to the volley method, that means it get null data (empty data)..... see example:

protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map=new HashMap<>();
                map.put(region, regionName);
                return map;
            }

if regionName is empty it will give you NullPointerException, so regionName must have something.....