Error initializing class instances
hi,
heres my code for my main class:
Code:
public class Main {
static UserInterface ui = new UserInterface();
static Flight aFlight = new Flight();
static Keyboard kb = new Keyboard();
Seat seat = new Seat();
char menuChoice = 0;
public static void main(String[] args) {
int menuChoice = 0;
do {
ui.displayMainMenu();
menuChoice = kb.getInt("select: ");
if(menuChoice == 1)
flightAdmin();
else if(menuChoice == 2)
bookingsMenu("");
else if(menuChoice == 3)
aFlight.viewSeat();
else if(menuChoice == 4){
aFlight.displayFlightInfo();
kb.enterToContinue();
}
} while (menuChoice != 6);
}
public static void flightAdmin(){
char menuChoice = ' ';
do{
ui.displayAdminMenu();
menuChoice = kb.getChar("select: ");
if(menuChoice == '1')
enterFlightDetails();
else if (menuChoice == '2')
changeFlightStatus();
} while (menuChoice != 'R');
}
public static void enterFlightDetails(){
ui.displayFlightMenu();
int menuChoice = 0;
menuChoice = kb.getInt("Chose Flight: ");
if(menuChoice == 1)
aFlight.setFlightDetails("SA123", "Glasgow", "Barra", "01-10-10" );
else if (menuChoice == 2)
aFlight.setFlightDetails("SA124", "Barra", "Glasgow", "02-10-10" );
else if (menuChoice == 3)
aFlight.setFlightDetails("SA234", "Glasgow", "Benbecula", "03-10-10" );
else if (menuChoice == 4)
aFlight.setFlightDetails("SA235", "Benbecula", "Glasgow", "04-10-10" );
else if (menuChoice == 5)
aFlight.setFlightDetails("SA345", "Glasgow", "Stornaway", "05-10-10" );
else if (menuChoice == 6)
aFlight.setFlightDetails("SA346", "Stornaway", "Glasgow", "06-10-10" );
}
public static void bookingsMenu(String flightNumber){
bookings(flightNumber);
}
public static void changeFlightStatus(){
aFlight.setFlightStatus();
System.out.println("Status Changed");
kb.enterToContinue();
}
public static void flightDetails(){
ui.displayStatusMenu();
int menuChoice = 0;
do{
menuChoice = kb.getInt("select: ");
if(menuChoice == 1)
aFlight.displayFlightInfo();
else if (menuChoice == 2)
aFlight.displaySeatingPlan();
} while (menuChoice != 3);
}
public static void bookings(String flightNumber){
ui.displayBookingsMenu();
char menuChoice = 0;
do{
menuChoice = kb.getChar("select: ");
if(menuChoice == '1')
aFlight.updateSeat(flightNumber, 0);
else if (menuChoice == '2')
aFlight.updateSeat(flightNumber, 1);
else if (menuChoice == '3')
aFlight.updateSeat(flightNumber, 3);
else if (menuChoice == '4'){
aFlight.seats[6].getSeatNo();
aFlight.seats[9].getSeatNo();
aFlight.seats[2].getSeatNo();
}
} while (menuChoice != 'R');
}
}
and here is my code for my flight class:
Code:
public class Flight {
protected Seat[] seats = new Seat[24];
String flightNumber;
String departure;
String arrival;
String seatNumber = "";
boolean available;
boolean checkingIn;
boolean boarding;
boolean closed;
boolean full;
static String statusMessage = "";
int freeSeats = 24;
int reservedSeats;
int bookedSeats;
int rows;
int cols;
int i;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String fileName = "h://MyDocuments/NetBeansProjects/ScotiaAirlines/Flights.txt";
StringTokenizer s;
File myFile;
static Keyboard kb = new Keyboard();
String Date;
// String line = "";
// line = read.readline();
// s = StringTokenizer(line, ':');
public Flight(){
//myFile = new File(fileName);
for (i = 0; i < 24; i++) {
seats[i] = new Seat();
/* int row = 1;
String col = "";
if(i%4 == 1){
col = "A";
}
else if(i%4 == 2){
col = "B";
System.out.println(row);
}
else if(i%4 == 3){
col = "C";
System.out.println(row);
}
else if(i%4 == 0){
col = "D";
System.out.println(row);
}
seats[i].seatNo = row+col;
row++;
}*/
}
}
public void setFlightDetails(String FlightNumber, String departure, String arrival, String Date){
this.flightNumber = FlightNumber;
this.departure = departure;
this.arrival = arrival;
this.Date= Date;
}
public void setFlightStatus(){
statusMessage = (kb.getString("Enter Flight Status: "));
}
public void viewSeat(){
System.out.println(" A B C D");
int i=0;
for (rows = 0; rows < 6; rows++) {
System.out.print(" " + (rows + 1) + " - ");
for (cols = 0; cols < 4; cols++) {
System.out.print(seats[i].status + " ");
i++;
}
System.out.println("");
}
kb.enterToContinue();
}
public void displayFlightInfo(){
System.out.println("Flight Number = " + flightNumber);
System.out.println("Daparture City = " + departure);
System.out.println("Arrival = " + arrival);
System.out.println("Date = " + Date);
System.out.println("Flight Status = " + statusMessage);
}
public void displaySeatingPlan(){
}
public void loadFromFile(){
}
public void saveToFile(){
}
public void updateSeat(String flightNumber, int type){
int r = 0;
int c = 0;
int position = 0;
int seatStatus = 0;
if(freeSeats == 0){
System.out.println("Flight " + flightNumber + " is full");
kb.enterToContinue();
return;
}else if (statusMessage.equals("Gate Closed")) {
System.out.println("Flight " + flightNumber + " has closed");
kb.enterToContinue();
return;
}
seatNumber = kb.getString("Please Enter Seat Number to Update: ");
try{
r = Integer.parseInt(String.valueOf(seatNumber.charAt(0)));
switch(seatNumber.charAt(1)){
case 'A':case 'a':
c = 1;
break;
case 'B':case 'b':
c = 2;
break;
case 'C':case 'c':
c = 3;
break;
case 'D':case 'd':
c = 4;
break;
}
} catch (Exception error) {
System.out.println("INVALID SEAT NUMBER");
kb.enterToContinue();
return;
}
position = (((r - 1) * 4) + c) - 1;
seatStatus = Integer.parseInt(seats[position].seatNo[0]);
System.out.println(seatStatus);
}
}
the error i am getting is:
Code:
java.lang.ExceptionInInitializerError
Caused by: java.lang.ArrayIndexOutOfBoundsException: 4
at package.Flight.<init>(Flight.java:42)
at package.Main.<clinit>(Main.java:14)
Could not find the main class: scotiaAirlines.Main. Program will exit.
any ideas? :(