csanders8
csanders8

Reputation: 23

How can I get the file reader to not have the exception?

The assignment is to read each word of the file and count the frequency of each word using a map. Then to organize them using the given method sortByValue(Map map). I am confused as to how to get the file to read in correctly and assign integer values to the String keys. Help?

 import java.util.*;
 import java.io.*;
 public class WordFrequency {
     public static void main(String []args) throws IOException {

     File textFile = new File("book.txt"); 
     BufferedReader in = new BufferedReader(new FileReader(textFile));
     TreeMap<String, Integer> frequencyMap = new TreeMap<String, Integer>();
     String currentLine; 
     while ((currentLine = in.readLine()) != null) {
         currentLine = currentLine.toLowerCase();


     }

     StringTokenizer tokenizer = new StringTokenizer(currentLine, " \t\n\r\f.,;:!?'");

     while (tokenizer.hasMoreTokens()) { 
         final String currentWord = tokenizer.nextToken(); Integer frequency = frequencyMap.get(currentWord);
         if (frequency == null) { frequency = 0; } frequencyMap.put(currentWord,frequency + 1);

     }
     ArrayList map;
     map = sortByValue(frequencyMap);

     for(int i= 0; i < 20; i++)
         System.out.println(map.get(i));

     }

// Sort a map with its values in ascending order
public static ArrayList < Map . Entry > sortByValue ( Map map )
{
ArrayList < Map . Entry > a = new ArrayList ( map . entrySet () );
// sort by providing my own comparator
Collections . sort (a , new Comparator ()
{
public int compare ( Object o1 , Object o2 )
{
Map . Entry e1 = ( Map . Entry ) o1 ;
Map . Entry e2 = ( Map . Entry ) o2 ;
return (( Comparable ) e1 . getValue () ). compareTo ( e2 . getValue () ) ;
}
}) ;
return a;
}


 }

The IOException is

  Exception in thread "main" java.lang.NullPointerException
at java.util.StringTokenizer.<init>(Unknown Source)
at java.util.StringTokenizer.<init>(Unknown Source)
at WordFrequency.main(WordFrequency.java:20)

Upvotes: 0

Views: 495

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347234

Take a look at while ((currentLine = in.readLine()) != null) {

When this loop exits, currrentLine will be null, this was the requirement of the loop.

What you should consider doing is moving the processing of currentLine within the while loop you are using to read the file...

For example...

while ((currentLine = in.readLine()) != null) {
     currentLine = currentLine.toLowerCase();
     StringTokenizer tokenizer = new StringTokenizer(currentLine, " \t\n\r\f.,;:!?'");

     while (tokenizer.hasMoreTokens()) { 
         final String currentWord = tokenizer.nextToken(); 
         Integer frequency = frequencyMap.get(currentWord);
         if (frequency == null) { 
             frequency = 0; 
         } 
         frequencyMap.put(currentWord,frequency + 1);
     }

 }

Upvotes: 1

Fuzzy Analysis
Fuzzy Analysis

Reputation: 3188

Just add another String to get currentLine before it become null, e.g. see entireText below:

package org.fuzzyanalysis.demo;


import java.util.*;
import java.io.*;

public class WordFrequency {

    public static void main(String []args) throws IOException {

         File textFile = new File("book.txt"); 
         BufferedReader in = new BufferedReader(new FileReader(textFile));
         TreeMap<String, Integer> frequencyMap = new TreeMap<String, Integer>();
         String currentLine; 
         String entireText = "";
         while ((currentLine = in.readLine()) != null) {

             currentLine = currentLine.toLowerCase();            
             entireText += currentLine;

         }

         StringTokenizer tokenizer = new StringTokenizer(entireText, " \t\n\r\f.,;:!?'");

         while (tokenizer.hasMoreTokens()) { 
             final String currentWord = tokenizer.nextToken(); Integer frequency = frequencyMap.get(currentWord);
             if (frequency == null) { frequency = 0; } frequencyMap.put(currentWord,frequency + 1);

         }
         ArrayList map;
         map = sortByValue(frequencyMap);

         for(int i= 0; i < 20; i++)
             System.out.println(map.get(i));

         }

    // Sort a map with its values in ascending order
    public static ArrayList < Map . Entry > sortByValue ( Map map )
    {
        ArrayList < Map . Entry > a = new ArrayList ( map . entrySet () );
        // sort by providing my own comparator
        Collections . sort (a , new Comparator ()
        {
            public int compare ( Object o1 , Object o2 )
            {
                Map . Entry e1 = ( Map . Entry ) o1 ;
                Map . Entry e2 = ( Map . Entry ) o2 ;
                return (( Comparable ) e1 . getValue () ). compareTo ( e2 . getValue () ) ;
            }
        }) ;
    return a;
    }


}

Upvotes: 0

Blub
Blub

Reputation: 3822

The problem is here:

 while ((currentLine = in.readLine()) != null) {
     currentLine = currentLine.toLowerCase();
 }

You basically just go through the file until the end. Then null is saved in currentLine and you call the Tokenizer with null ---> NullPointerException.

Add a new variable:

 String text = "";
 while ((currentLine = in.readLine()) != null) {
     text += currentLine.toLowerCase();
 }

Then use text for the Tokenizer.

Upvotes: 0

Related Questions