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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:08:33+00:00 2026-06-04T15:08:33+00:00

I have that function where i want the user to pick a number of

  • 0

I have that function where i want the user to pick a number of all schedules to delete one.
My logic for delete is to find the i that I want to delete and then swap the last one schedule of my array with the i one, and then the last one to be null.

When i test my program i see that when I want to delete the last one of my array everything works ok, but when i try to delete something else an error shows up in eclipse.

Exception in thread “main” java.lang.NullPointerException
I cant figure out what’s happening. I have reviewed my code a lot of times.

 public void ScheduleDelete(){
    StandardInputRead readDeleteSchedule = new StandardInputRead();

    int deleteScheduleNum =readDeleteSchedule.readPositiveInt("Enter the schedule number you want to delete: ");
    for(int i=0;i<scheduleSize;i++){
        if(deleteScheduleNum==this.scheduleList[i].getScheduleNum()){
            this.scheduleList[i]= this.scheduleList[this.scheduleSize-1];
            this.scheduleList[i].setScheduleNum(i);
            this.scheduleList[this.scheduleSize-1]= null;
        }
    }
    this.scheduleSize--;
}
  • 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-04T15:08:33+00:00Added an answer on June 4, 2026 at 3:08 pm

    When you swap the element with the last one the second time, you will replace the element with null (as you have just set the last element of the array to null in the previous swap). So when you want to use the element, you are operating on a null reference, hence the exception.

    You need to keep track of how many active element you have in the array and swap the element with the last active one.

    Alternatively, you can use a dynamic collection (e.g. a list) where you don’t need to excplicitly keep track of the active elements because you only store the active ones.

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

Sidebar

Related Questions

I have one function ParseHtmlTable(string htmlContent) . Now in that function I want to
I have 1 function that I want to return the address of an assigned
I have an onclick function that I want to add an anchor to the
I have a PHP function that I want to make available publically on the
I have function getCartItems in cart.js and I want to call that function in
I have a function that return [[]] , and I want to test the
I have a function that is recursively calling itself, and i want to detect
I have a function that takes a string-like argument. I want to decide if
I have the following function that I only want to run inside <div id=imgWrapper>
I have a PHP function that requires can take 3 parameteres... I want to

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.