-3

Trying to add the object member to the ArrayList loggedInList when logging into the Website class.

public class Website
{
// The name of the website.
private String name;
// The number of hits on the website.
public int hits;
// The amount of money taken at the checkout.
private double salesTotal;
// 
private ArrayList<Member> loggedInList;

Is the code for my Website class.

    public Website(String newName)
{
    // Intialises the name of the website.
    name = newName;
    // Intialises the number of hits on the site.
    hits = 0;
    // Intialises the amount of money taken at the checkout.
    salesTotal = 0;
    // 
    loggedInList = new ArrayList<Member>();


}

Is the constructor for the Website class.

    public void memberLogin(Member member)
{
    member.setLoginStatus(true);
    member.setWebsite(this);

    System.out.println(name + " welcomes member " + member.getMembershipNumber() + "," + " you are now logged in.");

    member.setWebsite(this);

    hits +=1;

    loggedInList.add(member);
}

Is the method which SHOULD add the current member into the ArrayList.

The error I get is a NullPointerException on the line:

loggedInList.add(member);

I honestly have no clue why.

jd123
  • 1
  • 2
    construct the list before using it – Ely Dec 11 '17 at 19:44
  • ^ it looks like they are doing that from the code pasted ... – Amir Afghani Dec 11 '17 at 19:46
  • Either list is null or member is null – Antoniossss Dec 11 '17 at 19:46
  • Could you explain what you mean? I'm very new to Java and BlueJ etc so I'm at really basic level – jd123 Dec 11 '17 at 19:46
  • This is a strange post because member wouldn't be null if the exception is happening on the line the OP describes... – Amir Afghani Dec 11 '17 at 19:47
  • 3
    Where else in your class do you do anything with `loggedInList`? Perhaps you're setting it to `null` sometime after construction? The code you've posted should work. Can you [edit] your question to provide a [mcve]? Perhaps you're not calling the code you think you are... – azurefrog Dec 11 '17 at 19:48
  • 1
    Guys, it's perfectly valid to add `null` to an `ArrayList`, so that couldn't be it anyway. `loggedInList` must be null. – D M Dec 11 '17 at 19:49
  • `loggedInList` must be getting reinstantiated post-construction somewhere else in `Website` – jseashell Dec 11 '17 at 19:51
  • Also @jd123, you should have your class member `loggedInList` be a `List` rather than an `ArrayList`. This is called "programming to the interface" and is probably a little more advanced than you're ready for. But make note of it for the future to look into. – jseashell Dec 11 '17 at 19:54
  • 2
    I decided to vote this question as duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/q/218384) because despite that we can't reproduce this problem, solution is still same as in duplicate: ensure that `loggedInList` is initialized properly before using `loggedInList.add(..)`. This can be done at declaration level `private ArrayList loggedInList = new ArrayList<>();` which will ensure that this list will always be initialized, unless OP in different place will explicitly set it to null. – Pshemo Dec 11 '17 at 20:01
  • @azurefrog I don't use loggedInList anywhere else, its new. I created it along with the ArrayList – jd123 Dec 11 '17 at 21:32

1 Answers1

0

I believe you are not using the correct constructor. You may have a default no arg constructor in your code.

In any case, you should do it similar to the code below.

   Website website = new Website ("someString value");
website.memberLogin (objectCreatedMember);
GemSky
  • 1
  • 4
  • 1
    [Your recent deleted post](https://stackoverflow.com/questions/59084027/problem-using-firebase-db-connect-to-spring-project) is not an acceptable use of Stack Overflow. If you want to experiment, there is a sandbox page on meta: https://meta.stackexchange.com/questions/3122/formatting-sandbox – tripleee Nov 28 '19 at 10:38