-1

Alright, I've looked all over the website for a possible solution to send a POST request using android but I don't seem to understand the usage of Volley here or the method which was starting an async thread to make the request.

private RequestQueue signupRequestQ = Volley.newRequestQueue(this);

@Override
public void onClick(View view) {
    String postUrl = "https://mypost.url";
    signupRequestQ.start();

    switch (view.getId()) {
        case R.id.signupBtn:
            if (((EditText) findViewById(R.id.passwordSignup)).getText().toString().length() < 6) {
                Dialog.showInvalidInputFieldsDialog(this,"Password must be longer than 5 characters").show();
            } else {
                try {

                    StringRequest strRequest = new StringRequest(Request.Method.POST, postUrl,
                            response -> {
                                System.out.println(response);
                            },
                            error -> {
                                System.out.println("What the funk");
                            }) {
                        @Override
                        protected Map<String, String> getParams() {
                            LinkedHashMap<String, String> postForm = new LinkedHashMap<String, String>();
                            postForm.put("test", "var");
                            return postForm;
                        }
                    };

                    signupRequestQ.add(strRequest);
                } catch (ParseException e) {
                    System.out.println("Something went wrong");
                }

            }

    }

For some reason, this code causes my app to crash with a java.lang.RuntimeException and java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference.

However, I should not be encountering the NullPointerException as the Volley RequestQueue javadoc clearly stated that I only had to provide a context for the newRequestQueue method which I did.

Could anyone please help with my issue? Thank you!

xiurobert
  • 164
  • 12
  • The crash is not produced by your code. You are calling the "getCacheDir()" method from a null Context somewhere else. If you want help, please post the code where "getCacheDir()" is called. – Bubu Jun 26 '18 at 11:55
  • I did not call `getCacheDir()` at all in the code. The tooltip on Android Studio told me that the constructor for the request queue only took a `Context` – xiurobert Jun 26 '18 at 12:32
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Selvin Jun 26 '18 at 12:48
  • You are at the top of an Activity when you call "Volley.newRequestQueue(this)" ? – Bubu Jun 26 '18 at 13:07
  • @Bubu I did it right at the top of the Class – xiurobert Jun 27 '18 at 12:32
  • @Selvin no, that's not the issue. I used the documentation of Volley and they did not specify I had to provide a cache directory, I only had to provide a context which I did. Not a duplicate. – xiurobert Jun 27 '18 at 12:33

1 Answers1

1

It's because you instanciate your RequestQueue with a null Context at the top of your Activity :

private RequestQueue signupRequestQ = Volley.newRequestQueue(this); // "this" is null here

Try to create your RequestQueue in the onCreate() method of your Activity like :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    signupRequestQ = Volley.newRequestQueue(this);
}
Bubu
  • 1,533
  • 14
  • 14