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

标签 java php android mysql

我是一名新的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/

相关文章:

java - 如何使用 .sh 脚本运行具有不同 java 路径的 jar (Openj9)

android - 频繁的 Android 小部件更新

android - 在 Android (kotlin) 的 ListView 上向下滚动时发生错误

php - 使用包 ('H*' 验证字符串)

php - 在 Symfony2 中将 Facebook Connect 与 HWIOAuthBundle 绑定(bind)

php - 遍历 MySQL 列中的重复条目

android - 如何以编程方式在 Android 中禁用振动和触摸声音?

java - “mvnw”不被识别为内部或外部命令

java - 您如何将以下三元运算符转换为常规 if 语句?

java - Hibernate 还是应用程序服务器 JPA?