0

I get the error: Static Error: This class does not have a static void main method accepting String[]. The console also tells me that it runs Guest. However, I start my code with Hotel with the main, so I do not understand why it start with Guest. I used for some classes a constructor. To my knowledge I do not need to have a constructor for every class.

This is my code:

import java.util.*;

class Guest {
  String name;
  static Scanner sc = new Scanner(System.in);

  Guest() {
    name = "NN";
  } 

  void setName() {
    //static Scanner sc = new Scanner(System.in);
    name = sc.next();
  }

  void printName() {
    System.out.println(name);
  }
}

class Room {
  Guest guest;
  int number;

// Room() {
  // number = 1;
 //}

 void newGuest() {
   guest = new Guest();
 }

 void nameGuest() {
   guest.setName();
 }

 void printRoom() {
   System.out.println(number);
   guest.printName();
 }
}

class Hotel {
  Room[] rooms;

  Hotel(int b){
    rooms = new Room[b];
  }

  void printRooms() {
    for(int i=0; i<rooms.length; i++) {
      rooms[i].printRoom();
      System.out.println();
    }
  }

  public static void main(String[] args) {
    new Hotel(4).demo();
  }

  void demo() {
    for(int i=0; i<rooms.length; i++) {
      rooms[i].newGuest();
      rooms[i].nameGuest();
    }
    printRooms();
  }

}
T.v.L.
  • 43
  • 8
  • A few questions in my mind: what is your project structure? Is it all in one file? – DamCx May 17 '18 at 14:25
  • 6
    Noticed none of your classes is public, that's probably why. Put your classes in separate files, make them public and name files the same as classes. And show us how you are starting your app. – Shadov May 17 '18 at 14:26
  • @DamCx Yes it is – T.v.L. May 17 '18 at 14:26
  • Then, as @Shadov just mentionned it above, I strongly suggest you to follow his advice as it is the best way to start solving your problem – DamCx May 17 '18 at 14:27
  • @Shadov Can I also put public for every class instead of making many files? This program will not be any longer. – T.v.L. May 17 '18 at 14:28
  • @DamCx I need to hand it in in one file, so is there maybe another way? – T.v.L. May 17 '18 at 14:32
  • Make `public`the class that needs to be executed, which should be the `Hotel` class, considering where the `main` method is, which also need to be in a `Hotel.java` file – DamCx May 17 '18 at 14:33
  • You should really have one class per file, it is the expected way to do it. If you insist on having them in one file then I suggest making Hotel public and changing the other two to be [inner classes](https://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html) – Joakim Danielson May 17 '18 at 14:33
  • 1
    Oh, oh. Two suggestions to use _inner classes_. There is no need to use inner classes, and in fact you should not do so. You could either use _static nested classes_ - or better keep using _top level classes_, but make only one public. – Seelenvirtuose May 17 '18 at 14:36
  • @DamCx It does indeed run Hotel, however this error occurs now: java.lang.NullPointerException – T.v.L. May 17 '18 at 14:36
  • My bet is on the fact you initialize the `Room` array, but each room of the array is null, so what you need to do is to create a new room for each index – DamCx May 17 '18 at 14:40
  • @T.v.L.in which line do you get this exception ? Please have a look to https://stackoverflow.com/a/218510/5914654 – vincrichaud May 17 '18 at 14:40

1 Answers1

1

Actually this can be run even if you have written it like that. The good practice is to keep separate classes in separate files. Java actually don't allow you to have more than one public classes in a single file. But the way you have written them they are not public so you are fine (I am not saying that's good way to do it).

If you compile the program by javac yourfile.java you will notice you get multiple class files (one for each class defined in that file) and then you can run your program with java Hotel using the class name even if Hotel is not public class! Still it would be better to make it public and keep the others in separate files.

Apart from running the program you get null pointer exception because in this line:

 rooms[i].newGuest();  // rooms[i] is null and you can't call its method

Try changing your constructor to :

 Hotel(int b){
    rooms = new Room[b];
     for(int i=0; i<b; i++) {
          rooms[i]=new Room(); //Create the rooms
     }
  }

This way we prepare the room for guests :)

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
  • This works, thanks a lot. I can now open my hotel :). I will ask my teacher why we do not learn the industry standard. For my understanding, I forgot to make the room object right? – T.v.L. May 17 '18 at 18:39
  • Yes you initialize the array of rooms which prepares the array to take that number of rooms but you never fill it with rooms. And on the [i] position it has empty/null object – Veselin Davidov May 18 '18 at 07:58