我使用Volley将当前存储在SharedPreferences中的信息发送到远程mysql数据库。当我调用addDataToDB()方法时,我得到一个NullPointer异常。测试以查看值是否存在且它们是否存在(使用Log print out)。无法理解为什么我得到空指针异常。
在下面添加了相关代码。
用于向远程服务器发送信息的方法:
private void addDataToDB(){
username = preferences.getString("username", null);
firstname = preferences.getString("firstname", null);
lastname = preferences.getString("lastname", null);
Log.d("TAG", "addDataToDB: " + username + firstname + lastname); //Getting correct name print outs
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//not reaching here
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
Toast.makeText(MainActivity.this, "All data added", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Data not added!", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequestAll registerRequestAll = new RegisterRequestAll(
username, firstname, lastname, responseListener);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(registerRequestAll);
}
RegisterRequestAll Class:
class RegisterRequestAll extends StringRequest{
private static final String REGISTER_REQUEST_URL = "register.php";
private Map<String, String> params;
RegisterRequestAll(
String username, String firstname, String lastname, Response.Listener<String> listener) {
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("username", username);
params.put("firstname", firstname);
params.put("lastname", lastname);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
register.php:
<?php
require("connect.php"); // working file which connects to database
$username = $_POST["username"];
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$response = array();
$response["success"] = false;
function registerUser() {
global $connection, $username, $firstname, $lastname, $response;
$sql = "UPDATE user SET firstname=?, lastname=? WHERE username=?";
if($stmt = mysqli_prepare($connection, $sql)){
mysqli_stmt_bind_param($stmt, "sss", $firstname, $lastname, $username);
mysqli_stmt_execute($stmt);
}
mysqli_stmt_close($stmt);
mysqli_close($connection);
$response["success"] = true;
}
registerUser();
echo json_encode($response);
例外:
[263] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at java.net.URLEncoder.encode(URLEncoder.java:204)
at com.android.volley.Request.encodeParameters(Request.java:450)
at com.android.volley.Request.getBody(Request.java:436)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:260)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:234)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
答案 0 :(得分:0)
通常在发布null String
时发生params.put("username", username);
params.put("firstname", firstname);
params.put("lastname", lastname);
一个(或多个)用户名和...为空;