title: Null Object Access error in system verilog
body: <p>I am trying to perform binary tree insertion and in order traversal in System Verilog using OOPs concepts. I am getting error that the object is being used before creating it. Please take a look at the code and help me if someone find any mistakes</p>
  <pre><code>class node;
    byte data;
    node left;
    node right;
    function new();
      this.data = data;
      this.left = null;
      this.right = null;
    endfunction
  endclass
  class bin_search extends node;
    node newNode;
    node nd,root,current,parent;
    byte in_data;
    function new();
      super.new();
      this.in_data = in_data;
    endfunction
  function automatic insert(in_data);
      newNode.data = nd.data;
      if(root.data == null) begin
        root = newNode;
        return;
      end
      else begin
        current = root;
        parent = null;
      end
      forever begin
        parent = current;
        if(in_data &lt; current.data) begin
      current = current.left;
      if(current.left == null) begin
        parent.left = newNode;
        return;
      end
      end
    else begin
      current = current.right;
      if(current.right == null) begin
        parent.right = newNode;
        return;
      end
      end
      end
    endfunction
    function automatic inorder_traverse(node node_tr);
      //using nodes here
    endfunction
  endclass
  
  module binary;
    node NODE;
    bin_search bs;
    byte ins;
  
    initial begin
      NODE = new;
      bs = new;
      bs.insert(50);
      $display(&quot;Binary search tree after insertion:&quot;); 
      bs.inorder_traverse(bs.root);
    end
  endmodule
  </code></pre>
  <p><strong>Error message:</strong>
  Error-[NOA] Null object access
  binary.sv, 28
  The object at dereference depth 1 is being used before it was
  constructed/allocated.
  Please make sure that the object is allocated before using it.</p>