-5

I am trying to get values from a patient registration form into my database. But each time when I click on Submit button, application stops with following message:
Unfortunately. Application has stopped.

Logcat details are:

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

Logcat Details

My code for the same is as follows:

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);
    }
};  
}

And my database connectivity file is as follows:

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);
   }
}
Dhaval Simaria
  • 1,886
  • 3
  • 28
  • 36
  • 3
    It is clearly stating what the error is, you are trying to get the text of a not yet initialized object – SomeJavaGuy Apr 02 '15 at 13:38
  • Don´t do it like this...You are initializing the Activity indoor_patient inside the dbwork AsyncTask and You are executing the dbwork AsyncTask inside this activity....this will always lead You into big problems. You have to rework Your code for sure. Make an inner class for the Asynctask inside Your activity and then get the editText strings – Opiatefuchs Apr 02 '15 at 13:47

2 Answers2

1

When you execute indoor_patient ins=new indoor_patient();, it doesn't mean that you call onCreate(). So all EditText components are null.

PageNotFound
  • 2,320
  • 2
  • 23
  • 34
1

You can't just instantiate your Activity and access the TextView members on it. This doesn't make any sense:

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()

Instead you could figure out what your webUrl is in your onClick() method and pass in the complete URL as a parameter to the 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);
    }
});

Then change your AsyncTask to something like this:

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("");
        // ...
ci_
  • 8,594
  • 10
  • 39
  • 63