Based upon another Headfirst exercise I’m having trouble with populating my GUI with the data of vehicles. I use a Controller class to manage my vehicle Object class. For some reason I’m getting an index out of range exception.
Gui Class
public class ShowroomDriver{
public static Showroom Cars = new Showroom("Cars");
public static void main(String[] args) {
Showroom Cars = new Showroom("Cars");
Vehicle vechicle1 = new Vehicle("Ford");
Cars.addVehicle(vechicle1);
GuiInterface gui = new GuiInterface("Car Showroom");
}
private static class GuiInterface extends JFrame {
private JButton saleButton, previousButton, nextButton;
private static JTextField textField1;
private JLabel label1;
private JPanel[] p = new JPanel[5];
public GuiInterface(String sTitle) {
super(sTitle);
setLayout(new FlowLayout());
previousButton = new JButton("Previous Car");
nextButton = new JButton("Next Car");
saleButton = new JButton("Sale");
for(int i = 0; i < 5; i++){
p[i] = new JPanel();
}
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel formPanel = new JPanel(new GridLayout(1, 2));
textField1 = new JTextField(10);
label1 = new JLabel("Manufacture");
p[0].add(label1);
p[1].add(textField1);
for(int i = 0; i < 2; i++){
formPanel.add(p[i]);
}
contentPane.add(formPanel, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
this.setResizable(false);
this.setLocationRelativeTo(null);
getField();
this.setVisible(true);
}
private void getField(){
textField1.setText(Cars.currentVehicle().getManufacutre());
}
}
}
Controller Class
public class Showroom{
private ArrayList<Vehicle> vehiclesSold = new ArrayList();
private ArrayList<Vehicle> theVehicles;
private String vechicleType;
private int arrayPosition = 0;
public Showroom(String type){
vechicleType = type;
theVehicles = new ArrayList<Vehicle>();
}
public boolean addVehicle(Vehicle newVehicle){
theVehicles.add(newVehicle);
return true;
}
public Vehicle currentVehicle(){
return theVehicles.get(arrayPosition);
}
public void getVehicles(){
System.out.println("---Vehicle Type: " + vechicleType +"---");
for(Vehicle nextVehicle : theVehicles){
System.out.println(nextVehicle.toString());
}
}
}
Vehicle Class
public class Vehicle{
private String Manufacture
Vehicle(String Manufacture){ //There are more
this.Manufacture = Manufacture;
}
}
@Override
public String toString(){
String s = "Maufacture: " + getManufacutre()
"\n";
return s;
}
public String getManufacutre() { return this.Manufacture; }
}
Without more code it is not possible to tell where the error comes from. But from this piece of code, the only place an
IndexOutOfBoundsExceptioncan com from isYour problem is, that
arrayPositionis wrong.Try debugging your code for finding out what exactly goes wrong, or post more code
Edit:
You seem to have a misunderstanding on what the
statickeyword does.staticobjects or methods are something, that is only instantiated once during runtime.For example your declaration of the
Carsattribute inclass ShowhroomDrivermeans, that the classShowroomDriverhas a single class attribute namedCars(and by the way – do not let attributes start with an uppercase character. This is very confusing).What you want though is to pass an instance of
ShowRoom(yourCarsattribute) to your classGuiInterface(also remove thestatickeyword there) via its constructor, like this:Then, instead of
you write
Also remove all
statickeywords except the one at themainmethod.