对于Node类,如何实现是空的

时间:2017-12-01 20:42:30

标签: java nullpointerexception tree avl-tree

我有以下代码来表示树的节点:

public class Node {

/**
 * All the attributes.
 * @param value: the generic value assigned to this node.
 * @param leftNode: the left leaf of this node.
 * @param rightNode: the right leaf of this node.
 */
private Map map;
private Node leftNode;
private Node rightNode;

/**
 * Constructor Node. 
 * Creates a node with it's leafs being null.
 * @param map 
 */
public Node(Map map){
    this(map, null, null);
}


/**
 * Constructor Node.
 * Creates a node with it's leafs assigned accordingly.
 * @param value
 * @param left_node
 * @param right_node 
 */
public Node(Map value, Node left_node, Node right_node){
    this.leftNode = left_node;
    this.rightNode = right_node;
    this.map = value;
}

/**
 * Returns the value of the node.
 * @return 
 */
public Map getValue(){
    return this.map;
}

/**
 * Returns the left child of the node.
 * @return 
 */
public Node getLeftChild(){
    return this.leftNode;
}

/**
 * Returns the right child of the node.
 * @return 
 */
public Node getRightChild(){
    return this.rightNode;
}

/**
 * Removes the value of the node and returns the value.
 * @return 
 */
public Map removeValue(){
    Map temp = this.map;
    this.map = null;
    return temp;
}

/**
 * Removes the left child of the node and returns the child node.
 * @return 
 */
public Node removeLeftChild(){
    Node temp = this.leftNode;
    this.leftNode = null;
    return temp;
}

/**
 * Removes the right child of the node and returns the child node.
 * @return 
 */
public Node removeRightChild(){
    Node temp = this.rightNode;
    this.rightNode = null;
    return temp;
}

/**
 * Sets the value of the node to a given value.
 * @throws IllegalArgumentException if the value is empty
 * @param map 
 */
public void setValue(Map map){
    if (this.map == null)
        throw new IllegalArgumentException("The value is empty.");
    this.map = map;
}

/**
 * Sets the left child of the node to a given node.
 * @throws IllegalArgumentException if the node is empty
 * @param node 
 */
public void setLeftChild(Node node){
    if (node.isEmpty())
        throw new IllegalArgumentException("The node is empty.");
    this.leftNode = node;
}

/**
 * Sets the right child of the node to a given node.
 * @throws IllegalArgumentException if the node is empty
 * @param node 
 */
public void setRightChild (Node node){
    if (node.isEmpty())
        throw new IllegalArgumentException("The node is empty.");
    this.rightNode = node;
}

/**
 * Checks if the following Node is empty.
 * Node can be null or it's value being null for it to be empty.
 * @return 
 */
public boolean isEmpty(){
    if (this == null){
        return true;
    }
    else return false;
}
}

但是,当我尝试运行以下代码(我已经测试过其他情况并且我的树工作)时,这会给我一个NullPointerException。

主要

{...
Node test2_2 = new Node(new Map<> (5, 2));
System.out.println("Object left child is: " + test2_2.getLeftChild().isEmpty());
}

线程“main”java.lang.NullPointerException中的异常     在MainPackage.Main.main(Main.java:35) C:\ Users \ mehrz \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml:53:Java返回:1

Anny建议?

1 个答案:

答案 0 :(得分:0)

在构造函数中,为Node属性设置null,而不是通过以下方式调整构造函数:

/**
 * Constructor Node. 
 * Creates a node with it's leafs being null.
 * @param map 
 */
public Node(Map map){
    this(map, new Node(), new Node());
}