BlackFire27
BlackFire27

Reputation: 1550

Nullpointerexception when checking EditText value

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)

Upvotes: 1

Views: 414

Answers (4)

Codeman
Codeman

Reputation: 12375

You might try asking a question explicitly, it will engender more helpful answers. :)

Two things. You need to make sure you are doing your answer1=(EditText) findViewById(R.id.editText1); calls before your item.getText().toString().equals("") calls, or you will always get a nullref error.

The other problem is that you are never actually assigning anything to the answers array. You need to put each of these EditTexts into the array before you can use it, or it will always be empty.

Try this:

answers = new EditText[6]();
answers[0] = answer0;
answers[1] = answer1;
...
answers[5] = answer5;

The code should work when you do that.

EDIT to reflect changes in question:

Put your assignment code (answers[0] = (EditText) findViewById(R.id.editText1);) inside onResume(), not in onCreate(). The EditTexts have not been given ids at the point, and will give nullrefs.

Upvotes: 2

Kevin Coppock
Kevin Coppock

Reputation: 134664

answers = new EditText[7];
answer1 = (EditText) findViewById(R.id.editText1);
answer2 = (EditText) findViewById(R.id.editText2);
answer3 = (EditText) findViewById(R.id.editText3);
answer4 = (EditText) findViewById(R.id.editText4);
answer5 = (EditText) findViewById(R.id.editText5);
answer6 = (EditText) findViewById(R.id.editText6);

Should be:

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);

Otherwise you aren't ever adding anything to the answers array, as has been stated. It seems to me that you're thinking answer1 through answer6 are going into your answers array, unless you have those declared elsewhere that you haven't posted. All 6 EditTexts in your answers array are currently null, which is causing your Exception.

Also, something like this would be more concise:

int[] editTexts = { R.id.editText1, R.id.editText2, R.id.editText3, 
    R.id.editText4, R.id.editText5, R.id.editText6 };

EditText[] answers = new EditText[editTexts.length];

for(int i = 0; i < answers.length; i++) {
    answers[i] = (EditText)findViewById(editTexts[i]);
}

Upvotes: 3

Marmoy
Marmoy

Reputation: 8079

You're missing the onclick method of the onclicklistener in the code you provided

Upvotes: 0

Gyuri Majercsik
Gyuri Majercsik

Reputation: 2253

Make sure those EditText ids are from the current activity's layout, and findViewById() method calls are after setContentView() method call.

Upvotes: 1

Related Questions