-1

when I'm trying to use sentMessages method in testClass, I got a NullpointerException error in the line receiverUser.privateMessageList.add(privateMessage); even I initialized the "privateMessageList" list. How can I deal with this problem?

public class User {

    private String userName;
    private String password;
    private String firstName;
    private String lastName;
    private String userType;
    int topicCount = 0;
    final int maxTopicNumber = 5;

    ArrayList<PrivateMessage> privateMessageList;

    public User(String userName, String password, String firstName, String lastName, String user_type) {
        this.userName = userName;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.userType = "USER";

    }
    //private message section
    public PrivateMessage sentMessage(String newMessage, User receiverUser) {
        privateMessageList = new ArrayList<>();
        PrivateMessage privateMessage = new PrivateMessage(this.userName, newMessage);
        receiverUser.privateMessageList.add(privateMessage);
        System.out.println("Message sent successfully!");
        return privateMessage;
    }

    public void ListMessages(User receiverUser) {
        System.out.println("Inbox of " + receiverUser);
        for (int i = 0; i < receiverUser.privateMessageList.size(); i++) {
            System.out.println(receiverUser.privateMessageList.get(i));
            System.out.println("Sent by: " + this.userName);
        }
    }
Guy
  • 46,488
  • 10
  • 44
  • 88

3 Answers3

0

If you create a User and not call sentMessage method, your privateMessageList will not be created, thus executing any list method will cause NPE.

So initialize privateMessageList inside of the constructor or inline.

List<PrivateMessage> privateMessageList =new ArrayList<>();

Your sendMessage method will look like this:

public PrivateMessage sentMessage(String newMessage, User receiverUser) {
    PrivateMessage privateMessage = new PrivateMessage(this.userName, newMessage);
    receiverUser.privateMessageList.add(privateMessage); // list is already created
    System.out.println("Message sent successfully!");
    return privateMessage;
}

Secondly, if you are creating privateMessageList inside of your sentMessage method, than your list will always have single element. Because another method call will remove previous list. It seems to be a bug.

Beri
  • 11,470
  • 4
  • 35
  • 57
  • It really worked, thanks Sir! – siyahdemir Dec 29 '19 at 13:31
  • Glad to help/. but remember, that all messages added to privateMessageList will not be removed by the GC, leading to a memory problem. So you need a point in time when you wil clean this list. – Beri Dec 29 '19 at 13:36
0

You're NOT initializing privateMessageList.

Initialize it in the constructor, so that it's always intialized.

Your sentMessage() method initializes the privateMessageList of this. But it doesn't initialize (and shouldn't) the list of receiverUser. Each user has its own list. Initializing one user's list won't initialize another user's list.

Initializing it in the constructor makes everything simpler and safer: there is no good reason for a user to ever have a null list.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • _"there is no reason for a user to ever have a null list. "_ -- lazy initialization to save memory could be a reason. – frogatto Dec 29 '19 at 12:39
  • OK. That would be a reason, but a bad one. It's premature optimization, especially when you're a newbie learning the basics. – JB Nizet Dec 29 '19 at 12:41
  • Thanks so much, I'm a newbie so I struggle so much while coding, but solved as I initialize the "privateMessageList" in constructor. Thanks again. – siyahdemir Dec 29 '19 at 13:28
0

replace this receiverUser.privateMessageList.add(privateMessage); to privateMessageList.add(privateMessage);

create ArrayList object inside instance initializer block or inside constructor.

Sarjit
  • 799
  • 7
  • 9