Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6895749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:56:56+00:00 2026-05-27T06:56:56+00:00

I get a nullpointerexception when I do this: EditText[] answers; //At the oncreate method

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:56:56+00:00Added an answer on May 27, 2026 at 6:56 am
    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]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I get a NullPointerException in a call like this: someObject.getSomething().getSomethingElse(). getAnotherThing().getYetAnotherObject().getValue(); I get
In this opportunity I try to do a simple activity that get a EditText
I get a NullPointerException when I run this. It occurs on the line listings[i].input();
I was trying to solve this problem in UVa, but I get a NullPointerException
When I run my program I get this error nullPointerException: null. import model.*; import
It doesn't seem to be working right now. I get a java.lang.NullPointerException I have
I get this error: Can't locate Foo.pm in @INC Is there an easier way
I want to show custom dialog with a spinner. Strangely enough, I get NullPointerException
I get a NullPointerException at a line on which just a simple null check
I get a NullPointerException when a url passed in webview in an activity (the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.