-1

Running into trouble with the output of my code (listed below, separate files and all) where I can execute the correct action up until I need to execute one directly after another. It's spitting out the Exception in thread "main" java.lang.NullPointerException error along with at which lines it is incorrect, however I don't know how to fix it as of now. (This is done in Eclipse compiler)


public class Assignment4
 {
  public static void main (String[] args)
   {
       // local variables, can be accessed anywhere from the main method
       char input1 = 'Z';
       //String inputInfo= "";
       String name,  firstName, lastName,  city ;
       int years = 0;
       String line = new String();

       // instantiate a Team object
       Team suns = null;

       printMenu();

       //Create a Scanner object to read user input
       Scanner scan = new Scanner(System.in);

       do  // will ask for user input
        {
            System.out.println("What action would you like to perform?");
            line = scan.nextLine();

            if (line.length() == 1)
            {
                input1 = line.charAt(0);
                input1 = Character.toUpperCase(input1);

                // matches one of the case statement
                switch (input1)
                {
                    case 'A':   //Add a coach


                        System.out.print("Please enter the Coach's information:\n");
                        System.out.print("Enter coach's first name:\t");
                        firstName = scan.nextLine();
                        System.out.print("Enter coach's last name:\t");
                        lastName = scan.nextLine();
                        System.out.print("Enter coach's years of experience:\t");
                        years = scan.nextInt();
                        scan.nextLine();
                        Coach sunsCoach = new Coach(firstName, lastName, years);

                        System.out.print("\nPlease enter the Team's information:");
                        System.out.print("\nEnter teams name:\t");
                        name = scan.nextLine();

                        System.out.print("Enter Team's city:\t");
                        city = scan.nextLine();
                        suns = new Team(name, sunsCoach, city);
                        break;
                    case 'D':   //Display course
                        System.out.print(suns.toString());
                        break;
                    case 'Q':   //Quit
                        break;
                    case '?':   //Display Menu
                        printMenu();
                        break;
                    default:
                        System.out.print("Unknown action\n");
                        break;
                }
          }
         else
          {
              System.out.print("Unknown action\n");
          }
        } while (input1 != 'Q' || line.length() != 1);
       scan.close();
   }

  /** The method printMenu displays the menu to a user **/
  public static void printMenu()
   {
     System.out.print("Choice\t\tAction\n" +
                        "------\t\t------\n" +
                        "A\t\tAdd Coach\n" +
                        "D\t\tDisplay Team\n" +
                        "Q\t\tQuit\n" +
                        "?\t\tDisplay Help\n\n");
  }

}

Along with this code, there are my two child class files, Coach.java and Team.java respectively, listed below.

public class Coach
{ String firstName, lastName; //constructor 
int years;
    { String numYears;
    firstName = lastName = numYears = "?";
    
    }
    
    public Coach(String first, String last, int years) { //Set variables 
    
        firstName = first;
        lastName = last; 
        int numYears = years;
    }
        
    

//Accessors 
public String getFirstName()
{return firstName;}
public String getLastName()
{return lastName;} 
public String getYears()
{String numYears = null;
return numYears;}
// Mutators 
public void setFirstName(String theFirstName) 
{firstName = theFirstName;}
public void setLastName(String theLastName) 
{lastName = theLastName;}
public void setYears(int years) 
{int numYears = years;}

public String toString() { 
    String output = "\nLast Name:\t" + lastName + "\nFirst Name:\t " + firstName;
    output += "\nYears of Experience:\t " + getYears() + "\n";
    return output;
}
}

public class Team { 
    String teamName;
    Coach coach; 
    String getCity; 
//Constructor   
    public Team() { 
            teamName = getCity = "?";
            
        }
        
public Team(String name, Coach coach, String cityName) 
{ teamName = name;
 

}
//Accessors 
public String getName() {return teamName;}
public Coach getCoach() {return coach;}
public String getCity() {return getCity;} 

//Mutators 
public void setName(String theName) {teamName = theName;}
public void setCity(String someCity) {getCity = someCity;}
public void setCoach(String firstName, String lastName, int years) 
{ coach = new Coach (firstName, lastName, years);
}

public String toString() 
{ String output = "Team's name:\t" + teamName + " at " + getCity() + "\nCoach Information:";
output += coach.toString();
return output; 
}
}

Running the code, and putting in inputs until I wish to select option D, will eventually yield this specific error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Coach.toString()" because "this.coach" is null at Team.toString(Team.java:30) at Assignment4.main(Assignment4.java:68)

Kimchar
  • 11
  • 3
  • Your question title is misleading. You are NOT compiling when this exception occurs. (And "run in the compiler" is meaningless.) – Stephen C Jan 29 '22 at 03:15
  • As the exception message says, `this.coach` is `null` when you called `toString()` on the `Team` object. It hasn't been initialized. Solution: **work out** why it hasn't been initialized, and fix it. – Stephen C Jan 29 '22 at 03:18

1 Answers1

1

suns = new Team(name, sunsCoach, city);

As I can see you are passing sunsCoach as a parameter to the constructor of Team, but you are not initialising the coach object.

Update your code from

 public Team(String name, Coach coach, String cityName) 
 { 
   teamName = name;
 }

to

  public Team(String name, Coach coach, String cityName) 
  {
    teamName = name;
     coach = coach;
     getCity = cityName;
  }

this would resolve the NullPointerException

Dhanraj
  • 139
  • 3