I hava a function in a contract:

  function getLastBidding() public view returns (Bidding) {
    return biddings[biddings.length-1];

The problem is when the biddings array is empty, it doesn't work, because I can't access index -1. So, I would like to return some sort of null when the array is empty. What can I return instead?

2 Answers 2


Your best bet is to probably require(bidding.length > 0); before returning.

It will revert the request...but that's kind of the point.


Along the lines of @ReyHaynes's suggestion, you can put almost anything into a dynamic array, but you'll get an error if you try to recall row 0 from a list with less than 1 row.

Consider separating concerns by returning the length of the list by itself. That way, if a client requests the first row before it exists, error is the appropriate response. With a public array, the clients can inspect by row number, starting at row 0 (if it exists).

BiddingStructs[] public biddingStructs;

function getBiddingCount() public view returns(uint count) {
    return biddingStructs.length;

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.