0

I'm struggling with an assignment of mine and I can't figure out how to add another element to my list.

import java.util.ArrayList; public class Ballot { private ArrayList<Candidate> ballot; private String officeName; public Ballot(String officeName) { this.officeName = officeName; ArrayList<Candidate> ballot = new ArrayList<Candidate>(); } public String getOfficeName() { return officeName; } public void addCandidate(Candidate c) { ballot.add(c); } public ArrayList<Candidate> getCandidates() { return ballot; } public static void main(String[] args) { Ballot b = new Ballot("Election"); b.addCandidate(new Candidate("Sarah", "President")); System.out.println(b); } } 

When I try to run the document, it throws a NullPointerException. What am I doing wrong?

1
  • 5
    You don't initialise the ballot ArrayList field. You declare a local ballot variable in your constructor and you initialise that. Commented Mar 3, 2020 at 19:50

4 Answers 4

3

The constructor initializes a local variable named ballot that hides the data member with the same name. Then, when you try to add to it, it fails with a NullPointerException, since it was never initialized. If you initialize it you should be OK:

public Ballot(String officeName) { this.officeName = officeName; ballot = new ArrayList<Candidate>(); // Here! } 
Sign up to request clarification or add additional context in comments.

Comments

1

You're not initializing your list of candidates properly in the Ballot constructor. You need to do:

this.ballot = new ArrayList<Candidate>(); 

Right now you're just creating a local variable named ballot in the constructor which shadows the actual class field. Since it has never been initialized, you end up getting a NullPointerException when you eventually try to add an element to it.

Also, as a best practice, use interfaces instead of the concrete type. This makes it easy to change implementations later. So instead of defining the field as private ArrayList<Candidate> ballot;, define it as private List<Candidate> ballot;.

Comments

1

As simple that you are not using this object. You are never initiliazing your object

Correct way

public Ballot(String officeName) { this.officeName = officeName; this.ballot = new ArrayList<Candidate>(); } 

Comments

1

You're overriding your class variable with a local variable of the same name. Either initialize the list directly

private List<Candidate> ballot = new Arraylist<>(); 

or initialize it in the constructor with

ballot = new ArrayList<>(); 

FYI: You shouldn't assign implementation classes for your local variables and return values if you can help it. "ballot" should just be the List interface as should the getter. That way if you ever want to change the implementation, you don't have to change everything. It could be an ArrayList, LinkedList, Stack, Vector, etc and it won't matter because they're all using the List interface.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.