Results 1 to 2 of 2
- 06-22-2011, 03:52 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Error initializing class instances
hi,
heres my code for my main class:
and here is my code for my flight class:Java 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'); } }
the error i am getting is:Java 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); } }
any ideas? :(Java 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.
- 06-22-2011, 03:58 PM #2
Can you read the stack trace shown in the error. It tells you what happened: ArrayIndexOutOfBoundsException: 4
The index value of 4 is outof bounds for the array.
The statement is at line 42 Flight.<init>(Flight.java:42) in Flight.java is where it happened.
The <init> shows that the code was in a constructor that was called from line 14 in Main: (Main.java:14)
Look at line 42 and see why the code allows the index to get to a value that is greater than the number of elements in the array.
Similar Threads
-
Help with initializing error
By GDW in forum New To JavaReplies: 7Last Post: 10-20-2010, 08:10 PM -
Drawing multiple instances of one class with an array
By Grimmjow in forum New To JavaReplies: 16Last Post: 05-22-2010, 03:51 AM -
approx. 1000 instances of class?
By artemff in forum CLDC and MIDPReplies: 0Last Post: 01-01-2010, 07:57 PM -
Class Instances stored in an ArrayList
By Ersk in forum New To JavaReplies: 4Last Post: 12-12-2009, 04:13 PM -
Problem with class instances
By sdwinder in forum New To JavaReplies: 7Last Post: 10-21-2009, 01:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks