0

Sorry, maybe it is a beginner problem. This is a part of my MainActivity.java:

public class MainActivity extends Activity{
...
        EditText et101 = (EditText) findViewById(R.id.editText101);
...
        public myOwnClass(String my_id){
                et000.setText(my_id)
}

why do I get an NPE when I use et101 in myOwnClass? This is the error:

Caused by: java.lang.NullPointerException

The error is in that line, where EditText et101 = (EditText) findViewById(R.id.editText101); is

Michael
  • 57,169
  • 9
  • 80
  • 125
noobee
  • 373
  • 1
  • 4
  • 10
  • 1
    Do you have an EditText with "*editText101*" as id in your xml file? – Rami Jan 10 '16 at 22:02
  • yes I do have the editText101 in my xml-file. as I said before, the error occurs at the beginning of the java-file: `EditText et101 = (EditText) findViewById(R.id.editText101);` maybe there is the problem?? there is no duplicate :( – noobee Jan 10 '16 at 22:19
  • Where you defined et000 in your MainActivity ? – Yatish Jan 11 '16 at 05:57

1 Answers1

0

You can declare your EditText as a class level variable, and initialize it in onCreate

Something like this

public class MainActivity extends Activity {

    EditText et101;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.yourActivity);

    et101 = (EditText) findViewById(R.id.editText101);

    ...
    }

    public void myOwnClass(String my_id){
        et101.setText(my_id)
    }
}

This guide in the Android Documentation might be a good reference for you

Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55
  • but my function isn't in the oncreate.So it does'n make sense to do this in the oncreate, or am I wrong. My function is in the `public class MainActivity extends Activity {...}` – noobee Jan 10 '16 at 22:25
  • Right, but the first thing that happens when an activity starts is `onCreate`. You were trying to call `findViewById` outside of any function, which causes the NPE. It is a good practice to initialize all of your views in `onCreate`, so you can access them from wherever you want to later on – Andrew Brooke Jan 10 '16 at 22:36
  • By the way, is `myOwnClass` supposed to be a method? You need to specify a return type, or if it doesn't return anything, `void`, like this `public void myOwnClass(String my_id) { et101.setText(my_id); }` – Andrew Brooke Jan 10 '16 at 22:37
  • ok, ich done it. all the `findViewByID` are in the `onCreate` an I declare everything at the beginning of `MainActivity extends Activity`. But that wont help. Now the NPE is on `btnSave11.setOnClickListener(new OnClickListener()`, not on the EditText. But the button is declared and also `findViewById` and the button is in the xml. Whats the error now ? :`( btw: yes the return type is void. i just forgot to write it here – noobee Jan 11 '16 at 21:14
  • Could you update your original question to have all of your new code? – Andrew Brooke Jan 11 '16 at 21:15
  • my code is like @Andrew Brooke shows it. instead of the NPE at EditText, now the NPE is at `btnSave11.setOnClickListener(new OnClickListener() {...` but it is all declared – noobee Jan 11 '16 at 21:41