Weird null bug

When I run the game the first time it gives me an error
ArgumentNullException: Value cannot be null.
Parameter name: key
if I continue playing it just never come again
any idea what is going on?
Youtube video of the bug

1 Like

I’m guessing this won’t help you this early on in your course, but technically it’s a reference variable is attempted to be accessed when it’s null (either through being deleted, not being set in the first place or it being set to null). Think of it like a credit where the account has been closed. It’s going to hit you up with an error if you try to spend money from an account that doesn’t exist.

It’s hard to narrow it down without more information. Unity tells you the details in the console when the error occurs.

so it’s something going on inside of the editor, not the c# code?

These are the error details can you point for me a possible thing to do or search what is going on in
ArgumentNullException: Value cannot be null.
Parameter name: key
System.Collections.Generic.Dictionary2[TKey,TValue].FindEntry (TKey key) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Collections.Generic.Dictionary2[TKey,TValue].ContainsKey (TKey key) (at <695d1cc93cca45069c528c15c9fdd749>:0)
GameDevTV.Inventories.InventoryItem.GetFromID (System.String itemID) (at Assets/scripts/Inventories/InventoryItem.cs:53)
GameDevTV.Inventories.Inventory.Evaluate (System.String predicate, System.String parameters) (at Assets/scripts/Inventories/Inventory.cs:263)
RPG.Core.Condition.Check (System.Collections.Generic.IEnumerable1[T] evaluators) (at Assets/scripts/Core/Condition.cs:19) RPG.Dialogue.DialogueNode.CheckCondtion (System.Collections.Generic.IEnumerable1[T] evaluators) (at Assets/scripts/Dialogue/DialogueNode.cs:33)
RPG.Dialogue.PlayerConversant+d__18.MoveNext () (at Assets/scripts/Dialogue/PlayerConversant.cs:116)
System.Linq.Enumerable.Count[TSource] (System.Collections.Generic.IEnumerable`1[T] source) (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
RPG.Dialogue.PlayerConversant.HasNext () (at Assets/scripts/Dialogue/PlayerConversant.cs:110)
RPG.UI.DialogueUI.UpdateUI () (at Assets/scripts/UI/Dialogue/DialogueUI.cs:60)
RPG.Dialogue.PlayerConversant.StartDialogue (RPG.Dialogue.AIConversant newConversant, RPG.Dialogue.Dialogue newDialogue) (at Assets/scripts/Dialogue/PlayerConversant.cs:28)
RPG.Dialogue.AIConversant.HandleRaycast (RPG.Control.PlayerController callingController) (at Assets/scripts/Dialogue/AIConversant.cs:26)
RPG.Control.PlayerController.InteractWithComponent () (at Assets/scripts/Control/PlayerController.cs:68)
RPG.Control.PlayerController.Update () (at Assets/scripts/Control/PlayerController.cs:54)

Ignore what I said before. I misread what you said as a null reference error.

Unless you’ve got something really funky going on, the issue is more likely in code, not the inspector.

By the look of it, you’re adding an entry or retrieving an entry from a dictionary without telling it the associated key to what you’re adding, or the key being null. But that’s a semi-guess without context of the error.

This looks like this is from the RPG course. Have you done any Unity courses before this one? If not, you picked one hell of a course to start with. It’s an intermediate course.

no it’s not my first course I’ve been learning for almost 2 years, The error accrues only when I add I IPredicateEvaluator to the inventory.cs I have my project and the one from this course side by side and copy pasting everything and the error still there

I haven’t done that part of the course, so I’m not going to have a chance of helping blind.

while debugging I’ve found that Resources folder in my project don’t load in the start of the game, it load when I start a dialogue, any idea where to look for to fix it?

Take a look here:
Resources.Load

A lot of debugging later, found that the display name for one of the weapons is not set and it was the reason for the error
image

Good job finding that, sorry I didn’t get to you sooner.

Null strings and uninitialized Lists can get us into situations like this, with difficult to nail down NREs. Here’s a handy trick to prevent unitialized strings:

[SerializeField] string DisplayName = ""; //Intitialize it with "" prevents NRE

An alternate suggestion, to help highlight that you haven’t changed the string when you are wearing your designer hat is to initialize it with something like “Placeholder Text”

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms