1

Please help i have an error I just cannot figure out

Exception

Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 97
    at java.lang.String.charAt(String.java:658)
    at Autocomplete$TrieAutocomplete.topMatches(Autocomplete.java:440)

Code

      public Iterable<String> topMatches(String prefix, int k) {
        if (prefix == null) {
        throw new NullPointerException("Prefix must not be null");
        }
        Node.ReverseSubtreeMaxWeightComparator comp = new Node.ReverseSubtreeMaxWeightComparator();
        Node curr = myRoot;
        for (int i=0; i < prefix.length(); i++) {
           if (curr.children.containsKey(prefix.charAt(i))) {
              curr = curr.getChild(prefix.charAt(i));
           }
           else {
              return new ArrayList<String>();
           }
        }
        ArrayList<Node> wordsFound = new ArrayList<Node>();
        PriorityQueue queue = new PriorityQueue();
        double queuemax = myRoot.mySubtreeMaxWeight;
        while ((wordsFound.size() < k) || (wordsFound.get(k-1).mySubtreeMaxWeight > queuemax)) {
           queuemax = curr.mySubtreeMaxWeight;
           ArrayList<Node> children = new ArrayList<Node>();
           for (Character ch : curr.children.keySet()) {
            //The next line is line 440  
              children.add(curr.getChild(prefix.charAt(ch)));
           }
           Collections.sort(children, comp);
           for (Node n : children) {
              queue.add(n);
           }   
           if (curr.isWord == true) {
              wordsFound.add(curr);
              Collections.sort(wordsFound, comp);
           }
           curr = (Node)queue.poll();
           if (curr == null) {
              break;
           }
           queuemax = curr.mySubtreeMaxWeight;
        }
        ArrayList<String> wordList = new ArrayList<String>();
        for (int i=0; i < k; i++) {
           wordList.add(wordsFound.get(i).getWord());
        }
       return wordList;
    }
stdunbar
  • 16,263
  • 11
  • 31
  • 53
Evan Kiser
  • 31
  • 3

1 Answers1

1

This means you don't have that char:

for (Character ch : curr.children.keySet()) {
            //The next line is line 440  
              children.add(curr.getChild(prefix.charAt(ch)));
           }

And the char is this: 97 a

So debug and check that prefix has the letter a in it

ACV
  • 9,964
  • 5
  • 76
  • 81