-1
public Game() throws FileNotFoundException {
    map = new ArrayList<Room>(); 

    String room1Desc;
    int room1Name = 1;
    String line1 = null;
    try(BufferedReader br = new BufferedReader(new FileReader("Roomprompts.txt"))) {
        for(int i = 1;i<room1Name;i++){
            br.readLine();
            line1 = br.readLine();
            System.out.println(line1);
        }

    } catch (IOException e) {
        System.out.println("There was an error");
    }

    //                 Room( name,   description,                             N,        S,      W,      E )
    map.add(new Room(line1, "A dank room that smells of troll", Direction.NOEXIT, 2, Direction.NOEXIT, 1));
    map.add(new Room("Forest", "A leafy woodland", Direction.NOEXIT, Direction.NOEXIT, 0, Direction.NOEXIT));
    map.add(new Room("Cave", "A dismal cave with walls covered in luminous moss", 0, Direction.NOEXIT, Direction.NOEXIT, 3));
    map.add(new Room("Dungeon", "A nasty, dark cell", Direction.NOEXIT, Direction.NOEXIT, 2, Direction.NOEXIT));

    player = new Actor("player", "a loveable game-player", map.get(0));
}

I'm trying to read a line 1 from a text file I have on my desktop called "Roomprompts.txt". I have tried to make the line to read into a string, but the output I always get is "There was an error". I'm not sure why it's not reading the first line from the text file into the room name. The point of this is to have the console print "This is a stinky trolls room", but it just gives back the message "There was an error".

Thank you @Scott Hunter. I never even knew that existed, it says the file was not found, but I'm looking at it right now. I fixed the file not found error, and now it says that it is null, but at the same time it tells me it has to be initialized as null?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    You should print out the [stacktrace](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) of the exception that happens. Without knowing the actual error that occurred it is nearly impossible to tell what went wrong. Try adding `e.printStackTrace();` into the catch block and then you should add the printed stacktrace to your question. – OH GOD SPIDERS Aug 29 '23 at 15:30
  • you should provide the whole code and also the exact error, otherwise it is not possible to help – Shila Mosammami Aug 29 '23 at 15:39

1 Answers1

0

If room1Name is set to 1, then your for loop will never be executed because the condition i < room1Name will not be satisfied...

  • Thank you I didn't even realize I didn't set it to the length of the file I needed! – Benny Harvey Aug 29 '23 at 15:37
  • *If room1Name is set to 1, then your for loop will never be executed* True, but academic here as an error is being reported. You need to replace that error print with `e.printStackTrace();` and then post the exception *formatted as code*. Also you are reading lines *twice* in the loop - why? I would caution you against naming a `List` 'map' as well – g00se Aug 29 '23 at 16:07