I get a nullpointerexception when I do this:
EditText[] answers;
//At the oncreate method I do this:this is set after the setContent() method.
answers=new EditText[6];
answers[0]=(EditText) findViewById(R.id.editText1);
answers[1]=(EditText) findViewById(R.id.editText2);
answers[2]=(EditText) findViewById(R.id.editText3);
answers[3]=(EditText) findViewById(R.id.editText4);
answers[4]=(EditText) findViewById(R.id.editText5);
answers[5]=(EditText) findViewById(R.id.editText6);
In the onClickListener of the button I put this:
nextQuestion_btn.setOnClickListener(new View.OnClickListener()
{
for(EditText item : answers) {
if(item.getText().toString().equals("")) { //nuller Exception here
editTextIsEmpty=true;
break;
}
}
}
why do I get a nullpointerexception… in the if statement?
UPDATE;
I put all the EditText into an array. All the code is inside the onCreate method
This is what I get:
11-29 16:11:24.076: E/AndroidRuntime(616): FATAL EXCEPTION: main
11-29 16:11:24.076: E/AndroidRuntime(616): java.lang.NullPointerException
11-29 16:11:24.076: E/AndroidRuntime(616): at com.NewOrder.SetTest$1.onClick(SetTest.java:95)
11-29 16:11:24.076: E/AndroidRuntime(616): at android.view.View.performClick(View.java:2485)
11-29 16:11:24.076: E/AndroidRuntime(616): at android.view.View$PerformClick.run(View.java:9080)
11-29 16:11:24.076: E/AndroidRuntime(616): at android.os.Handler.handleCallback(Handler.java:587)
11-29 16:11:24.076: E/AndroidRuntime(616): at android.os.Handler.dispatchMessage(Handler.java:92)
11-29 16:11:24.076: E/AndroidRuntime(616): at android.os.Looper.loop(Looper.java:123)
Should be:
Otherwise you aren’t ever adding anything to the
answersarray, as has been stated. It seems to me that you’re thinkinganswer1throughanswer6are going into youranswersarray, unless you have those declared elsewhere that you haven’t posted. All 6EditTextsin youranswersarray are currentlynull, which is causing your Exception.Also, something like this would be more concise: