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

  • Home
  • SEARCH
  • 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 6884461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:30:41+00:00 2026-05-27T05:30:41+00:00

The problem I’m having is that a Vehicle object is not being deleted from

  • 0

The problem I’m having is that a Vehicle object is not being deleted from an array (I’m not sure if it’s even storing correctly.)

The user must first add a Vehicle object to the array. And then it brings them back to the menu to ask if they want to add another, or delete an Vehicle.

The method in this class accepts a choice (either 1 or 2) to add a Vehicle to an array, or delete a vehicle from an array:

private void validate(int choice, File file) throws FileNotFoundException
{
    Vehicle[] vehicle =  new Vehicle[4];
    switch(choice)
    {
    case 1:
        store = new Store(file);
        store.addVehicle(vehicle);
        run();
        break;
    case 2:
        store = new Store();

        if(store.deleteVehicle(vehicle) == false)
        {
            System.out.println("Vehicle not deleted");
        }
        run();
        break;
}

Let’s say the user wants to add a vehicle. It gets passed to the Store class:
My declarations in the Store class:

Vehicle[] vehicles = new Vehicle[4];

The addVehicle method in the Store class:

public void addVehicle(Vehicle vehicle[]) throws FileNotFoundException
{
    boolean found = false;
    if(canAddVehicle())
    {
        for(int i = 0; i < vehicles.length || !found; i++)
        {
            if(vehicles[i] == null)
            {
                Scanner reader = new Scanner(file);
                vehicle[i] = new Vehicle();
                vehicle[i].readRecord(reader);
                found = true;
                reader.close();
            }

        }
        System.out.println("Vehicle Added!");
    }
    else
    {
        System.out.println("You can not add more than 4 vehicles.");
    }

    System.out.println(vehicle[0].getVIN());
}

I added that last line at the bottom to test if it was storing the variables in the Vehicles class from when I called readRecord(). (It shows up correctly.)

Now it will go back to the first method posted and let’s say the user wants to delete a Vehicle. It gets passed to the Store class method (deleteVehicle(vehicle)):

public boolean deleteVehicle(Vehicle vehicle[])
{
System.out.println(vehicle[0].getVIN());

    Scanner input = new Scanner(System.in);
    String VIN = null;

    System.out.println("Please enter the VIN of the vehicle you want to delete:");
    VIN = input.next();
    for(int i = 0; i < vehicles.length; i++)
    {
        if(vehicle[i] != null)
        {
            if(vehicle[i].getVIN().equals(VIN))
            {
                vehicle[i] = null;
                return true;
            }
        }
    }
    return false;
}

I added that line at the top to see if there was still a value for the VIN in slot 0 of the vehicles array… it turns out I get a NullPointerException error there… so it seems the first Vehicle object added is not being stored in the array correctly? I’m new to arrays and kind of lost as to what I am doing wrong. Any help would be appreciated. Thanks!

  • 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-27T05:30:42+00:00Added an answer on May 27, 2026 at 5:30 am
    case 2:
        store = new Store();
    
        if(store.deleteVehicle(vehicle) == false)
        {
            System.out.println("Vehicle not deleted");
        }
        run();
        break;
    

    Since you set store to new Store() every time, this is a fresh new instance of the Store class, so there is nothing to be deleted.

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

Sidebar

Related Questions

Problem: We have a web app that calls some web services asynchronously (from the
Problem: I have an address field from an Access database which has been converted
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
Problem: Ajax suggest-search on [ n ] ingredients in recipes. That is: match recipes
Problem: Given a list of strings, find the substring which, if subtracted from the
Problem: I can set the exposureMode property of AVCaptureDevice, but it does not stay
Problem: I'm working on a website where there is a dial that displays a
Problem: I need an array (datamember) in a parent class A. However, depending on
Problem! I Have the following input (rules) from a flat file (talking about numeric

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.