Home » Android » java – Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

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

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have tried all possible cases but not solved , Need suggestions

public class WolfActivity extends ActionBarActivity {

   EditText fname_ele,lname_ele,email_ele,phone_ele,pwd_ele,fax_ele,addr_ele,addr1_ele,city_ele,zipcode_ele,facility_ele,state_ele;

    String fname,lname,email,phone,pwd,fax,addr,addr1,city,zipcode,facility,state;
    Button button;
    AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fname_ele=(EditText) findViewById(R.id.first_name);
        lname_ele=(EditText) findViewById(R.id.last_name);
        email_ele=(EditText) findViewById(R.id.email);
        button=(Button) findViewById(R.id.submit_button);
        phone_ele=(EditText) findViewById(R.id.phone);
        addr_ele=(EditText) findViewById(R.id.address);
        addr1_ele=(EditText) findViewById(R.id.address1);
        fax_ele=(EditText) findViewById(R.id.fax);
        facility_ele=(EditText) findViewById(R.id.facility);
        zipcode_ele=(EditText) findViewById(R.id.zip);
        city_ele=(EditText) findViewById(R.id.city);
        state_ele=(EditText) findViewById(R.id.state);

    /* Spinner dropdown = (Spinner)findViewById(R.id.state);
        String[] items = new String[]{"1", "2", "3" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
        dropdown.setAdapter(adapter);*/

        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fname=fname_ele.getText().toString();
            lname=lname_ele.getText().toString();
            email=email_ele.getText().toString();
            pwd=pwd_ele.getText().toString();
            phone=phone_ele.getText().toString();
            addr=addr_ele.getText().toString();
            addr1=addr1_ele.getText().toString();
            fax=fax_ele.getText().toString();
            facility=facility_ele.getText().toString();
            zipcode=zipcode_ele.getText().toString();
            city=city_ele.getText().toString();
            state=state_ele.getText().toString();

            Log.d("Email",email);
            if (!isValidEmail(email)) {

                Toast.makeText(WolfActivity.this, "Enter valid email", Toast.LENGTH_SHORT).show();
            } else
                new signmeup().execute();
        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private boolean isValidEmail(String email) {
    String EMAIL_PATTERN = "^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@"+"[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";
    Pattern pattern = Pattern.compile(EMAIL_PATTERN);
    Matcher matcher = pattern.matcher(email);
    return matcher.matches();
}

class signmeup extends AsyncTask<String, String, String> {

        private ProgressDialog pDialog;
        JSONParser jsonParser = new JSONParser();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(WolfActivity.this);
            pDialog.setMessage("Thanks for your Sign up...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);

        }

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("section","user"));
            params.add(new BasicNameValuePair("action","new"));
            params.add(new BasicNameValuePair("first_name",fname));
            params.add(new BasicNameValuePair("last_name",lname));
            params.add(new BasicNameValuePair("email",email));
            params.add(new BasicNameValuePair("phone",phone));
            params.add(new BasicNameValuePair("fax",fax));
            params.add(new BasicNameValuePair("address",addr));
            params.add(new BasicNameValuePair("address1",addr1));
            params.add(new BasicNameValuePair("facility",facility));
            params.add(new BasicNameValuePair("password",pwd));
            params.add(new BasicNameValuePair("zip",zipcode));
            params.add(new BasicNameValuePair("city",city));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest("http://eastendwebsolutions.com/wf/ws/", "GET", params);

            Log.d("First Name",fname);
            Log.d("Last Name",lname);
            Log.d("Email",email);
            // check log cat for response
            Log.d("Create Response", json.toString());

            // check for success tag
           try {
                String success = json.getString("success");

                if (email != null & success == "true") {

                    Toast.makeText(WolfActivity.this, "Youre Email Posted..",
                            Toast.LENGTH_SHORT).show();

                } else {

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @SuppressWarnings("deprecation")
        //@Override
        protected void onPostExecute(String file_url) {

            if(pDialog.isShowing()){
                pDialog.dismiss();
            }
            AlertDialog.Builder builder=new AlertDialog.Builder(WolfActivity.this);
            builder.setTitle("Info");
            builder.setMessage("You have been subscribed to the HHI Vacation Directory Mailing List. Check email to confirm.");
            builder.setIcon(R.drawable.app_icon);
            builder.create().show();

        }
    }
}
How to&Answers:

You have 12 EditText declared but you’ve initialized 11

You forgot to initialize pwd_ele

Answer:

add this line after setContentView:

pwd_ele=(EditText) findViewById(R.id.password);

Answer:

EditText fname_ele,lname_ele,email_ele,phone_ele,pwd_ele,fax_ele,addr_ele,
         addr1_ele,city_ele,zipcode_ele,facility_ele,state_ele;

Notice that you are lacking 1 EditText that you have declared above. You missed initializing the String pwe_ele in the onCreate method.

        fname_ele=(EditText) findViewById(R.id.first_name);
        lname_ele=(EditText) findViewById(R.id.last_name);
        email_ele=(EditText) findViewById(R.id.email);
        button=(Button) findViewById(R.id.submit_button);
        phone_ele=(EditText) findViewById(R.id.phone);
        addr_ele=(EditText) findViewById(R.id.address);
        addr1_ele=(EditText) findViewById(R.id.address1);
        fax_ele=(EditText) findViewById(R.id.fax);
        facility_ele=(EditText) findViewById(R.id.facility);
        zipcode_ele=(EditText) findViewById(R.id.zip);
        city_ele=(EditText) findViewById(R.id.city);
        state_ele=(EditText) findViewById(R.id.state);