简体   繁体   中英

Android NullPointerException when using Volley library

I have been following a tutorial ( http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ ) and when running the code for registration a encounter a nullpointerexception.

 02-11 16:40:57.795: E/AndroidRuntime(1024): java.lang.NullPointerException
 02-11 16:40:57.795: E/AndroidRuntime(1024): at activity.RegisterActivity.registerUser(RegisterActivity.java:209)

The following line of code is what causes the issue

ApplicationController.getInstance().addToRequestQueue(strReq, tag_string_req);

Because when I comment it out, everything runs find - well apart from the fact it stays stuck on the "registering spinner"

The code below is the RegisterUser method.

StringRequest strReq = new StringRequest(Method.POST,ApplicationServicesConfig.Register_URL, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Register Response: " + response.toString());
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");
                    if (!error) {
                        JSONObject user = jObj.getJSONObject("user");
                        String id = user.getString("id");
                        String name = user.getString("name");
                        String email = user.getString("email");
                        String dob = user.getString("dob");
                        String gender = user.getString("gender");
                        String created_at = user.getString("created_at");

                       db.addUser(name, email, dob, gender, created_at);

                        Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();

                        // Launch login activity
                        Intent intent = new Intent(
                                RegisterActivity.this,
                                LoginActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Registration Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting params to register url
                Map<String, String> params = new HashMap<String, String>();
                params.put("name", name);
                params.put("email", email);
                params.put("dob", dob);
                params.put("gender", gender);
                params.put("password", password);
                Log.d(TAG, "params: " + params);
                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String, String>();
                params.put("Content-Type","application/x-www-form-urlencoded");
                return params;
            }

        };
        // Adding request to request queue           
        ApplicationController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

My initial thoughts was that within the application controller class I was never actually creating a request queue (hence the null pointer exception).

Although within the .addToRequestQueue, one is created.

Code supplied for reference:

public class ApplicationController extends Application {

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

    private RequestQueue mRequestQueue;

    private static ApplicationController mInstance;

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

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

    public RequestQueue getRequestQueue() {
        Log.d(TAG, "request queue" + mRequestQueue);
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        Log.d(TAG ,"Within top request queue");
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        Log.d(TAG ,"Within bottom requeuest queue");
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

I have also (as you can see) placed some Logging within the Volley functionality, however that never gets called.

I'm hoping I'm just being ignorant,

Any ideas?

Thanks to @NguyenQuangAnh I realised that I didn't specify that ApplicationController is applications class.

I also looked on this page http://rominirani.com/android-application-class/ where I learn't more.

Basically: Updated android manifest

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@drawable/icon"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:name="app.ApplicationController" >

(Before you specify activities etc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM