Using start_item without uvm_do_with causes UVM_FATAL: attempting to start a null item

In the body of my sequence class I have this code below and it is failing with an error that says, “attempting to start a null item from sequence”


 virtual task body();
    
   //`uvm_do_with(req,{ (cmd == SPI_RD) && (addr == 15'h0454) && (num_rd == 1) && (sclk_count == 28) && (end_cmd_delay == 20) && (en_rdata_comp == 1'b0); })

   start_item(req);
     void'(req.randomize() with 
                           { (cmd == SPI_RD) && (addr == 15'h0454) && (num_rd == 1) && (sclk_count == 28) && (end_cmd_delay == 20) && (en_rdata_comp == 1'b1); });
                              
     req.data_chk_mask = Mask[7:0];
     req.data_queue = {};
     req.data_queue.push_front(ExpData[7:0]);
   finish_item(req);
 endtask

But when I remove the comment to enable the uvm_do_with, there is no problem.

I tried to research about uvm_do_with macro and I found out that inside that macro it is creating the item using uvm_create macro.
Can someone please verify this?
And do I need to create the req or just construct it in the function new?

In reply to Reuben:

Hi Reuben,

First you need to create the req and then you can use the rest of your code with `uvm_do_with commented.

In reply to jimik:

Yeah, I found 2 ways of fixing this:


// Create the item in the function new
req = packet::type_id::create("req");

// or use `uvm_create macro in the virtual task body()
`uvm_create(req)

As with anything in OOP, you must create the sequence item before you can do anything with it. The uvm_do macros are wrappers around

req = packet::type_id::create();
start_item(req);
req.randomize();
finish_item(req);

with various flavors for adding constraints to the randomization, disabling randomization, specifying the sequencer and other things. They also allow you to add pre_do and post_do callback methods that the macros call in between start_item() and finish_item().
As we’ve said many times, there is no reason to use the uvm_do macros. There are too many of them to remember, the callbacks add unnecessary complexity, and they hide too much detail that you really should be paying attention to, causing problems like the one you reported.
And there is certainly no reason you would ever want to use BOTH a uvm_do macro and start_item/finish_item.
In short, DO NOT USE THE `uvm_do MACROS.
See Are Macros Evil? DVCon 2011 Best Paper .