0

So I'm running a program that does various things to a String array. One of them is a inserting a string inside the array and sorting it. I'm able to use the sort method, but when I attempt to insert a string and then sort it I get a NullPointerException. This is the code:

 import java.util.Scanner; import java.io.*; public class List_Driver { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); int choice = 1; int checker = 0; String [] words = new String[5]; words[0] = "telephone"; words[1] = "shark"; words[2] = "bob"; ListWB first = new ListWB(words); int menu = uWB.getI("1. Linear Seach\n2. Binary Search\n3. Insertion in Order\n4. Swap\n5. Change\n6. Add\n7. Delete\n8. Insertion Sort\n9. Quit\n"); switch(menu) { //other cases case 3: { String insert = uWB.getS("What term are you inserting?"); first.insertionInOrder(insert); first.display(); }//not working break; }//switch menu }//main }//List_Driver 

uWB is a basic util driver. It doesn't have any problems. This is the ListWB file itself:

 public class ListWB { public void insertionSort() { for(int i = 1; i < size; i++) { String temp = list[i]; int j = i; while(j > 0 && temp.compareTo(list[j-1])<0) { list[j] = list[j-1]; j = j-1; } list[j] = temp; } } public void insertionInOrder(String str) { insertionSort(); int index = 0; if(size + 1 <= list.length) { while(index < size && str.compareTo(list[index])>0) index++; size++; for (int x = size -1; x> index; x--) list[x] = list[x-1]; list[index] = str; } else System.out.println("Capacity Reached"); }//insertioninorder }//ListWB 

How would I fix this?

3
  • 3
    Please post the exception stacktrace Commented Apr 9, 2014 at 21:07
  • possible duplicate of What is a Null Pointer Exception? Commented Apr 9, 2014 at 21:12
  • 1
    You seem to have ignored your stack trace, which tells you precisely where the error occurs, then combined with the definition of a NullPointerException, it would have been straightforward to determine what was null. Then you look at your code and determine why it was null, and either make that not be the case, or handle it. Do not disregard the stack trace. Commented Apr 9, 2014 at 21:14

1 Answer 1

1

You have an array of 5 Strings, but only 3 of them initialized. The rest points to null (because you did not initialize them):

 String [] words = new String[5]; words[0] = "telephone"; words[1] = "shark"; words[2] = "bob"; words[3] = null; words[4] = null; 

The first line only initializes the array itself, but not the containing objects.

But the insert iterates over all 5 elements. And temp is null, when i is 3. So the statement temp.compareTo throws an NullPointerException.

 for(int i = 1; i < size; i++) { String temp = list[i]; int j = i; while(j > 0 && temp.compareTo(list[j-1])<0) 

Solution: Also check temp for null in the while loop. Or do not use a string array at all but a auto-resizable data structure list java.util.ArrayList.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.