# HashTable possible bug on GetOrCreateBucket

**URL:** <https://forum.codewithmosh.com/t/hashtable-possible-bug-on-getorcreatebucket/27556>\
**Category:** Data Structures\
**Created:** [July 9, 2024, 11:37am UTC](https://forum.codewithmosh.com/t/hashtable-possible-bug-on-getorcreatebucket/27556 "2024-07-09T11:37:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dimanjenje](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/dimanjenje/32/331_2.png) [@dimanjenje](https://forum.codewithmosh.com/u/dimanjenje)\
**Post date:** [July 9, 2024, 11:37am UTC](https://forum.codewithmosh.com/t/hashtable-possible-bug-on-getorcreatebucket/27556/1 "2024-07-09T11:37:45Z")

</div>

I finished the Part 1 of the course, by using C# (with a Unit Test Project in the same solution), I think there’s a bug method GetOrCreateBucket(key), in HashTable video Solution - Refactoring…

 ![Screenshot_249](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/1/1f27a47a2b4a44d4267a16799e815700f6c6e289.png)

In case the bucket is null, it will create a new LinkedList in the entries[index] but wont initialize in the “bucket” variable that will be return, what can cause a null reference exception, that I caught in the Unit Testing. So in my case I solved by

> if (bucket == null)  
> bucket = \_entries[index] = new LinkedList();
> 
> return bucket;

or more concise:

> var bucket = \_entries[index] ?? (\_entries[index] = new LinkedList());  
> return bucket;
