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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:52:18+00:00 2026-05-27T02:52:18+00:00

There is probably a simple solution to this problem I’ve been having all night.

  • 0

There is probably a simple solution to this problem I’ve been having all night. At least I hope there is. When attempting to offer an object to my subQueues LinkedList, I receive a NullPointerException. My program prints out the correct “head” Objects and “digit” integers, but then the Exception is thrown and the program ends.

My program, in short, is supposed to take a mainQueue LinkedList of integers, look at them all one-by-one, and sort them. Its examining the last digit of each integer, and placing them into corresponding subQueues. As of now, I am only to the ones place. After I get past this dilemma, I’ll be able to work out the tens, hundreds, etc.

Ex)

mainQueue = { 12 50 215 100 85 539 16 35 }
subQueue[0] = { 50 100 }
subQueue[1] = { }
subQueue[2] = { 12 }
subQueue[3] = { }
subQueue[4] = { }
subQueue[5] = { 215 85 35 } 
subQueue[6] = { 16 }
subQueue[7] = { }
subQueue[8] = { }
subQueue[9] = { 539 }

So what am I doing wrong here? Like I said, once I get by this little problem, the rest of the program should be a breeze. Any help is appreciated, thanks!

public class Sorting
 {
   private LinkedList mainQueue;
   private LinkedList[] subQueues;
   private final int SIZE = 10;
   private int maxDigits; //maximum number of digits

   //The constructor instantiates the mainQueue using the LinkedList,
   //subQueue array as an array of LinkedList using SIZE(10),
   //and initializes maxDigits = 0;
   public Sorting()
   {
    mainQueue = new LinkedList();
    for (int i=0; i<SIZE; i++)
    {
        subQueues = new LinkedList[i];
    }

    // I have also tried:
    // subQueues = new LinkedList[SIZE];   
    //I get the same runtime error.

    maxDigits = 0;
 }

   public void sortNumbers()
    {
    while (mainQueue.isEmpty() == false)
    {
        Object head = mainQueue.peek();
        mainQueue.remove();
        String digitLine = "" + head;
        int digit = Integer.parseInt(digitLine.substring(digitLine.length()-1, digitLine.length()));

        System.out.println(head);
        System.out.println(digit);

        subQueues[digit].offer(head);
    }
    }
}
  • 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-27T02:52:19+00:00Added an answer on May 27, 2026 at 2:52 am

    You’re not correctly building your subQueues it looks like.
    If you want an array of SIZE linked-lists, try this:

    subQueues = new LinkedList[ SIZE ];
    for ( int i = 0; i < SIZE; ++i ) {
        subQueues[i] = new LinkedList();
    }
    

    Note that this is using raw types as your code is, though preferably you should use parameterized types.

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

Sidebar

Related Questions

I'm new with Objective-C, so there probably is a simple solution to this. I
There is probably is simple fix for this but I currently have code similar
I'm new to the OOP paradigm, so there's probably a simple explanation for this
I've got probably what is a simple problem, but there's no informative errors or
I'm sure that there's a really simple solution to this that will make me
there's probably a very simple solution but I'm at my wit's end here. I
I'm aware that there probably isn't a perfect solution to my question (this sounds
I was searching Google for a page offering some simple OpenMp algorithms. Probably there
I'm using .Net 2.0 and this is driving me crazy but there's probably some
Yes, this is probably yet another greatest-n-per-group question... But I've tried at least a

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.