Java.lang.nullPointerException:尝试在空对象引用上调用虚方法'int java.lang.string.hashCode()

时间:2017-08-08 13:30:45

标签: java php android nullpointerexception

我需要帮助。 我正在做一个可以注册和签名的活动。在注册中,我遵循了我认为应遵循的每个程序。但是当我点击注册按钮时,我仍然遇到错误。我不知道错误产生的位置,这就是为什么我在registerclick方法上发现错误并且它给了我这个错误Java.lang.nullPointerException: Attempt to invoke virtual method 'int java.lang.string.hashCode() on a null object reference。请问它不是任何问题的重复,因为我搜索了每个看起来像这样的问题。仍然没有解决方案这是我的常量类

package tk.trythisout.learnit;



public class Constants {
private static final String ROOT_URL = "192.168.33.149/and/req/";
public static final String URL_REGISTER = ROOT_URL+"register.php";
}

RegisterActivity1.java

package tk.trythisout.learnit;

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

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class RegisterActivity1 extends AppCompatActivity {
    EditText FirstName, LastName, Email, IndexNumber, Password, Repassword, PhoneNumber;
    Spinner Level, Program;
    Button Register;
//    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register1);
        FirstName = (EditText) findViewById(R.id.txtFirstName);
        LastName = (EditText) findViewById(R.id.txtLastName);
        Email = (EditText) findViewById(R.id.txtEmail);
        IndexNumber = (EditText) findViewById(R.id.txtIndexNumber);
        Password = (EditText) findViewById(R.id.txtPassword);
        Repassword = (EditText) findViewById(R.id.txtRePassword);
        PhoneNumber = (EditText) findViewById(R.id.txtPhoneNumber);
        Level = (Spinner) findViewById(R.id.spinLevel);
        Program = (Spinner) findViewById(R.id.spinProgram);
        Register = (Button) findViewById(R.id.btnRegister);
//        progressDialog = new ProgressDialog(this);
        Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    registerClick();
                } catch (Exception e) {
                    Toast.makeText(RegisterActivity1.this, "error: "+e, Toast.LENGTH_LONG).show();
                }
            }
        });

    }

    private void registerClick() throws Exception  {
        final String firstName = FirstName.getText().toString().trim();
        final String lastName = LastName.getText().toString().trim();
        final String email = Email.getText().toString().trim();
        final String indexnumber = IndexNumber.getText().toString().trim();
        final String password = Password.getText().toString().trim();
        final String repassword = Repassword.getText().toString().trim();
        final String phonenumber = PhoneNumber.getText().toString().trim();
        final String level = Level.getSelectedItem().toString();
        final String program = Program.getSelectedItem().toString();
//        progressDialog.setMessage("Registering User...");
//        progressDialog.show();

            StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.URL_REGISTER, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
//                    progressDialog.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        Toast.makeText(RegisterActivity1.this, jsonObject.getString("message"), Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {

                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
//                    progressDialog.hide();
                    Toast.makeText(RegisterActivity1.this, "too bad: "+error, Toast.LENGTH_LONG).show();

                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<>();
                    params.put("indexnumber", indexnumber);
                    params.put("firstName", firstName);
                    params.put("lastName", lastName);
                    params.put("level", level);
                    params.put("email", email);
                    params.put("program", program);
                    params.put("password", password);
                    params.put("phonenumber",phonenumber);
                    return params;
                }
            };
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);



    }
}

register.php

<?php

require_once '../includes/dbOperation.php';

$response = array();

if ($_SERVER['REQUEST_METHOD']=='POST') {
    # code...
    if (isset($_POST['indexnumber']) and isset($_POST['firstname']) and isset($_POST['lastname'])
        and isset($_POST['level']) and isset($_POST['email'])
        and isset($_POST['program']) and isset($_POST['password']) and isset($_POST['phonenumber'])) 
    {
        # code...
        $db = new dbOperation();
        if ($db->createUser(
            $_POST['indexnumber'], $_POST['firstname'], $_POST['lastname'], $_POST['level'], $_POST['email'], $_POST['program'], $_POST['password'], $_POST['phonenumber'])) {
            # code...
            $response['error'] = false;
            $response['message'] = 'Successfully Registered';

        }else{
            $response['error'] = true;
            $response['message'] = 'Unknown error occurred';
        }
    }else{
        $response['error'] = true;
        $response['message'] = 'All fields are required, Pls input correct values into the fields';
    }
}
echo json_encode($response);

dboperation.php

<?php

class dbOperation{
    private $con;

    function __construct(){
        require_once dirname(__FILE__).'/DbConnect.php';
        $db = new DbConnect();
        $this->con = $db->connect();
    }

    public function createUser($indexnumber, $firstname, $lastname, $level, $email, $program, $password, $phonenumber)
    {
        # code...
        $password = md5($password);
        $stmt = $this->con->prepare("INSERT INTO `students`(`sn`, `IndexNumber`, `firstName`, `LastName`, `level`, `email`, `department`, `password`, `phonenumber`) VALUES (NULL,?,?,?,?,?,?,?,?)");
        $stmt->bind_param("ssssssss",$indexnumber, $firstname, $lastname, $level, $email, $program, $password, $phonenumber);

    if ($stmt->execute()) {
        # code...
        return true;
    }else{
        return false;
    }
   }
}

请帮忙。我被困了!

"main@2" prio=5 waiting
  java.lang.Thread.State: WAITING
      at com.android.volley.Request.<init>(Request.java:136)
      at com.android.volley.toolbox.StringRequest.<init>(StringRequest.java:43)
      at tk.trythisout.learnit.RegisterActivity1$4.<init>(RegisterActivity1.java:0)
      at tk.trythisout.learnit.RegisterActivity1.registerClick(RegisterActivity1.java:74)
      at tk.trythisout.learnit.RegisterActivity1.access$000(RegisterActivity1.java:27)
      at tk.trythisout.learnit.RegisterActivity1$1.onClick(RegisterActivity1.java:52)
      at android.view.View.performClick(View.java:4756)
      at android.view.View$PerformClick.run(View.java:19749)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5221)
      at java.lang.reflect.Method.invoke(Method.java:-1)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

这就是我得到的全部......

this = {RegisterActivity1$4@4262} "[ ] 192.168.33.149/and/req/register.php 0x0 NORMAL null"
 this$0 = {RegisterActivity1@4057} 
 mListener = null
 mCacheEntry = null
 mErrorListener = {RegisterActivity1$3@4263} 
 mEventLog = null
 mRequestQueue = null
 mRetryPolicy = {DefaultRetryPolicy@4271} 
 mSequence = null
 mTag = null
 mUrl = "192.168.33.149/and/req/register.php"
 mRequestBirthTime = 0
 mCanceled = false
 mDefaultTrafficStatsTag = 0
 mMethod = 1
 mResponseDelivered = false
 mShouldCache = true
 shadow$_klass_ = {Class@3966} "class tk.trythisout.learnit.RegisterActivity1$4"
 shadow$_monitor_ = -1782153838
Exception = {NullPointerException@4261} 
 cause = {NullPointerException@4261} "java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference"
 detailMessage = "Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference"
  value = {char[93]@4298} 
  count = 93
  hashCode = 154031741
  offset = 0
  shadow$_klass_ = {Class@1531} "class java.lang.String"
  shadow$_monitor_ = -1407867750
 stackState = {Object[17]@4287} 
 stackTrace = {StackTraceElement[0]@4288} 
 suppressedExceptions = {Collections$EmptyList@4289}  size = 0
 shadow$_klass_ = {Class@3006} "class java.lang.NullPointerException"
 shadow$_monitor_ = -2087831779
email = "fadf@sdgh.fyt"
firstName = "busaf"
indexnumber = "afa"
lastName = "adf"
level = "300"
password = "qwerty"
phonenumber = "5556321"
program = "BSc. Midwifery"
x0 = 1
x1 = "192.168.33.149/and/req/register.php"
x2 = {RegisterActivity1$2@4273} 
x3 = {RegisterActivity1$3@4263} 

1 个答案:

答案 0 :(得分:-6)

更改此按钮注册;到按钮注册

以及此Register.setOnClickListener()

register.setOnClickListener()