简体   繁体   中英

Null pointer exception for button value

I got a problem with my web services. My layout contains these fields

username  edittextbox

password  edittextbox

gender   button(male), button(female)

mailid   edittextbox

Whenever I click the male button for gender option it should take the string value as male and for female it should take female. I couldn't able to write the exact code as I am new to android becoz of which I'm getting null pointer exception in the logcat.

I want to add these string values to request obj of my webservices to send the data to server like request.addProperty("gender", gender); according to user selection

some one please help me to write the exact code for gender option so that i will not get any NPE in logcat

the code is

Button regmalebtn;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
public void register() {
    Log.v(TAG, "Trying to Login");

    EditText etxt_user = (EditText) findViewById(R.id.regetfirstname);
    EditText etxt_pass = (EditText) findViewById(R.id.regetlastname);
    EditText etxt_dob = (EditText) findViewById(R.id.regetdob);
    EditText etxt_email = (EditText) findViewById(R.id.regetemail);
    EditText etxt_password = (EditText) findViewById(R.id.regetpwd);
    EditText etxt_mobno = (EditText) findViewById(R.id.regetmobno);
    EditText deviceid = null;
    String fname = etxt_user.getText().toString();
    String lname = etxt_pass.getText().toString();
    String dob = etxt_dob.getText().toString();
    String email = etxt_email.getText().toString();
    String password = etxt_password.getText().toString();
    String mobno = etxt_mobno.getText().toString();
    String gender= regmalebtn.getText().toString();
    int latitude =**;
    int longitude= **;
    String  device_id= "12345";
    // regmalebtn.sette

//  DefaultHttpClient client = new DefaultHttpClient();
    //HttpPost httppost = new HttpPost(
    //      "http://.....");
    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    soapEnvelope.dotNet = true;
    soapEnvelope.setOutputSoapObject(request);



    //String response1 = request.getProperty(0).toString() ;
    request.addProperty("fname", fname);
    request.addProperty("lname", lname);
    request.addProperty("dateofbirth", dob);
    request.addProperty("email", email);
    request.addProperty("password", password);
    request.addProperty("mobno", mobno);
    request.addProperty("latitude", latitude);
    request.addProperty("longitude", longitude);
    request.addProperty("device_id", device_id);
    request.addProperty("gender", gender);
    //request.addProperty("latitude',latitude);





    try {
        androidHttpTransport.call(SOAP_ACTION, soapEnvelope);

        SoapPrimitive resultsRequestSOAP = (SoapPrimitive) soapEnvelope
                .getResponse();
        //SoapObject result = (SoapObject) soapEnvelope.getResponse();
        String resultData;
        resultData = request.getProperty(0).toString();
    } catch (Exception e) {

        e.printStackTrace();
    }

}

These are the logcat errors

06-16 11:55:58.955: ERROR/AndroidRuntime(471): FATAL EXCEPTION: Thread-8
06-16 11:55:58.955: ERROR/AndroidRuntime(471): java.lang.NullPointerException
06-16 11:55:58.955: ERROR/AndroidRuntime(471):     at com.soap.Register.register(Register.java:125)
06-16 11:55:58.955: ERROR/AndroidRuntime(471):     at com.soap.Register$1$1.run(Register.java:66)
06-16 11:55:58.965: WARN/ActivityManager(58):   Force finishing activity com.soap/.Register

You need a:

Button regmalebtn = (Button) findViewById(R.id.regetmalebtn);

Or something like that.

I cannot see where you're specifying your regmalebtn elsewhere, so this

String gender= regmalebtn.getText().toString();

Would cause a null pointer since you've only got this

Button regmalebtn;

atm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM