Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8888179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:58:37+00:00 2026-06-14T21:58:37+00:00

Based upon another Headfirst exercise I’m having trouble with populating my GUI with the

  • 0

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; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:58:38+00:00Added an answer on June 14, 2026 at 9:58 pm

    Without more code it is not possible to tell where the error comes from. But from this piece of code, the only place an IndexOutOfBoundsException can com from is

    return theVehicles.get(arrayPosition);
    

    Your problem is, that arrayPosition is 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 static keyword does.
    static objects or methods are something, that is only instantiated once during runtime.
    For example your declaration of the Cars attribute in class ShowhroomDriver means, that the class ShowroomDriver has a single class attribute named Cars (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 (your Cars attribute) to your class GuiInterface (also remove the static keyword there) via its constructor, like this:

    // ...
    private Showroom cars;
    public GuiInterface(String sTitle, Showroom cars) {
        // ...
        this.cars = cars;
        // ...
    }
    

    Then, instead of

    private void getField(){
        textField1.setText(Cars.currentVehicle().getManufacutre());
    }
    

    you write

    private void getField(){
        textField1.setText(this.cars.currentVehicle().getManufacutre());
    }
    

    Also remove all static keywords except the one at the main method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to copy a row from one sheet to another based upon having
I've got an oDate source based upon WCF data services. When I browse to
I'm trying to update a table based upon the user id from another table.
This scenario is based upon a schema in another question and I'm not interested
I want to make a site that updates itself based upon changes on another
I need to update cells within a specific column based upon ids in another
Summary : I need to authorize pages based upon the data present in the
I have to request data for a JS-script from a MySQL database (based upon
I am writing a template in Word 2010 which is based upon another template,
Based upon this question i decided to try to use waithandles/eventwaithandle for my solution

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.