从EditText获取值时出现java.lang.NullPointerException

时间:2015-04-02 13:36:27

标签: java android nullpointerexception android-edittext

我正在尝试从患者登记表中获取值到我的数据库中。但每次当我点击提交按钮时,应用程序将停止并显示以下消息:
不幸的是。申请已停止。

Logcat详细信息如下:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

Logcat Details

我的相同代码如下:

import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TimePicker;

public class indoor_patient extends Activity{

public EditText sr_no, patient_name, consultant_name, ref_dr, department,
        rel_name, rel_no, arr_date, arr_time;
Button submit;
ImageButton datepicker, timepicker;
Calendar cal;
int day, month, year;
int hour, min;

static final int TIME_DIALOG_ID = 1;

static final int DATE_DIALOG_ID = 0;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.indoor_patient_tbl);

    sr_no = (EditText) findViewById(R.id.etSrNo);
    patient_name = (EditText) findViewById(R.id.etPatientName);
    consultant_name = (EditText) findViewById(R.id.etConsultantDr);
    ref_dr = (EditText) findViewById(R.id.etRefDr);
    department = (EditText) findViewById(R.id.etDepartment);
    rel_name = (EditText) findViewById(R.id.etRelativeName);
    rel_no = (EditText) findViewById(R.id.etRelContact);
    arr_date = (EditText) findViewById(R.id.etArrDate);
    arr_time = (EditText) findViewById(R.id.etArrTime);

    submit = (Button) findViewById(R.id.btnIndoorSubmit);

    cal = Calendar.getInstance();

    day = cal.get(Calendar.DAY_OF_MONTH);
    month = cal.get(Calendar.MONTH);
    year = cal.get(Calendar.YEAR);
    datepicker = (ImageButton) findViewById(R.id.imgBtnDate);

    datepicker.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {

            showDialog(DATE_DIALOG_ID);
        }
    });


    hour = cal.get(Calendar.HOUR_OF_DAY);
    min = cal.get(Calendar.MINUTE);
    timepicker = (ImageButton) findViewById(R.id.imgBtnTime);

    timepicker.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID);
        }
    });

    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            new dbwork().execute();
        }
    });
}

@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, datePickerListener, year, month,
                day);
    case TIME_DIALOG_ID:    
        return new TimePickerDialog(this, timePickerListener, hour, min,
                false);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        arr_date.setText(selectedDay + "/" + (selectedMonth + 1) + "/"
                + selectedYear);
    }
};

private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        int hour;
        String am_pm;
        if (hourOfDay > 12) {
            hour = hourOfDay - 12;
            am_pm = "PM";
        } else {
            hour = hourOfDay;
            am_pm = "AM";
        }
        arr_time.setText(hour + ":" + min + " " + am_pm);
    }
};  
}

我的数据库连接文件如下:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.AsyncTask;

public class dbwork extends AsyncTask<Void, String, String> {

indoor_patient ins=new indoor_patient();    
String webUrl = "http://10.0.3.2:8084/data_web/indoorPatient.jsp?sr_no="
        + ins.sr_no.getText().toString()
        + ""
        + ins.patient_name.getText().toString()
        + ""
        + ins.consultant_name.getText().toString()
        + ""
        + ins.ref_dr.getText().toString()
        + ""
        + ins.department.getText().toString()
        + ""
        + ins.rel_name.getText().toString()
        + ""
        + ins.rel_no.getText().toString()
        + ""
        + ins.arr_date.getText().toString()
        + ""
        + ins.arr_time.getText().toString() + "&submit=Submit";

@Override
protected String doInBackground(Void... params) {
    // TODO Auto-generated method stub
    StringBuffer strbuffer=new StringBuffer("");
    String line="";

    try {

       URL url=new URL(webUrl);

        HttpURLConnection hurl;
        hurl=(HttpURLConnection)url.openConnection();

        BufferedInputStream bis;
        bis=new BufferedInputStream(hurl.getInputStream());

        InputStream is;
        is=bis;//implicit casting  // from downward to upward

        InputStreamReader isr;
        isr=new InputStreamReader(is);

        BufferedReader br;
        br=new BufferedReader(isr);

        //reading html form
        while ((line = br.readLine()) != null) {

            strbuffer.append(line);
        }

        bis.close();
        is.close();
        isr.close();
        br.close();

        hurl.disconnect();
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
    return strbuffer.toString();
}

@Override
protected void onPostExecute(String result) {

    super.onPostExecute(result);
   }
}

2 个答案:

答案 0 :(得分:1)

执行indoor_patient ins=new indoor_patient();时,并不意味着您致电onCreate()。因此,所有EditText组件都为空。

答案 1 :(得分:1)

您不能只是实例化Activity并访问其上的TextView成员。这没有任何意义:

indoor_patient ins=new indoor_patient();    
String webUrl = "http://10.0.3.2:8084/data_web/indoorPatient.jsp?sr_no="
        + ins.sr_no.getText().toString()

相反,您可以在webUrl方法中找出onClick()的内容,并将完整的网址作为参数传递给AsyncTask

submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String webUrl = "http://10.0.3.2:8084/data_web/indoorPatient.jsp?sr_no="
                + ins.sr_no.getText().toString()
                + ""
                + ins.patient_name.getText().toString()
                + ""
                + ins.consultant_name.getText().toString()
                + ""
                + ins.ref_dr.getText().toString()
                + ""
                + ins.department.getText().toString()
                + ""
                + ins.rel_name.getText().toString()
                + ""
                + ins.rel_no.getText().toString()
                + ""
                + ins.arr_date.getText().toString()
                + ""
                + ins.arr_time.getText().toString() + "&submit=Submit";
        new dbwork().execute(webUrl);
    }
});

然后将AsyncTask更改为以下内容:

public class dbwork extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        String webUrl = params[0];
        // TODO Auto-generated method stub
        StringBuffer strbuffer=new StringBuffer("");
        // ...