0

I'm getting an unusual ,

NullPointerException: Exception in thread "main" java.lang.NullPointerException.

In it's own TeamClass I have:

  public DSALinkedList<String> players;
  private String name, contactName, ContactAddr, contactEmail, parent;

public Team(String inName, String inConName, String inConAddr, String inConEmail, String inParent)
{     


        name = inName;
        DSALinkedList<String> players = new DSALinkedList<String>(); //Constructs an empty players list using Strings as the value.
        parent = inParent;
        contactName = inConName;
        ContactAddr = inConAddr;
        contactEmail = inConEmail;

}

//With the method

public void addPlayer(String inPlayer)
{
    players.insertFirst(inPlayer);        
}

Then in my Main class i'm constructing a team from file, which calls this method:

public static Team constructTeam(String inLine)
    {
   String[] lineArray = new String[16];
   lineArray = inLine.split(",");  

    int i;
    String name, contact, email, address, parent;
    name = lineArray[1];
    parent = lineArray[2];
    contact = lineArray[3];
    email = lineArray[4];
    address = lineArray[5];

    Team team = new Team(name, contact, address, email, parent);
    System.out.println(team.getName());

//This is a sanity check, this prints the Team name correctly which to me shows that 'team' is not null.
//Add players(string) to a player List. "PLAYER: FirstName LastName"

    for(i=6; i<15; i++) //First player in lineArray to potentially the last.
    {
        if(!lineArray[i].equals("")) 
        {
            System.out.println("Test:"+ lineArray[i]);
            String player = lineArray[i];
            team.addPlayer(player); //ERROR OCCURS HERE
        }
    }

  return team;
}
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98

2 Answers2

0

You are re declaring your class variable - DSALinkedList<String> players on this line in constructor , DSALinkedList<String> players = new DSALinkedList<String>(); ,

It should be only - players = new DSALinkedList<String>();

So in this below method, players is NULL since its class variable.

public void addPlayer(String inPlayer) {
    players.insertFirst(inPlayer);        
}
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
  • Thanks so much mate! – Riley Bennett May 17 '17 at 04:14
  • I have a follow up question, Players come under teams, teams come under clubs, clubs come under state, and state comes under national. I've constructed all these classes similarly: DSALinkedList states = new DSALinkedList(); etc which caused the error above. How ever this has not made any errors upon running. Should this code be changed also? – Riley Bennett May 17 '17 at 08:33
  • If current question is answered, you need to ask a new question for any confusion. mixing of questions isn't good as you will get new audience & fresh perspectives for your new question.You can refer this question in new question. – Sabir Khan May 17 '17 at 08:36
  • For the case that you asked this question - it was a simple Java mistake. Class level field`players` was not properly initialized. What you initialized in constructor was a local reference to that constructor method and the reference you tried to use in method `addPlayer` was another uninitialized class level field so the error. Both are two distinct references since you re declared reference in constructor with same name. Error has nothing to do with class hierarchy or how you compose your classes. – Sabir Khan May 17 '17 at 09:34
0

Player will be used by the following way :

public DSALinkedList<String> players;
  private String name, contactName, ContactAddr, contactEmail, parent;

public Team(String inName, String inConName, String inConAddr, String inConEmail, String inParent)
{     


        name = inName;
        players = new DSALinkedList<String>(); //Constructs an empty players list using Strings as the value.
        parent = inParent;
        contactName = inConName;
        ContactAddr = inConAddr;
        contactEmail = inConEmail;

}
Christopher Marlowe
  • 2,098
  • 6
  • 38
  • 68