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 7572105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:50:28+00:00 2026-05-30T15:50:28+00:00

In FleetTUI.java I have a list of Fleets (Each fleet will hold its own

  • 0

In FleetTUI.java I have a list of Fleets (Each fleet will hold its own list of Trucks).

private static ArrayList<Fleet> fleetCollection;

In Fleet.java, I have a list of Truck objects.

ArrayList<Truck> fleetList;

which gets initialized in it’s constructor.

    public Fleet(String businessName){
    this.businessName = businessName;
    this.fleetList = new ArrayList<Truck>();
}

So every time I make a new fleet, I am also making a list of Trucks along with it.

In FleetTUI.java, I have a method that adds a truck to a Fleet, made prior:

    public static void addTruck(){
    printFleets();
    System.out.println("Please enter the fleet number where this truck will be added:");
    inputText = scan.nextLine();
    int inputFleetId = Integer.parseInt(inputText);
    System.out.println("Please enter the truck's horn sound:");
    inputText = scan.nextLine();
    String inputHorn = inputText;
    System.out.println("Please enter the truck's fuel capactity:");
    inputText = scan.nextLine();
    int inputFuelCapacity = Integer.parseInt(inputText);
    System.out.println("Please enter the amount of gas remaining in the tank:");
    double inputGasRemaining = Double.parseDouble(scan.nextLine());
    **Fleet.fleetList**.add(inputFleetId, new Truck(inputHorn, inputFuelCapacity, inputGasRemaining));
    System.out.println("--- A truck that goes " + inputHorn + " with " + inputGasRemaining + " gallon(s) in a " + inputFuelCapacity + " gallon tank has been added to " + **fleetCollection.getBusinessName()** + "'s fleet ---" );
    System.out.println("");
}

I have bolded where I am getting errors.
The errors are:

Cannot make a static reference to the non-static field Fleet.fleetList

Cannot make a static reference to the non-static method getBusinessName() from the type Fleet

I don’t know any other way to access the list of trucks from inside the fleet I created with the method:

    public static void createFleet(){
    System.out.println("");
    System.out.println("Please enter the name of the fleet.");
    inputText = scan.nextLine();
    
    fleetCollection.add(new Fleet(inputText));
    printFleets();
    System.out.println("");
    System.out.println("--- Fleet: " + inputText + " added ---");
    System.out.println("");
    
}

So I guess really, my question is:
How do I correctly add a new Truck into the list of Trucks, which is inside the Fleet I just made using createFleet(), which is housed in it’s own collection of fleets?

Update: Thanks 🙂 you guys really helped me! once again!

  • 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-05-30T15:50:29+00:00Added an answer on May 30, 2026 at 3:50 pm

    Your problem is that you are creating static methods within which you are then trying to access a non-static variable, in this case this being fleetlist. This will cause the error you are showing.

    Since you are using a constructor and everything, you do not need to call the method static. Calling it static will allow the method to be called without the object needing to be constructed, which from what I am seeing is not something you are after.

    So in short, make your methods non-static by removing the static keyword from the method declaration and access your methods such as addTruck() and createFleet() through the use of the initialized Fleet object.

    On the other hand, you can declare your fleetlist to be static, like so:

    private static ArrayList<Truck> fleetList;
    

    EDIT: Seeing your comment, this is how you can do it:

    In Java, it is considered good practice to encapsulate your object variables so that you can better control the access to such variable, so you can do something like so in your Fleet Class:

    private List<Truck> fleetList;
    
    public Fleet(String businessName){
        this.businessName = businessName;
        this.fleetList = new ArrayList<Truck>();
    }
    
    public List<Truck> getFleetList()
    {
        return this.fleetList;
    }
    

    In your FleetUI Class you can do something like so:

    private ArrayList<Fleet> fleetCollection;    
    ...   
    for (Fleet fleet : this.fleetCollection)
    {
        for (Truck truck : fleet.getFleetList())
        {
             //Do something with your truck here
        }
    }
    

    EDIT 2: To add a truck to your array, you can do it in 2 ways. Let us assume that the object fleet is the Fleet to which you want to add your truck. In your FleetUI class you can do like so:

    fleet.getFleetList().add(new Truck(...))

    The other (and in my opinion, better way) is to have an addTruck method in your fleet object like so:

    public void addTruck(Truck truck)
    {
        this.fleetList.add(truck);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.