V.tharan924
V.tharan924

Reputation: 7

Error when trying to display an array in alphabetic order

I'm making a hotel booking system to be displayed through the console. The system should allow the user to select up to 10 room numbers (1-10) and give the customers name that is being booked in. Below I have put a method to keep all the rooms empty when the program is run.

private static void initialise(String hotelRef[]) {
for (int x = 1; x < 11; x++) {
    hotelRef[x] = "EMPTY";
    }
}

I can book rooms and view them but when i try to sort the array to be displayed in alphabetical order, it terminates the program with an error. (nullpointerexception in main thread).

     Arrays.sort(hotel);                
            for (int x = 1; x < 11; x++) {

                System.out.println(Arrays.toString(hotel));

            }

Above is what I am currently trying but it doesn't reach the first line. Any idea on how I can display the array in order? Any help is greatly appreciated.

P.s forgot to mention the array is initialised at the start of the main method. The code above is in another method. My main method:

public static void main(String[] args) {
    String[] hotel = new String[11];
    initialise(hotel);   
    Menu(hotel);
}

Upvotes: 1

Views: 119

Answers (4)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59960

First Your loop start from 1 to 11, so always the first valus is null :

[null, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY]
--^^------------------------------------------------------------------------

Second when you change your array you should to return it.

Your program should be like this :

public static void main(String[] args) {
    String[] hotel = new String[11];
    hotel = initialise(hotel);
    Menu(hotel);

}

private static String[] initialise(String hotelRef[]) {
    for (int x = 0; x < hotelRef.length; x++) {
        hotelRef[x] = "EMPTY";
    }
    return hotelRef;
}

private static void Menu(String[] hotel) {
    Arrays.sort(hotel);
    for (int x = 0; x < hotel.length; x++) {
        System.out.println(Arrays.toString(hotel));
    }
}

Note

When you use the lenght don't use the length like this x < 11 this can make a problem if you change the size, so to avoid all this use arrayName.length instead like hotel.length

Upvotes: 0

Mohammed Ait
Mohammed Ait

Reputation: 149

Just, a problem on the for loop.
You have to start with 0 not 1 :

private static void initialise(String hotelRef[]) {
  for (int x = 0; x < 10; x++) {
   hotelRef[x] = "EMPTY";
  } 
}

Because, when you instantiate the hotel array like this :

String[] Hotel = new String[10]

you have 10 rooms starting with 0 to 9

Upvotes: 0

G. Fiedler
G. Fiedler

Reputation: 726

Array indexes are starting at 0 but your loop is starting at index 1. So hotel[0] is null.

Upvotes: 0

Timothy Truckle
Timothy Truckle

Reputation: 15622

here is your Problem:

for (int x = 1; x < 11; x++) {

You use natural numbers for indexing within the array, But in Java indexes start with 0. Therefore the first element in your array is not initialized.

In turn when Arrays.sort(hotel) tries to invoke the equals() method on the element hotel[0] it raises a NullPointerException.

Solution:

Get used to zero based indexing.

Upvotes: 1

Related Questions