Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
994 views
in Technique[技术] by (71.8m points)

class - Null Object Access error in system verilog

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

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 < 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("Binary search tree after insertion:"); 
    bs.inorder_traverse(bs.root);
  end
endmodule

Error message: 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The null error is thrown from the following line.

    newNode.data = nd.data;

I don't see neither newNode nor nd objects being constructed by calling new() either in constructor of bin_search class or in the insert() method itself. You need to create objects of classes before accessing their members or methods.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...