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 8736201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:15:07+00:00 2026-06-13T10:15:07+00:00

Trying to create an object and adding it to an ArrayList. Code looks similar

  • 0

Trying to create an object and adding it to an ArrayList. Code looks similar to this:

class B{
    public ArrayList<Answer> answerList = new ArrayList<Answer>();

    questionList.add(new EssayQuestion());
    answerList.add(new StringAnswer());
}

Exception in thread "main" java.lang.NullPointerException
at main.Test.createNewQuestion(Test.java:31)
at main.Survey.<init>(Survey.java:21)
at main.Test.<init>(Test.java:9)
at main.MainDriver.main(MainDriver.java:35)

And in the debugger, right before the crash I get this:
Thread.dispatchUncaughtException(Throwable) line: not available

It’s crashing at
answerList.add(new StringAnswer());
and I have no idea why.

If it’s relevant, questionList is initialized in the superclass of where this code chunk is. I am accessing it because it is protected. answerList is created locally.

The constructor of StringAnswer asks the user for a String and reads it via Scanner. EssayQuestion()’s constructor is very similar.

Any ideas?

Edit: Here is some more code as requested. Yes, answerList is showing as null after initialized and before anything gets added. Is that the problem? Why is it happening? Once again, questionList is declared and initialized in the parent class of this, so it’s okay not to re-declare or initialize it right?

public class Test extends Survey
 {
  public ArrayList<Answer> answerList;

  public Test()
  {
            answerList = new ArrayList<Answer>();
    System.out.print("How many questions? ");
    int numQuestions = kb.nextInt();
    for (int i = 0; i < numQuestions; i++)
    {
        displayMenu();
        createNewQuestion();
    }
   }

   public void createNewQuestion()
   {
    int input = -1;
    do{
        System.out.print("Question Type: ");
        input = kb.nextInt();
        System.out.println();

        switch (input)
        {
            case 1:     questionList.add(new EssayQuestion());
                        answerList.add(new StringAnswer());
                        break;

And here is StringAnswer:

public class StringAnswer extends Answer
{
String text;

public StringAnswer()
{
    setAnswer();

}

public StringAnswer(String text)
{
    this.text = text;
}

@Override
public void display()
{
    System.out.println(text);       
}

public String getAnswer()
{
    return text;
}

@Override
public void setAnswer()
{
    System.out.print("Enter Answer: ");
    setAnswer(kb.nextLine());
    System.out.println("");
}

public void setAnswer(String text)
{
    this.text = text;
}

}
  • 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-06-13T10:15:08+00:00Added an answer on June 13, 2026 at 10:15 am

    From your comments to @D_Jones’s answer, I assume you’ve something like this:

    public class Survey{
        protected ArrayList<EssayQuestion> questionList;
    
        //your parent class methods;
    }
    

    And after that you’re accessing questionList and trying to add elements to it from your Test class. The problem over here is this that you’ve not instantiated questionList. This means that questionList is still null and when you’re trying to add elements to questionList, you’re thrown a NullPointerException by the JVM.

    There’re a couple of solutions to it:

    Solution 1: Instantiate questionList in the base class constructor. Something like this:

    public class Survey{
        protected ArrayList<EssayQuestion> questionList;
    
        public Survey(){
            questionList = new ArrayList<EssayQuestion>();
        }
        //your parent class methods;
    }
    

    Now you can access it in your child class without an exception.

    Solution 2: Instantiate it within the constructor of your child class or wherever you’re trying to use it for the first time.

    public class Survey{
        protected ArrayList<EssayQuestion> questionList;
    
        //your parent class methods;
    }
    
    
    public class Test extends Survey
    {
      public ArrayList<Answer> answerList = new ArrayList<Answer>();
    
      public Test(){
        questionList = new ArrayList<EssayQuestion>(); //Option 1
      }
    
      public void createNewQuestion(){
        //If you've not initialized it in the constructor, try instantiating it in on first use
        if(questionList == null){ //Option 2
            questionList = new ArrayList<EssayQuestion>();
        }
    
        //now you can access the questionList without an exception.
        //this is because you've a valid instance of of ArrayList<EssayQuestion> to which the
        //variable questionList points to
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create COM -object on server. code is: var myGuid = new Guid(530A1815-820C-11D3-BBB7-008048DE406A);
I'm trying to create a single 'query' NSString object that basically looks like this:
I am trying to create an object of Console class, but could not succeed.
How can I do inheritance with this example. I'm trying to create an object
I'm trying to create a new object of type T via its constructor when
I've got a Backbone.js/Rails app and I'm trying to create a new object through
I am trying create a xmldocument object by a different XML see the code
this.tModel.insertRow(rowCount,new Object[] {,,,}); this.table.setRowSelectionAllowed(true); this.table.changeSelection(0, 0, false, false); this.table.addKeyListener(new KeyListener() { @Override public void
I'm trying to create an object file which can be useable in any computer.
I'm trying to create a Database object for myself in .net. The constructor takes

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.