用Java创建Trie数据结构 - NullPointerException

时间:2015-09-05 06:38:28

标签: java nullpointerexception

package trie;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

class Node {
    private String word;
    private HashMap<Character, Node> nodes;

    public List<String> getAll() {
        List<String> x = new ArrayList<String>();
        for (HashMap.Entry<Character, Node> entry : nodes.entrySet()) {
            Character key = entry.getKey();
            Node node = entry.getValue();
            if (node.word != null)
                x.add(node.word);
            x.addAll(node.getAll());
        }
        return x;
    }

    public String toString() {
        return this.word;
    }

    public HashMap<Character, Node> getNodes() {
        return this.nodes;
    }

    public boolean insert(String word, int stringPos) {
        this.word = word;
        this.nodes = new HashMap<Character, Node>();

        Character currentLetter = word.charAt(stringPos);
        if (nodes.containsKey(currentLetter)) {
            nodes.put(currentLetter, new Node());
        }

        if (stringPos + 1 == word.length()) {
            nodes.get(currentLetter).word = word;
        } else {
            nodes.get(currentLetter).insert(word, stringPos);
        }
        return true;
    }

    public List<String> getAllWithPrefix(String prefix, int stringPos) {
        List<String> x = new ArrayList<String>();
        for (HashMap.Entry<Character, Node> entry : nodes.entrySet()) {
            Character key = entry.getKey();
            Node node = entry.getValue();
            if (stringPos >= prefix.length() || key.equals(prefix.charAt(stringPos))) {
                if (node.word != null) {
                    x.add(node.word);
                }

                if (node.nodes.size() == 0) {
                    if (stringPos + 1 <= prefix.length()) {
                        x.addAll(node.getAllWithPrefix(prefix, stringPos + 1));
                    } else {
                        x.addAll(node.getAllWithPrefix(prefix, stringPos));
                    }
                }
            }
        }
        return x;
    }
}

public class Trie {
    private Node root;
    public Trie() {
        this.root = new Node();
    }

    public void insert(String word) {
        root.insert(word, 0);
    }

    public List<String> getAll() {
        return root.getAll();
    }

    public List<String> getAllWithPrefix(String prefix, int stringPos) {
        return root.getAllWithPrefix(prefix, stringPos);
    }

    public static void main(String[] args) {
        Trie trie = new Trie();
        trie.insert("java");
    }
}

输出:

Exception in thread "main" java.lang.NullPointerException
    at trie.Node.insert(Trie.java:47)
    at trie.Trie.insert(Trie.java:83)
    at trie.Trie.main(Trie.java:96)

我在运行程序后立即获得NullPointerException。我知道当对象指向null时会发生此异常。但是在Trie类的构造函数中,我创建了一个Node的新对象,为什么会发生这种情况呢?

2 个答案:

答案 0 :(得分:2)

问题似乎是这一行:

nodes.get(currentLetter).insert(word, stringPos);

nodes.get(currentLetter)返回null,因为地图为空。

您需要先插入HashMap。

答案 1 :(得分:1)

您的插入方法,注释:

public boolean insert(String word, int stringPos) {
    this.word = word;
    this.nodes = new HashMap<Character, Node>(); // <-- nodes is now empty

    Character currentLetter = word.charAt(stringPos);
    if (nodes.containsKey(currentLetter)) { // <-- can never be true since nodes is empty
        nodes.put(currentLetter, new Node()); // <-- will never be executed
    }

    if (stringPos + 1 == word.length()) {
        nodes.get(currentLetter).word = word; // <-- nodes is empty, so get() returns null causing NPE
    } else {
        nodes.get(currentLetter).insert(word, stringPos); // <-- nodes is empty, so get() returns null causing NPE
    }
    return true;
}