1
import java.util.ArrayList;

public class FriendList {

    private ArrayList<String> friendList;

    public FriendList() {
        ArrayList<String> friendList = new ArrayList<String>();
    }

    public void addFriend(String friend) {
        friendList.add(friend);
    }

    public String getFriends() {
        return friendList.toString();
    }
}

I've tried a few things but can't seem to manage to add strings to the array list. Any ideas why this might be?

Eran
  • 387,369
  • 54
  • 702
  • 768
Zetland
  • 569
  • 2
  • 11
  • 25

5 Answers5

10

Your constructor initializes a local ArrayList variable, so your friendList member is never initialized. When you try to access the uninitialized member in other methods, you get a NullPointerException.

Change

public FriendList() {
    ArrayList<String> friendList = new ArrayList<String>();
}

to

public FriendList() {
    friendList = new ArrayList<String>();
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    and even better: friendList = new ArrayList<>() if you are on Java 1.7 – David Soroko Oct 30 '14 at 14:57
  • @DavidSoroko why do you consider this better? Is it only because you don't have to type 6 letters more? – spoko Oct 30 '14 at 15:02
  • And one of them is capital ;-). Have a look at this [link]http://stackoverflow.com/questions/4166966/what-is-the-point-of-the-diamond-operator-in-java-7 One could change `private ArrayList friendList` to `private ArrayList friendList` in one place only and everything would still compile. – David Soroko Oct 30 '14 at 15:19
  • @DavidSoroko after I saw this example, your answer makes sense. I was afraid what would happen if you changed `private ArrayList friendList` to `private ArrayList` and tried to add `String` there, but according to this example compiler shouldn't let it go. I simply thought `ArrayList()` and `ArrayList<>()` worked in the same way. And with this raw type, adding `String` to `ArrayList` would make nice runtime crash, that wouldn't be prevented by compiler. Thanks ;) – spoko Oct 30 '14 at 15:50
7

You're hiding friendList field.

Use instead:

public FriendList() {
    friendList = new ArrayList<String>();
}

In your constructor, you instantiate a local variable friendList.

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
2

You are not initializing the instace member but creating and setting a list local to the constructor. It dies outside the scope of the constructor.

So basically you are trying to add strings to a list that has not been created yet.

One other thing to note from this post is Java sets all it uninitialized objects/instances to null Hence the Exception . Change this

public FriendList() {
    ArrayList<String> friendList = new ArrayList<String>();
}

to

public FriendList() {
    friendList = new ArrayList<String>();
}
Chiseled
  • 2,280
  • 8
  • 33
  • 59
0

The friendsList list that you create inside the constructor is a local variable that hides the global friendsList. So, to solve your problem right now, just replace the line

   ArrayList<String> friendList = new ArrayList<String>();

with

   this.friendList = new ArrayList<String>();

After that, you have to take a look at variable scope principles. The most of them are common for all programming languages

karvoynistas
  • 1,295
  • 1
  • 14
  • 30
0

OR you can change like this

public FriendList() {
    ArrayList<String> friendList = new ArrayList<String>();
}
to
public FriendList() {
    this.friendList = new ArrayList<String>();
}
Frank
  • 1
  • 4