-1
public class node { 
         int data ;
         node next = null ;  

         //constructor           
         public node(int newdata){
         data = newdata ;
         next = null ;
         }  
         //-----------------------------TROUBLE FUNCTION----------------
         public void attach(int newdata){
             node list = this ;

             if(list.next == null)System.out.println("alas only 1 element given! last element so far " + list.data) ;                    
                 while(list.next != null){

                System.out.println("---------------------------------------") ;
                 list = list.next ;                                          
                 }
                 list.next = new node(newdata) ;                

         }

}

I m writing another function split() that splits the list into 2 lists: bigger_than int x and less_than x.

But initially these 2 lists are empty(bigger_than & less_than)

I m trying to attach to these 2 empty lists - this is the bug I can't resolve

public static void divide_list_around_x(int x, node list){
        node less_than = null ;
        node bigger_than = null ;       
        node current = list ;
        while(current != null){
            if( current.data < x){
                //less_than is null here
                less_than.attach(current.data);        
            }           
            else{
                //bigger list is null 
                bigger_than.attach(current.data) ;

            }
            current = current.next ;            
        }    
    }


    public static void main(String[] args) {

         //creating list, attach 1 by 1
         node first = new node( 4) ;                    
         first.attach( 78) ;
         first.attach( 5) ;
         first.attach( 51) ;
         first.attach( 157) ;
         first.attach( 3) ;                       

         divide_list_around_x(78,first) ; 
    }
JavaBeigner
  • 608
  • 9
  • 28
ERJAN
  • 23,696
  • 23
  • 72
  • 146
  • 1
    possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – jdphenix Aug 26 '14 at 05:01
  • @SotiriosDelimanolis at the call `less_than.attach(current.data)`, `less_than` is null – jdphenix Aug 26 '14 at 05:01
  • @ERJAN They aren't empty, but `null`. Very different and important here – jdphenix Aug 26 '14 at 05:08
  • but they are declared as local variables inside your divide_list_around_x() method. they will always be null. – eldjon Aug 26 '14 at 05:10

3 Answers3

0

You need to initialize the :

node less_than = new node(0) ;
node bigger_than = new node(0) ;  

and then attach elements based on your condition x.

eldjon
  • 2,800
  • 2
  • 20
  • 23
0

You could keep a len variable in your class Node:

public class Node {
    int len = 0;

Add a constructor:

 public Node()  {
 }

Modify the attach function:

public void attach(int newdata){

        if (len == 0) {
            data = newdata;
            next = null;
            len++;
            return;
        }
        Node list = this ;

        if(list.next == null)System.out.println("alas only 1 element given! last element so far " + list.data) ;                    
            while(list.next != null){

           System.out.println("---------------------------------------") ;
            list = list.next ;                                          
            }
            list.next = new Node(newdata) ;
            len++;
    }

Also instantiate less_than and bigger_than in function divide_list_around:

Node less_than = new Node();
Node bigger_than = new Node();    
mohit
  • 5,696
  • 3
  • 24
  • 37
0

TD;LR - The code was checking that current != null and then tried accessing current.data when data was null - so the result was NPE. There were a few other problems which we'll see as we go.


There are a few problems, most of them are "looking ahead" at a node with null data, and incorrect null checks. I took the liberty to change attach() (which had another NPE issue when trying to print list.data) to be more concise.

Further, a toString() method was added in order to provide clearer visibility for the whole process (see "output" section in the bottom):

public class Node {
    Integer data;
    Node next = null;

    public Node() {
    }

    public Node(Integer data, Node next) {
        this.data = data;
        this.next = next;
    }

    public void attach(int newData) {
        if (data == null) {
            data = newData;
            next = new Node();
        }
        else {
            next.attach(newData);
        }
    }

    public static void divideListAroundX(int x, Node next, Node lessThan, Node biggerThan) {
        Node current = next;
        while (current.data != null) {
            if (current.data < x) {
                //less_than is null here
                lessThan.attach(current.data);
            } else {
                //bigger next is null 
                biggerThan.attach(current.data);
            }
            current = current.next;
        }
    }

    @Override
    public String toString() {
        return "[data: " + data + ", next:" + next + "]";
    }


    public static void main(String[] args) {

        //creating next, attach 1 by 1
        Node first = new Node(4, new Node());
        first.attach(78);
        first.attach(5);
        first.attach(51);
        first.attach(157);
        first.attach(3);

        Node lessThan = new Node(); // these parameters should be initialized
        Node biggerThan = new Node(); // these parameters should be initialized
        divideListAroundX(78, first, lessThan, biggerThan);

        System.out.println("Less than:");
        System.out.println(lessThan);
        System.out.println("------------------------\n");
        System.out.println("Bigger than:");
        System.out.println(biggerThan);
    }
}

OUTPUT

Less than:
[data: 4, next:[data: 5, next:[data: 51, next:[data: 3, next:[data: null, next:null]]]]]
------------------------

Bigger than:
[data: 78, next:[data: 157, next:[data: null, next:null]]]

Comment
A few names were changed according to Java naming convention to camelCase (from sanek_case)

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129