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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:20:05+00:00 2026-06-03T09:20:05+00:00

the following splitter method is designed to return a IntLinkedBag array of two separate

  • 0

the following splitter method is designed to return a IntLinkedBag array of two separate linked lists. It accomplishes this by calling listSplitLessGreater method and passes the head reference of a linked list of integers and a splitter integer value which returns an IntNode array that contains the head reference of two new linked lists of integers that have been sorted. The splitter method works as written. It was the only way I could get it to work. But I know there has to be an easier/better way to code it. everything between the //******** I want to replace with this

newLists[0].head = answer[0]; 
newLists[1].head = answer[1];

But when I do I receive null pointer exemption errors when running the program. My question is why? and how is the correct way to write this code?

 public IntLinkedBag[] splitter(int target)
{
    IntLinkedBag[] newLists = new IntLinkedBag[2]; 
    IntNode[] answer = new IntNode[2];
    answer = IntNode.listSplitLessGreater(head, target);

    //**********************************
    IntLinkedBag b = new IntLinkedBag();
    IntLinkedBag c = new IntLinkedBag();
    b.add(5);
    b.head = answer[0];
    newLists[0] = b;
    c.add(5);
    c.head = answer[1];
    newLists[1] = c;
    //**********************************

    return newLists;
}

I updated my code like to below. I get no errors, but it now isn’t returning anything. Even though the test code prints the linked list…. and idea’s?

  public IntLinkedBag[] splitter(int target)
{
    IntLinkedBag[] newLists = {new IntLinkedBag(), new IntLinkedBag()}; 

    IntNode[] answer = new IntNode[2];
    answer = IntNode.listSplitLessGreater(head, target);


        newLists[0].head = answer[0]; 
    newLists[1].head = answer[1];



    System.out.println(IntNode.showList(newLists[0].head)); //test code
    System.out.println(IntNode.showList(newLists[1].head)); //test code

    return newLists;
}

adding this code gets it to work but why is this required?

newLists[0].add(5); //adding a number to linked list to create a temporary head.
newLists[1].add(5); //don't know why I have to have this but its only way code works correctly
  • 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-03T09:20:06+00:00Added an answer on June 3, 2026 at 9:20 am

    There’s nothing actually initialized inside of newlists[0] or newlists[1]; the array creates default values of null on creation.

    Try something like this:

    IntLinkedBag[] newLists = new IntLinkedBag[2];
    for(int i = 0; i < newLists.length; i++) {
        newLists[i] = new IntLinkedBag();
    }
    

    More compact, you could do this:

    IntLinkedBag[] newLists = {new IntLinkedBag(), new IntLinkedBag()};
    

    That does the same thing without a loop.

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

Sidebar

Related Questions

Following is the script for matching regex and storing value in array: sub b1
Following on from this question , I am interested in finding out how you
following on from this question (Developing to an interface with TDD), I'm still having
I have the following simpl WPf grid, two columns, a button in each column,
Following the responses to this , I am curious to know what is the
I have a SplitContainer with two panels. When I drag the splitter to resize
Following are the two approaches: constructor with all the class properties Pros: I have
following this link http://www.codeproject.com/KB/cs/MDITabBrowsing.aspx I made MDI and things are working well. But I
Following from Way/coding method to do activitylist/log I worked with the accepted answer on
Following this question: Good crash reporting library in c# Is there any library like

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.