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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:57:02+00:00 2026-06-11T09:57:02+00:00

I am having trouble with java.util.LinkedList. I am getting a null pointer exception upon

  • 0

I am having trouble with java.util.LinkedList. I am getting a null pointer exception upon using the poll() on the list even after adding an element to the list. I am not using threads or anything similar to threads.

Any help is appreciated. Below is the myMethod code which is called from a main method of another class:

  void myMethod(Node start, int startRow){
    LinkedList<Node> queue = new LinkedList<Node>();
    LinkedList<Integer> rowQueue = new LinkedList<Integer>();

    queue.addFirst(start);
    rowQueue.addFirst((Integer)startRow);
    System.out.println( rowQueue.size() );      

    while (queue.size()!=0){            
        Node n = queue.poll();
        int row = rowQueue.poll().intValue(); //This is the line 33 in the error!
            /*Some remaining code which uses variables n and row. The thread of control does not reach here */

    }
  }

Below is the output:

Exception in thread "main" java.lang.NullPointerException
    at BFS.isConnected(LinkedListTest.java:33)
    at GraphsMain.main(GraphsMain.java:36)
1
ZERO

I am confused because the print statements execute after the error, and clearly I have written them the other way around. Is this a threading issue at all? I know that LinkedLists are not synchronized, but is that the problem here? should I be worried about it just for a simple implementation?

  • 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-11T09:57:04+00:00Added an answer on June 11, 2026 at 9:57 am

    Assume that you’ve some elements to your queue and nothing in your rowQueue. Then your logic in the while loop would work as follows:

    • Node n = queue.poll(); will get the first and only element from the queue and store in the n variable.

    • It would check if the size of rowQueue is zero or not. If it is, it would print a zero

    • However, and your problem lies in here, it would still try to poll() again, get a null this time and call intValue() on the returned value, resulting in the NullPointerException

    The issue as such lies with the following logic in your code:

            if(rowQueue.size() == 0){
                System.out.println("ZERO");
            }
            int row = rowQueue.poll().intValue(); 
    

    While you’re checking if rowQueue.size() is 0, and if true, you should not only print Zero but also ensure that you do not perform int row = rowQueue.poll().intValue();. So you should consider breaking out of the loop or something along those lines.

    So you should try something like this:

            if(rowQueue.size() == 0){
                System.out.println("ZERO");
                break; //This ensures that you come out of the loop
            }
            int row = rowQueue.poll().intValue(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble finding my physical log files. I'm using the java.util.logging classes. I
I'm new to regex in Java and am having trouble getting it to work
I'm using java.awt.Robot for integration tests of my Swing application, but I'm having trouble
I have a fairly simple maven-ized Java project, but am having trouble getting my
I'm having trouble understanding this bit of code in java.util.Arrays in openjdk. I'm curious
I have been having trouble while attempting to use the nextLine() method from java.util.Scanner.
Pretty new to Java programming, but I'm having trouble getting this to run. I
I am having trouble creating and using objects to type List<Integer> . When I
I am having trouble running a Java program with Windows Powershell 2.0. Any help
I'm having trouble transitioning to Java from C/C++ for my Telnet interface to some

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.