
-----------------------------------
xxbow-pkerxx
Tue Jan 02, 2007 7:36 pm

String Sorting HELP
-----------------------------------
Im a newer java programmer and i need major help the problem is that i keep getting an error (using drjava)

the error 

NullPointerException: 
  at java.lang.String.compareTo(Unknown Source)
  at insert1_1000.insertSort(insert1_1000.java:53)
  at insert1_1000.main(insert1_1000.java:32)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)

the code:


import java.io.*;

class insert1_1000 {
  public static void main (String [] args){
    
    FileReader fileIn;
    BufferedReader buffer;
   
    
    int size=6000; // array size
    String [] array = new String [size]; // array
    int time;
    
    try{ // read file in to array
      fileIn = new FileReader ("words1.txt");
      buffer = new BufferedReader (fileIn);
      
      for (int i=0; i < size; i++){
      
        array [i]= buffer.readLine ();
         fileIn.close ();
      }
     
    }catch (IOException err){};
    
 
    time=insertSort (array); // send array in to methord
    
    System.out.println ("it took "+time);

    
  }// end main
  
  public static int insertSort (String [] array) {
    String temp;//creates a variable to store the number to be sorted
    int current=0;//creates a variable to store the current element being sorted in the array
    int size = 1000;
    
    
     long startTime = System.currentTimeMillis ();// start timer
     
    for (int i=0; i0 && array[current-1].compareTo (temp) > 0 ) {
        array[current]=array[current-1];//shifts the next number to current spot
        current--;//moves to next current
      }
      array[current]=temp;//puts the number to be sorted into its element
    }
    
    int elapsedTime= (int)(System.currentTimeMillis()-startTime);
    
    return elapsedTime;
    
  }
  
}// end class[/code]

-----------------------------------
xxbow-pkerxx
Tue Jan 02, 2007 7:38 pm

please reply
-----------------------------------
please reply to me here or via e-mail

Taha1234567890@hotmail.com

-----------------------------------
McKenzie
Wed Jan 03, 2007 9:15 am


-----------------------------------
Attach the data file.  I tried putting empty strings in the array and it stops it from crashing.  I'd guess you don't have 6000 lines in your file, or the file is not in the same folder. 

I find the placement of 
         fileIn.close ();
odd, but It won't cause your problem.

-----------------------------------
xxbow-pkerxx
Wed Jan 03, 2007 2:39 pm


-----------------------------------
heres the data files 

if testing 3 of them

-----------------------------------
HellblazerX
Wed Jan 03, 2007 8:12 pm


-----------------------------------
My guess would be in this line:
while (current>0 && array[current-1].compareTo (temp) > 0 ) { 
Since you started with current being 0, current - 1 will give you -1, so you'll be comparing with something that doesn't exist.  I think Java will check all conditions inside the parentheses, regardless of whether the first condition is true or not.

-----------------------------------
McKenzie
Thu Jan 04, 2007 9:14 pm


-----------------------------------
Oops,

The FileIn.close() is causing the problem.  Your program will read in the first 767 lines then die with it there. Just place it after your for loop.  I haven't even looked at your sort, so it may or may not work.
