I am implementing Selection sort in doubly - linked list.
I have to sort list by surnames by finding smallest element and inserting it into beginning of the list
But there are some troubles when I run my program I have NIL exception in Sort Method in while loop.
Help will be appreciated.
Thanks.
It's whole app so you can compile it and run.

public class LinkedList {
    public Node first;
    public Node last;

    public LinkedList() {
        first = null;
        last = null;
    }

    public void addFirst(Student student) {
        Node f = first;
        Node newNode = new Node(student);
        first = newNode;
        if (f == null) last = newNode;
        else {
            f.previous = newNode;
            newNode.next = f;
        }
    }

    public void addLast(Student student) {
        Node l = last;
        Node newNode = new Node(student);
        last = newNode;
        if (l == null) first = newNode;
        else {
            l.next = newNode;
            newNode.previous = l;
        }
    }

    public void display() {
        Node current = first;
        while (current != null) {
            System.out.print(current.student.name + "\b");
            System.out.print(current.student.surname + "\b");
            System.out.println(current.student.educationType);
            current = current.next;
        }
    }

    public Node findSmallest(Node startNode) {
        Node smallest = startNode;
        while (startNode.next != null) {
            if (smallest.student.surname.compareToIgnoreCase(startNode.next.student.surname) > 1)
                smallest = startNode.next;
            else startNode = startNode.next;
        }
        return smallest;
    }

    public void Sort() {
        LinkedList newList = new LinkedList();
        Node current = first;

        while (current.next != null) {
            Node smallest = findSmallest(current);
            newList.addLast(smallest.student);
            delNode(smallest);
            current = current.next;

        }
        first = newList.first;
        last = newList.last;
    }

    public void delNode(Node toDel) {
        if (toDel.previous == null) {
            toDel.next.previous = null;
            first = toDel.next;
            return;
        }

        if (toDel.next == null) {
            toDel.previous.next = null;
            last = toDel.previous;
            return;
        }
        toDel.previous.next = toDel.next;
        toDel.next.previous = toDel.previous;
    }

}
public class Node {
    public Student student;

    public Node next;
    public Node previous;

    public Node(Student student) {
        this.student = student;
    }
}
public class Student {
    public String name;
    public String surname;
    public String educationType;

    static public Student createStudent() {
        Student student = new Student();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter student's name:");
            student.name = br.readLine();
            System.out.println("Enter surname:");
            student.surname = br.readLine();
            System.out.println("Enter educational type:");
            student.educationType = br.readLine();
        } catch (IOException e) {
            throw new NotImplementedException();
        }
        return student;
    }
}

Edited 5 Years Ago by greenq: n/a

Line 45 you are relying on good luck with your >1.
The JavaDoc says

Returns:
a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.

The actual source code shows that it returns either the difference ion length of the strings, of the numeric difference between the first non-matching characters. Either way it will sometimes return +1, and you will take the wrong path.
This may or may not be why you get your null, but it would be a good idea to fix it anyway.

The problem is from the bug you introduce in line 58 & 59 as well.

When you find the smallest node, you add the node to the new list WITHOUT copying but use the object reference (look at your Node class line 8). Then you modify the node you just added using delNode() method. As a result, the link is broken and becomes unstable. Finally, you will eventually get a Null value from it.

Edited 5 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.