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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:28:06+00:00 2026-06-18T01:28:06+00:00

The problem is at the partition() , which would read a LinkedList node and

  • 0

The problem is at the partition(), which would read a LinkedList node and compare the data value of that node with an input int number. the program throws NullPointerException at the compare statement node.data < x. I cannot figure it out, could anyone help me? Thanks a lot.

package Chapter2;
import java.util.*;
public class LinkedList2<E>{
    static class LinkedListNode<E>{
        E data;
        LinkedListNode<E> next;
    }

    private LinkedListNode<E> head;
    private LinkedListNode<E> tail;

    public LinkedList2(){
        this.head = new LinkedListNode<E>();
        this.tail = new LinkedListNode<E>();
        head.next = tail;
    }
    public void addLast(E e){
        LinkedListNode<E> node = new LinkedListNode<E>();
        tail.data = e;
        tail.next = node;
        tail = node;
    }

    public void print(){
        LinkedListNode<E> curr = head.next;
        while(curr.next != null){
            System.out.print(curr.data + " ");
            curr = curr.next;
        }
        System.out.println("");
    }

    public LinkedListNode<Integer> partition(LinkedListNode<Integer> node, Integer x){
        LinkedListNode<Integer> beforeStart = null;
        LinkedListNode<Integer> beforeEnd = null;
        LinkedListNode<Integer> afterStart = null;
        LinkedListNode<Integer> afterEnd = null;

        while(node!= null){
            LinkedListNode<Integer> next = node.next;
            node.next = null;
            System.out.println(node.data);
            if(node.data < x){
                if(beforeStart == null){
                    beforeStart = node;
                    beforeEnd = beforeStart;
                }else{
                    beforeEnd.next = node;
                    beforeEnd = node;
                }
            }else{
                if(afterStart == null){
                    afterStart = node;
                    afterEnd = afterStart;
                }else{
                    afterEnd.next = node;
                    afterEnd = node;
                }
            }
            node = next;
        }
        if(beforeStart == null){
            return afterStart;
        }
        beforeEnd.next = afterStart;
        return beforeStart;
    }

    public static void main(String[] args){

        LinkedList2<Integer> listInt = new LinkedList2<Integer>();
        listInt.addLast(5);
        listInt.addLast(8);
        listInt.addLast(1);
        listInt.addLast(3);
        listInt.addLast(6);

        listInt.print();

        System.out.println(listInt.head.next.data);
        listInt.partition(listInt.head.next, 4);

        listInt.print();
    }
}
  • 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-18T01:28:07+00:00Added an answer on June 18, 2026 at 1:28 am

    Quite simply, you are checking if the value of node.data is less than x. As node.data is an Integer type, it could quite possibly be null, meaning that this statement will throw a NullPointer. You could remedy this with a check in your while statement:

    while(node != null && node.data != null){
       /* Your Code */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have a table that prints out vertical but I would like it
Problem occured when i tried to display xml data that has been taken by
On a mountpoint (specifically: /var/data/) I have mounted a 3tb raid partition that stores
I have a little problem :(, I have a data base that reached 1000
For a problem that I'm working on right now, I would like a reasonably
Problem in MATLAB Code for solving desired 'n' number of simultaneous equations of the
Problem: I am trying to build a recursive tree using a function and data
We have a number of embedded systems requiring r/w access to the filesystem which
I've written a PL/SQL procedure that would benefit if indexes were first disabled, then
I Defined the following procedure which compiles correctly. http://pastebin.com/pBHFctHq When i call that procedure

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.