0

I am having a problem at a Java program. Exception in thread "main"

exception in thread main java.lang.nullpointerexception at VenueHireSystem.main (VenueHireSystem.java:27)

is the error that I'm getting. I could really use some help, since i am stuck hours on this spot...

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;


public class VenueHireSystem {

    public static void main(String[] args) {

          Scanner sc = null;
          try
          {
              sc = new Scanner(new FileReader("test.txt"));  
          }
          catch (FileNotFoundException e) {}
/*        finally
          {
              if (sc != null) sc.close();
          }
*/       
          
          Manager VenueManager = new Manager();
          
          String info;
          String key;
          
          while (sc.hasNextLine() && sc.hasNext()){
              key = sc.next();
              info = sc.nextLine();
              if (key.equals("Venue")){
                  VenueManager.addVenueAndRoom(info);
              } else if (key.equals("Request")){
                  VenueManager.RequestBooking(info);
              } else if (key.equals("Change")){
                  VenueManager.ChangeBooking(info);
              } else if (key.equals("Cancel")){
                  VenueManager.CancelBooking(info);
              } else if (key.equals("Print")){
                  VenueManager.PrintBooking(info);
              }
          }
          
          sc.close();
    }

}
konex
  • 1
  • 1
  • 2
    If you catch a `FileNotFoundException`, the `Scanner` will be `null` . – Arnaud Sep 30 '20 at 10:15
  • 1
    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) – Milgo Sep 30 '20 at 10:15
  • Never silently swallow an exception. – Henry Sep 30 '20 at 10:18
  • Really appreciate your quick replies but I'm totally new to Java. I know the error is in the exception handling but I don't really know how to fix it. – konex Sep 30 '20 at 10:21
  • @Arnaud Thanks for your quick reply, what am I supposed to catch then if I didn't catch the FileNotFoundException? – konex Sep 30 '20 at 10:23
  • If the file cannot be found, just print an error message and exit the program. – Arnaud Sep 30 '20 at 10:25

0 Answers0