0

I create a class Node which contains an int variable val When I try to get the val after I pop it from stack, the compiler throw a NullPointerException. Does anyone knows how to fix this?

public class Node{
  int val;
  Node left;
  Node right;
  Node(int x) {val = x;}
}


Node root = new Node(6);
Stack s = new Stack();
s.push(root);
Node node = (Node)s.pop();
System.out.println(node.val);
Lingyao
  • 23
  • 4

0 Answers0