1

i just don't understand why I have a nullpointerexception with this:

public class MainActivity extends AppCompatActivity {

private EditText field;

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

    EditText field = (EditText) findViewById(R.id.field);
    Button plus = (Button) findViewById(R.id.plus);
    Button moins = (Button) findViewById(R.id.moins);
    Button ctof = (Button) findViewById(R.id.ctof);

    plus.setOnClickListener(buttonclick);
    moins.setOnClickListener(buttonclick);
    ctof.setOnClickListener(buttonclick);

}


private View.OnClickListener buttonclick = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.plus:
                //TODO
                Toast.makeText(MainActivity.this, field.getText().toString(), Toast.LENGTH_LONG).show();
                break;
        }
    }
};


}

I don't understand my mistake, it's simple and I don't know where is the issue. I can't get my field and print the result.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Userlambda
  • 85
  • 1
  • 11

2 Answers2

3
private EditText field;

You defined your edit text

and you've done it again

EditText field = (EditText) findViewById(R.id.field);
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85
1

it's because you have declared your EditText twice. Remove "EditText" From "EditText field = ......" the problem will be solved

Matin Gdz
  • 217
  • 2
  • 13