Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Adding a player name

I'm trying to get the player to enter their name before my game starts playing. However I'm getting the error

"method addName in glass Gamme cannot be applied to given types;

required: java.lang.String[]; found no arguments

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private String playerName;

    public Game() 
    {
        createRooms();
        parser = new Parser();

        addName();
        play();       
    }


    private static void addName(String[] args)
    {
       Console console = System.console();
       String playerName = console.readLine("Welcome, what is your name? ");

     if (playerName.length() >= 2) {
         System.out.println("Hello" + playerName);
     }   
     else {
          String playerName = console.readLine("Not and ok name, re-enter name");
      }
    }
    }
}

3 Answers

private static void addName(String[] args) {...}

requires an input of an array of Strings. As it is now, you are not supplying that input when you call the method addName().

Just remove the input argument from the method signature:

private static void addName() {...}

Hi Rebecca,

That works thank you so much. I am however getting another error now when the game actually runs in the terminal:

java.lang.NullPointerException at Game.addName(Game.java:43) at Game.<init>(Game.java:34)

Don't have the line numbers. What is on those two lines, 43 and 34?

Don't have the line numbers. What is on those two lines, 43 and 34?

Blank lines according to Sublime, no worries I will try moving things around a bit