gpt4 book ai didi

java - 获取空指针并转换结果 java.lang.NullPointerException : lock == null

转载 作者:行者123 更新时间:2023-11-29 20:53:50 27 4
gpt4 key购买 nike

我是一名新的android开发人员,我正在使用Android Studio进行开发,我想使用php文件将数据插入MySQL

Java 代码是:

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Cus_Register extends AppCompatActivity {
Button submit;
EditText userName, pwd, fName, lName, phone, email;
String uName, pass, F_Name, L_Name, mail;
int mobile;
JSONParser jsonParser;
ProgressDialog progressDialog;
int value;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cus__register);

submit = (Button) findViewById(R.id.subCusRegBtn);
userName = (EditText) findViewById(R.id.userRegEditText);
pwd = (EditText) findViewById(R.id.pwdEditText);
fName = (EditText) findViewById(R.id.firstName_RegEditText);
lName = (EditText) findViewById(R.id.lastName_RegEditText);
phone = (EditText) findViewById(R.id.phone_reg_EditText);
email = (EditText) findViewById(R.id.email_reg_EditText);

jsonParser = new JSONParser();

submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

uName = userName.getText().toString();
pass = pwd.getText().toString();
F_Name = fName.getText().toString();
L_Name = lName.getText().toString();
Toast.makeText(Cus_Register.this,pass, Toast.LENGTH_SHORT).show();
mobile = Integer.parseInt(phone.getText().toString());
mail = email.getText().toString();


new AddCustomerReg().execute();

}
});
}



class AddCustomerReg extends AsyncTask<String, String, String> {

//Before Connection
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(Cus_Register.this);
progressDialog.setTitle("On Progress...");
progressDialog.show();

}

//After Connection
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (value == 1) {
Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Error is there...", Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}

//While the Connection
@Override
protected String doInBackground(String... params) {
List<NameValuePair> list = new ArrayList<NameValuePair>();

list.add(new BasicNameValuePair("userName", uName));
list.add(new BasicNameValuePair("Password", pass));
list.add(new BasicNameValuePair("fname", F_Name));
list.add(new BasicNameValuePair("lname", L_Name));
list.add(new BasicNameValuePair("phone", phone+""));
list.add(new BasicNameValuePair("email", mail));

JSONObject jsonObject = jsonParser.makeHttpRequest("http://192.168.100.15/we_deliver/add_cus.php", "POST", list);

try {
if (jsonObject != null && !jsonObject.isNull("Value")) {
System.out.println("Here");
value = jsonObject.getInt("Value");
} else {
System.out.println("Else Here");
value = 0;
}
} catch (Exception ex) {
ex.getStackTrace();
}

return null;
}
}

}

这个错误总是出现

06-13 13:26:38.430 10768-28236/zoghbi.abdulrahman.we_deliver E/Buffer Error: Error converting result java.lang.NullPointerException: lock == null
06-13 13:26:38.430 10768-28236/zoghbi.abdulrahman.we_deliver E/JSON Parser: Error parsing data org.json.JSONException: End of input at character 0 of

这是我的 PHP 文件

<?php

require_once ("db_connect.php");
$response = array();


if(isset($_POST['userName'])){
$user_name = $_POST['userName'];
$pwd = $_POST['Password'];
$fname = $_POST['fname'];
$lname= $_POST['lname'];
$phone= $_POST['phone'];
$email= $_POST['email'];

$db = new DB_Connect();

mysql_query("SET NAMES utf8");
$query = mysqli_query($db,"insert into customer (userName, password,fname,lname,password,phone,email) values
('$user_name', '$pwd', '$fname','$lname',$phone,'$email')");

if($query){
$response['Value'] = 1;

}else{
$response['Value'] = 0;
}
}else{
$response['Value'] = -1;
}

echo json_encode($response);

?>

代码将转到 jSonObject else 并在此处打印 Else,我提到过以了解错误在哪里

最佳答案

只是一个业余问题,您是否尝试直接创建 JsonParser

JSONParser jsonParser = new JSONParser();

或者也许可以做到public

关于java - 获取空指针并转换结果 java.lang.NullPointerException : lock == null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37795881/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com