0

I'm trying to create a class (Team) but I'm getting a nullpointer error This is where it's called

Team[] Teams = new Team[6];  

private void AddTeamButtonMouseClicked(java.awt.event.MouseEvent evt) {                                           
        String InputTeamName = TeamNameInput.getText();

        switch(TeamCounter)
        {
            case 0:
                Teams[TeamCounter].Name = InputTeamName;
                Team_1.setText(Teams[TeamCounter].Name);
                TeamCounter++;
                break;
}

Team Class

public class Team 
{
    String Name;
    Player[] Players = new Player[5];
}
  • 1
    You aren't actually creating any instances of `Team`. – GriffeyDog Mar 13 '20 at 15:40
  • 3
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – jhamon Mar 13 '20 at 15:40
  • On what line is the exception? – ControlAltDel Mar 13 '20 at 15:41
  • 1
    You need to initialize each element of the array as they are `Object`s (as opposed to primitives), so their default value is `null`. – Mena Mar 13 '20 at 15:41
  • What is Player ? – Rans Mar 13 '20 at 15:42
  • 1
    Please, please, please, never name a variable with an initial capital letter. – Pino Mar 13 '20 at 15:43
  • Besides following the Java style and capitalization guidelines, the code itself is bad. Please use getters and setters. Code like `Teams[TeamCounter].Name = InputTeamName;` is always bad code. – markspace Mar 13 '20 at 15:45

0 Answers0