通过齐射,NullPointerException发送数据时出错

时间:2017-05-25 19:59:36

标签: java android android-volley

错误是我获取设备IMEI的方式。这个问题的长度或程序给我带来了麻烦。建议一个更好的方法?

这里是错误日志:后跟代码。

E/Volley: [353] 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 libcore.net.UriCodec.encode(UriCodec.java:132)
          at java.net.URLEncoder.encode(URLEncoder.java:57)
          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)               在com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)               在com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)               在com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

package com.medref;
import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
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 java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class Signup extends AppCompatActivity{

    Spinner spinner, date, month, year;
    EditText Username, Password, Phnumber;
    FloatingActionButton Signup;
    RadioGroup radioGroup;
    RadioButton radioButton;

    String Selecteddates, Selectedyear;
    String Dob;
    String m_deviceId;

    String Bloodgroups[] = new String[]{"Not Known","O+","O-","A+","A-","B+","B-","AB+","AB-"};
    String dates[] = new String[]{"Day","31", "30", "29","28", "27", "26", "25", "24", "23", "22", "21", "20", "19", "18",
    "17", "16", "15", "14", "13", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1"};

    String months[] = new String[]{"Month","01", "02", "03", "04", "05", "06", "07", "08",
    "09", "10", "11", "12"};

    ArrayAdapter<String> adapter, dayAdapter, MonthAdapter;
    String userName, phNumber, password, gender, bloodGroup;
    String monthtonumber = "";

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

        try{
            TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
            m_deviceId = TelephonyMgr.getDeviceId();
        }catch (Exception e){}

        spinner = (Spinner) findViewById(R.id.spinner);
        date = (Spinner) findViewById(R.id.date);
        month = (Spinner) findViewById(R.id.month);
        year = (Spinner) findViewById(R.id.year);

        Username = (EditText) findViewById(R.id.Username);
        Password = (EditText) findViewById(R.id.Password);
        Phnumber = (EditText) findViewById(R.id.PhNumber);

        radioGroup = (RadioGroup) findViewById(R.id.gender);

        Signup = (FloatingActionButton) findViewById(R.id.Signup);

        adapter = new ArrayAdapter(Signup.this, R.layout.support_simple_spinner_dropdown_item, Bloodgroups);
        spinner.setAdapter(adapter);

        dayAdapter = new ArrayAdapter(Signup.this, R.layout.support_simple_spinner_dropdown_item, months);
        date.setAdapter(dayAdapter);

        MonthAdapter = new ArrayAdapter(Signup.this, R.layout.support_simple_spinner_dropdown_item, dates);
        month.setAdapter(MonthAdapter);

        ////////adding years to the array through a loop
        ArrayList<String> years = new ArrayList<>();
        years.add("Year");
        int thisYear = Calendar.getInstance().get(Calendar.YEAR);
        for (int i = 1960; i <= thisYear; i++) {
            years.add(Integer.toString(i));
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, years);
        year.setAdapter(adapter);

        Signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getVerification()){
                    //Getting RadioButton Id
                    int id = radioGroup.getCheckedRadioButtonId();
                    radioButton = (RadioButton) findViewById(id);
                    gender = radioButton.getText().toString();

                    /////COncatinating DOb
                    Dob = Selectedyear+"-"+Selecteddates+"-"+monthtonumber;

                    //Getting all the field valuesn
                    userName = Username.getText().toString().trim();
                    phNumber = Phnumber.getText().toString().trim();
                    password = Password.getText().toString().trim();

                    try {
                        //Now we have everything, lets send the data to our database
                        String url = "http://192.168.1.9/loginfiles/sendvalues.php";

                        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                                new Response.Listener<String>() {
                                    @Override
                                    public void onResponse(String response) {

                                        if (response.equals("10")) {
                                            Toast.makeText(Signup.this, "No connection"
                                                    , Toast.LENGTH_SHORT).show();
                                        } else {
                                            Intent i = new Intent(com.medref.Signup.this, MainActivity.class);
                                            startActivity(i);
                                        }
                                    }
                                }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(Signup.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        }) {
                            @Override
                            protected Map<String, String> getParams() throws AuthFailureError {

                                Map<String, String> params = new HashMap<>();
                                params.put("name", userName);
                                params.put("number", phNumber);
                                params.put("password", password);
                                params.put("dob", Dob);
                                params.put("gender", gender);
                                params.put("bloodGroup", bloodGroup);
                                params.put("deviceid", m_deviceId);
                                return params;
                            }
                        };

                        RequestQueue requestQueue = Volley.newRequestQueue(Signup.this);
                        requestQueue.add(stringRequest);
                    }catch (Exception e){
                        Toast.makeText(com.medref.Signup.this, e.getMessage(),Toast.LENGTH_LONG).show();
                    }

                }else{
                    Toast.makeText(Signup.this, "Please fill out the form", Toast.LENGTH_SHORT).show();
                }
            }
        });

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                bloodGroup = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        date.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Selecteddates = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        month.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               monthtonumber = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        year.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Selectedyear= parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    protected boolean getVerification(){
        if(Username.getText().toString().trim().equals("")){
            Username.setError("Empty");
            return false;
        }else if(Phnumber.getText().toString().trim().equals("")){
            Phnumber.setError("Empty");
            return false;
        }else if(Password.getText().toString().trim().equals("")){
            Password.setError("Empty");
            return false;
        }else if(Selecteddates.equals("") || Selectedyear.equals("")){
            return false;
        }else{
            return true;
        }
    }
}    

1 个答案:

答案 0 :(得分:-1)

您无法将响应字符串与“10”进行比较。首先你应该声明

JSONObject jObj = new JSONObject(response);

然后使用getString方法获取您的JSON值。

String variable = jObj.getString("some_var");
if (variable.equals("10")) { ... }

无论如何,这仍然不是你的错误发生的原因。

尝试删除此行并检查错误是否仍然显示:

Toast.makeText(Signup.this, error.getMessage(), Toast.LENGTH_SHORT).show();

请编辑您的问题并添加完整的错误日志。