NullPointerException addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference
Asked Answered
M

8

25

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.

Mildred answered 8/7, 2015 at 19:32 Comment(1)
Did you add AppController to the AndroidManifest.xml?Mild
D
78

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">
Datcha answered 8/7, 2015 at 19:42 Comment(4)
It's because of AppController, which extends Application.Datcha
Here you can read what Application is used for: developer.android.com/reference/android/app/Application.htmlDatcha
I am also facing the same problem. But I am making login and registration process in the middle of app. So I can not add it in the application tag. I tried adding it to the activity tag but still the same error. Any help ?Simferopol
WHAT A BOSS. Worked.Stanzel
M
7

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">
Mild answered 8/7, 2015 at 19:45 Comment(0)
H
6

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 it)
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        rq = Volley.newRequestQueue(YourActivity.this);  // rq != null
    }
    ...
    rq.add(request);
Harrisharrisburg answered 2/11, 2015 at 15:24 Comment(0)
A
3

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">
Acculturation answered 8/11, 2017 at 12:20 Comment(0)
C
2

Please check if you've initialized your requestQueue object as :

requestQueue = Volley.newRequestQueue(this);

Conflation answered 22/6, 2019 at 13:34 Comment(0)
W
0

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.....

Wolpert answered 30/6, 2016 at 20:17 Comment(0)
P
0

try this inside onCreate()

requestQueue=Volley.newRequestQueue(getApplicationContext());
Peale answered 22/9, 2020 at 13:49 Comment(0)
W
0

I have the same error because i have mistakenly referenced the wrong xml file in onCreate method

// R.layout.activity_calender should be the same xml file of your activity (CalenderActivity)
setContentView(R.layout.activity_calender);

Whole answered 3/1, 2021 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.