0

I am trying to retrieve a vat number from my database to display when the user logs in. It was working fine and then all of a sudden stopped working and started to return the error:

org.json.JSONException: Value null at vat of type org.json.JSONObject$1 cannot be converted to int

and I'm unsure as to why. Any help would be greatly appreciated.

Here is the code:

                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success =jsonResponse.getBoolean("success");
                        if (success) {

                            int vat = jsonResponse.getInt("vat");
                            Intent intent = new Intent(LoginActivity.this, ChoiceSelectActivity.class);
                            intent.putExtra("username", username);
                            intent.putExtra("vat", vat);
                            LoginActivity.this.startActivity(intent);

                        }else{

                            AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                            builder.setMessage ("Login Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            };

            LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
            RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
            queue.add(loginRequest);
        }

And here is the PHP file:

{<?php
$con = mysqli_connect("xxx.com", "xxx", "xxx", "xxx");

$username = $_POST["username"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "SELECT * FROM Name WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID,   $username,  $password,  $vat);

$response = array();
$response["success"] = false;  

while(mysqli_stmt_fetch($statement)){
    $response["success"] = true;  
    $response["username"] = $username;
    $response["password"] = $password;
    $response["vat"] = $vat;
}

echo json_encode($response);
?>
2
  • You are storing passwords in plain text. PHP provides password_hash() and password_verify() please use them. And here are some good ideas about passwords If you are using a PHP version prior to 5.5 there is a compatibility pack available here Commented Jan 29, 2017 at 16:03
  • Have you stepped into the code to verify that the result is still returned? You could put a watch on the jsonResponse to see what's inside it. Sometimes API's change or go down or your network connection falters. Commented Jan 30, 2017 at 16:24

1 Answer 1

1

even i faced the same problem. Try to sync gradle, click on the icon. Then the gradle will finish building, later on you can instant run your app

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, but still no joy.
It didn't work, no. I' still trying to figure out what has gone wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.