Home » Java » java – Alphabetized LinkedList Sorting-Exceptionshub

java – Alphabetized LinkedList Sorting-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to self implement a sorting method to put the objects in a self-created Doubly Linked List in alphabetical order. I am however receiving a null pointer exception on the part that iterates through the letters in case the first few letters are equal. What am I doing wrong?

 for(int i=0; i<n-1; i++) {
            for(int j=i+1; j<n; j++) {
                for(int k = 0; k<current.getName().length() && k<next.getName().length(); k++) {
                    int char1 = (int) current.getName().charAt(k);
                    int char2 = (int) next.getName().charAt(k);
                    if(char2 > char1) {
                        temp = current;
                        current = next;
                        next = temp;
                        break;
                    }
                }
                next = next.getRight();
            }
            current = current.getRight();
        }

Thank you very much.

How to&Answers: