-2

I am new at android. I am trying to take data from user and toast to screen... but I am facing null pointer exception... i am trying from long time to solve ...plzzz help me here is my code:

 public class MainActivity extends ActionBarActivity {

    private EditText etName;
    private EditText etEmail;
    private EditText etPhone;
    private EditText etDesignation;
    DatabaseHelper dbHelper;

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

         findViewById(R.id.etName);
         findViewById(R.id.etEmail);
         findViewById(R.id.etPhone);
         findViewById(R.id.etDesignation);
        //dbHelper = new DatabaseHelper(this);
    }

    public void save(View v) {
        String name = etName.getText().toString();
        String email = etEmail.getText().toString();
        String phone = etPhone.getText().toString();
        String designation = etDesignation.getText().toString();

        Employee employee = new Employee(name, email, phone, designation);
        Toast.makeText(getApplicationContext(), employee.toString(),
                Toast.LENGTH_LONG).show();



        }
amit singh
  • 1,407
  • 2
  • 16
  • 25
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Mark Rotteveel May 10 '15 at 07:56

5 Answers5

1

I suggest you to check the basics first

replace this part:

etName = (EditText) findViewById(R.id.etName);
    etEmail = (EditText) findViewById(R.id.etEmail);
    etPhone = (EditText) findViewById(R.id.etPhone);
    etDesignation = (EditText) findViewById(R.id.etDesignation);
    dbHelper = new DatabaseHelper(this);
ruhul_inonity
  • 59
  • 1
  • 5
0

None your your fields are initialized. Doing just a findViewById(R.id.etName); is not enough as global members will be holding null values giving NullPointerException. Hence assignment is important

private EditText etName;         // default - null
private EditText etEmail;        // default - null
private EditText etPhone;        // default - null
private EditText etDesignation;  // default - null
DatabaseHelper dbHelper;         // default - null

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

     // changes here, link layout fields to objects

     etName = (EditText) findViewById(R.id.etName);
     etEmail = (EditText)findViewById(R.id.etEmail);
     etPhone = (EditText)findViewById(R.id.etPhone);
     etDesignation = (EditText)findViewById(R.id.etDesignation);
    //dbHelper = new DatabaseHelper(this);
}

public void save(View v) {
    String name = etName.getText().toString();
    String email = etEmail.getText().toString();
    String phone = etPhone.getText().toString();
    String designation = etDesignation.getText().toString();

    Employee employee = new Employee(name, email, phone, designation);
    Toast.makeText(getApplicationContext(), employee.toString(),
            Toast.LENGTH_LONG).show();



    }
sujithvm
  • 2,351
  • 3
  • 15
  • 16
0

Try this

private EditText etName;
private EditText etEmail;
private EditText etPhone;
private EditText etDesignation;
DatabaseHelper dbHelper;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = this;
    etName = (EditText)findViewById(R.id.etName);
    etEmail = (EditText) findViewById(R.id.etEmail);
    etPhone = (EditText) findViewById(R.id.etPhone);
    etDesignation = (EditText) findViewById(R.id.etDesignation);
    //dbHelper = new DatabaseHelper(this);
}

public void save(View v) {
    String name = etName.getText().toString();
    String email = etEmail.getText().toString();
    String phone = etPhone.getText().toString();
    String designation = etDesignation.getText().toString();

    Employee employee = new Employee(name, email, phone, designation);
    Toast.makeText(context, employee.toString(),
            Toast.LENGTH_LONG).show();



    }
Jamil
  • 5,457
  • 4
  • 26
  • 29
0

In onCreate

 etName = (EditText)findViewById(R.id.etName);
 etEmail = (EditText)findViewById(R.id.etEmail);
 etPhone = (EditText)findViewById(R.id.etPhone);
 etDesignation = (EditText)findViewById(R.id.etDesignation);

You forgot to initialize the views and you called getText() on null

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

It appears your fields are not being instantiated, this is usually the indication of a null pointer exception.Have you tried changing

 findViewById(R.id.etName);
 findViewById(R.id.etEmail);
 findViewById(R.id.etPhone);
 findViewById(R.id.etDesignation);

to

 this.eName = (EditText)findViewById(R.id.etName);
 this.etEmail = (EditText)findViewById(R.id.etEmail);
 this.etPhone = (EditText)findViewById(R.id.etPhone);
 this.etDesignation = (EditText)findViewById(R.id.etDesignation);  

? Also are you certain that all if the R.id. are valid references?

dinsomniac
  • 56
  • 2