0

I am using WAMP Server. And I am having a hard time resolving these errors.

I am trying to save data to mysql database using android. When I tried to save data I've got an Error. Here's my activity.

appointment_form

public class appointment_form extends Activity{

//Instance Field


private EditText txtfname;
private EditText txtaddress;
private Spinner spoffice;
private Spinner spservices;
private EditText editdate;
private EditText edittime;

//getting data from edittext and spinner

String fname = txtfname.getText().toString();
String address = txtaddress.getText().toString();
String office = spoffice.getSelectedItem().toString();
String services = spservices.getSelectedItem().toString();
String date = editdate.getText().toString();
String time = edittime.getText().toString();

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

    txtfname =(EditText) findViewById(R.id.fnametxt);
    txtaddress =(EditText) findViewById(R.id.editAddress);

    editdate =(EditText) findViewById(R.id.schedDate);
    edittime =(EditText) findViewById(R.id.schedTime);


    spoffice =(Spinner) findViewById(R.id.SPoffice);
    spservices =(Spinner) findViewById(R.id.SPservice);

}
    public void btnSave (View view){

    inserttoDatabase(fname,address,date,time,office,services);
}

private void inserttoDatabase(final String fname,final String address,final String date,final String time,final String office,final String services) {
    class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            String paramUsename = params[0];

            List<NameValuePair> nameValuePairs = new ArrayList<>();
            nameValuePairs.add(new BasicNameValuePair("fname", fname));
            nameValuePairs.add(new BasicNameValuePair("address", address));
            nameValuePairs.add(new BasicNameValuePair("date", date));
            nameValuePairs.add(new BasicNameValuePair("time", time));
            nameValuePairs.add(new BasicNameValuePair("office", office));
            nameValuePairs.add(new BasicNameValuePair("services", services));


            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://192.168.254.110/Android/insert.php");
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpClient.execute(httpPost);

                HttpEntity entity = response.getEntity();


            } catch (ClientProtocolException e) {

            } catch (IOException e) {

            }
            return "success";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
        }
    }
    SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
    sendPostReqAsyncTask.execute(fname, address, date, time, office, services);
}

}

here's the error: It tells that

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.xtia.rockygwapo.iccharter.appointment_form.(appointment_form.java:58)*

It pointed at the String fname = txtfname.getText().toString();

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.xtia.rockygwapo.iccharter, PID: 31930
              java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.xtia.rockygwapo.iccharter/com.xtia.rockygwapo.iccharter.appointment_form}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2477)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
                  at android.os.Handler.dispatchMessage(Handler.java:111)
                  at android.os.Looper.loop(Looper.java:207)
                  at android.app.ActivityThread.main(ActivityThread.java:5728)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                  at com.xtia.rockygwapo.iccharter.appointment_form.<init>(appointment_form.java:58)
                  at java.lang.Class.newInstance(Native Method)
                  at android.app.Instrumentation.newActivity(Instrumentation.java:1072)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2467)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                  at android.os.Looper.loop(Looper.java:207) 
                  at android.app.ActivityThread.main(ActivityThread.java:5728) 
                  at java.lang.reflect.Method.invoke(Native Method) 
Neil
  • 14,063
  • 3
  • 30
  • 51
Cristiako
  • 19
  • 1
  • 8
  • Possible duplicate of [Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference](https://stackoverflow.com/questions/29408161/attempt-to-invoke-virtual-method-android-text-editable-android-widget-edittext) – Vishal Yadav Nov 18 '17 at 17:45

1 Answers1

0

You are trying to access the view widget references before they have been assigned to the actual widgets.

I am assuming you have in your layout file a button that has the following attribute, and that you expect someone to fill out a form and then click that button, right?

android:onClick="btnSave"

You really should check the widget references before you call a method on them, but you at least should have a chance of things working if you move the code that try's to extract the info from them into btbSave method. By the time it is called, onCreate has been called and the references are pointing to actual widgets (assuming you got all the findViewById 's correct), and assuming the user filled out the form, the data should be ready to be extracted from the views.

So move this code into the btnSave(View v) method

public void btnSave (View view){

    //getting data from edittext and spinner

    String fname = txtfname.getText().toString();
    String address = txtaddress.getText().toString();
    String office = spoffice.getSelectedItem().toString();
    String services = spservices.getSelectedItem().toString();
    String date = editdate.getText().toString();
    String time = edittime.getText().toString();

    inserttoDatabase(fname,address,date,time,office,services);
} 
nPn
  • 16,254
  • 9
  • 35
  • 58