0

I'm trying to read in a list of words a file using BufferedReader and determine if any are duplicates, and if they are, to not print them. I know that there is a SuperClass called "UniqueLineReader" that can check the occurrence of duplicate lines for you, and something else called Linked HashSet. However, I haven't learned either of these in class, so I'm trying not to use them.

Anyways, the e.getMessage() is printing "null", which I don't understand, because the file obviously isn't and I'm checking that it isn't. Do I have wrong placement of variables?

try {
 inFile = new BufferedReader(new FileReader(inputName));
 outFile = new PrintWriter(new FileWriter(outputName));

 while((nextLine = inFile.readLine())!= null){

   if(indexOf(nextLine) == -1){
      insert(nextLine);

      outFile.println(nextLine);}
   else{
     System.out.println(nextLine + " has been seen before!");
       }
    }

  inFile.close();
 }//try
 catch(Exception e){
 System.out.println(e.getMessage());
 }//catch

My indexOf method:

private int indexOf(String newWord){

for(int i = 0; i < numWords; i++){
  if(arrayStrings[i].equals(newWord)){
    return i;
  }
}

return -1;

}//indexOf

stacktrace

`java.lang.NullPointerException at A2Q2.indexOf(A2Q2.java:58) at A2Q2.removeDuplicateLines(A2Q2.java:79) at TestA2Q2.main(TestA2Q2.java:10)

user180708
  • 407
  • 1
  • 4
  • 16
  • 5
    Try using `e.printStackTrace()` instead of `System.out.println(e.getMessage())` and make sure it is `Exception` related to files. – dpassy Feb 27 '15 at 21:17
  • 2
    null usually means that it is NPE (NullPointerException) – Iłya Bursov Feb 27 '15 at 21:17
  • @eldest, It's saying that I have a copious amount of nullpointerexceptions. In this method specifically, it says that there's one at `if(indexOf(nextLine) == -1)`, but I'm not sure what that means, because here I'm just checking the index of the array, and seeing if it's existed before. – user180708 Feb 27 '15 at 21:27
  • @user180708 please provide full stacktrace here. and the method indexOf too – dpassy Feb 27 '15 at 21:40
  • @eldest, Okay, I added the first part of my stacktrace, and my indexOf method – user180708 Feb 27 '15 at 21:52
  • @user180708 i see you are writing binary search, I would suggest you to use `Collections.binarySearch()` method. Example here: http://www.tutorialspoint.com/java/util/collections_binarysearch_comparable.htm – dpassy Feb 27 '15 at 21:57
  • You manage the `arrayStrings` manually as a `String[ ]`. Since you do the binary search on the full length of the array, are you sure you are not checking on null elements ? It would be better to use a `ArrayList` since it grows dynamically and `size()` will give you the actual number of elements instead of the total capacity. – T.Gounelle Feb 27 '15 at 22:17
  • @Lashane. No it doesn't. It just means the message is null. This can happen with a wide variety of exceptions. – user207421 Feb 27 '15 at 22:22
  • Please provide the full stack trace. The first part is not always the one you need to diagnose an error, in the bottom part you may have "caused by ..." which will be the first exception thrown. – Daniel Feb 27 '15 at 23:50
  • @eldest I updated my indexOf method. I realized I didn't have to do a binary search. Just check if the given word already equals one in the array, – user180708 Feb 28 '15 at 00:06
  • @Mondkin, I added as much as the stack trace as I could. It doesn't say "caused by" unfortunartely – user180708 Feb 28 '15 at 00:07

1 Answers1

0

According to the stack trace, the problem is at line 58 of file A2Q2.java, at that line you are using this:

 arrayStrings[i].equals(...)

Which means that either arrayStrings is null, or arrayStrings[i] is null.

Adding a simple System.out.println before that line (or using a debugger) will show you exactly what is wrong.

BTW: I see you are using a fixed size array instead of an ArrayList to save the lines, I recommend the latter in this case as you don't need to specify the maximum number of lines beforehand.

Daniel
  • 21,933
  • 14
  • 72
  • 101