title: collections.sort(list, comparator) using comparator DEBUG
body: <p>I am trying to use  <code>collections.sort(list, comparator)</code> to sort a <code>&lt;Map.Entry&lt;String, Integer&gt;&gt;</code> object. The code compiles correctly, but I get the following error during runtime. This is my first time using a comparator and collections.sort, but cannot seem to find what is causing the problem in the code.  The error is saying that <code>Collections.sort(list, new WordCountComparator() );</code> is the cause of the problem.  Did I define the comparator class correctly?  Am I using the > generic type correctly? </p>
  
  <p>Initially I ran the code using these commands on the CLI to complile, create jar, and run:</p>
  
  <pre><code>javac MP1.java
  jar cfe MP1.jar MP1 MP1.class
  java -jar MP1.jar 1
  </code></pre>
  
  <p>And receive this error:</p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: MP1$WordCountComparator
      at MP1.process(MP1.java:112)
      at MP1.main(MP1.java:132)
  Caused by: java.lang.ClassNotFoundException: MP1$WordCountComparator
      at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
      ... 2 more
  </code></pre>
  
  <p>I tried running the code without creating a jar file by using these commands:</p>
  
  <pre><code>javac MP1.java
  java MP1 1
  </code></pre>
  
  <p>and this is the error that appears</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
  at java.lang.Integer.compareTo(Integer.java:1003)
  at MP1$WordCountComparator.compare(MP1.java:15)
  at MP1$WordCountComparator.compare(MP1.java:12)
  at java.util.TimSort.countRunAndMakeAscending(TimSort.java:325)
  at java.util.TimSort.sort(TimSort.java:203)
  at java.util.TimSort.sort(TimSort.java:173)
  at java.util.Arrays.sort(Arrays.java:659)
  at java.util.Collections.sort(Collections.java:217)
  at MP1.process(MP1.java:112)
  at MP1.main(MP1.java:132)
  </code></pre>
  
  <p>The code for the entire MP1.java file is :</p>
  
  <pre><code>import java.lang.reflect.Array;
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  import java.util.*;
  import java.io.*;
  
  
  public class MP1 {
  
  
  private static class WordCountComparator implements Comparator&lt;Map.Entry&lt;String, Integer&gt;&gt; {
      public int compare( Map.Entry&lt;String, Integer&gt; o1, Map.Entry&lt;String, Integer&gt; o2 )
      {
          if(o2.getValue().compareTo( o1.getValue()) != 0) {
              return o2.getValue().compareTo(o1.getValue());
          } 
          else {
              return o1.getKey().compareTo(o2.getKey());
          }
      }
  }
  
  Random generator;
  String userName;
  String inputFileName;
  String delimiters = " \t,;.?!-:@[](){}_*/";
  String[] stopWordsArray = {"i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your", "yours",
          "yourself", "yourselves", "he", "him", "his", "himself", "she", "her", "hers", "herself", "it", "its",
          "itself", "they", "them", "their", "theirs", "themselves", "what", "which", "who", "whom", "this", "that",
          "these", "those", "am", "is", "are", "was", "were", "be", "been", "being", "have", "has", "had", "having",
          "do", "does", "did", "doing", "a", "an", "the", "and", "but", "if", "or", "because", "as", "until", "while",
          "of", "at", "by", "for", "with", "about", "against", "between", "into", "through", "during", "before",
          "after", "above", "below", "to", "from", "up", "down", "in", "out", "on", "off", "over", "under", "again",
          "further", "then", "once", "here", "there", "when", "where", "why", "how", "all", "any", "both", "each",
          "few", "more", "most", "other", "some", "such", "no", "nor", "not", "only", "own", "same", "so", "than",
          "too", "very", "s", "t", "can", "will", "just", "don", "should", "now"};
  
  void initialRandomGenerator(String seed) throws NoSuchAlgorithmException {
      MessageDigest messageDigest = MessageDigest.getInstance("SHA");
      messageDigest.update(seed.toLowerCase().trim().getBytes());
      byte[] seedMD5 = messageDigest.digest();
  
      long longSeed = 0;
      for (int i = 0; i &lt; seedMD5.length; i++) {
          longSeed += ((long) seedMD5[i] &amp; 0xffL) &lt;&lt; (8 * i);
      }
  
      this.generator = new Random(longSeed);
  }
  
  Integer[] getIndexes() throws NoSuchAlgorithmException {
      Integer n = 10000;
      Integer number_of_lines = 50000;
      Integer[] ret = new Integer[n];
      this.initialRandomGenerator(this.userName);
      for (int i = 0; i &lt; n; i++) {
          ret[i] = generator.nextInt(number_of_lines);
      }
      return ret;
  }
  
  public MP1(String userName, String inputFileName) {
      this.userName = userName;
      this.inputFileName = inputFileName;
  }
  
  public String[] process() throws Exception {
      String[] ret = new String[20];
  
      File file = new File(this.inputFileName);
      Scanner scanner = new Scanner(file);
      String[] lines = new String[50000];
  
      int i = 0;
      while(scanner.hasNextLine()){
          lines[i] = scanner.nextLine();
          i++;
      }
      Integer[] indices = getIndexes();
  
      String[] records = new String[10000];
  
      //ArrayList&lt;String&gt; words = new ArrayList&lt;String&gt;();
      Map&lt;String, Integer&gt; wordCount = new HashMap&lt;String, Integer&gt;();
  
      i = 0;
      for(Integer index:indices){
  
          records[i] = lines[index].toLowerCase().trim();
          StringTokenizer tokenOfString = new StringTokenizer(records[i], this.delimiters);
          i++;
  
          while(tokenOfString.hasMoreTokens()){
  
              String token = tokenOfString.nextToken();
  
              if(!Arrays.asList(stopWordsArray).contains(token)){
  
                  if(wordCount.get(token) == null) {
  
                      wordCount.put(token,1);
                  }
                  else{
                      wordCount.put(token, wordCount.get(token + 1));
                  }
  
                  }
              }
          }
      List&lt;Map.Entry&lt;String, Integer&gt;&gt; list = new ArrayList&lt;Map.Entry&lt;String, Integer&gt;&gt;(wordCount.entrySet());
      Collections.sort(list, new WordCountComparator() );
  
      for (i = 0; i &lt; 20; i++ ){
  
          ret[i] = list.get(i).getKey() + "\t" + Integer.toString(list.get(i).getValue());
  
      }
  
      return ret;
  
  }
  
  public static void main(String[] args) throws Exception {
      if (args.length &lt; 1){
          System.out.println("MP1 &lt;User ID&gt;");
      }
      else {
          String userName = args[0];
          String inputFileName = "/home/unknown/git/cloudapp-mp1/input.txt";
          MP1 mp = new MP1(userName, inputFileName);
          String[] topItems = mp.process();
          for (String item: topItems){
              System.out.println(item);
          }
      }
  }
  </code></pre>
  
  <p>}</p>