Search Unity

What is this error all about?

Discussion in 'Getting Started' started by Gabrieldonley, Jan 21, 2016.

  1. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    Hi,
    I've been getting an error for quite a while (I'm pretty sure it's the only error breaking my program) and in the community, everyone's telling me that I can't ask for help because it's off-topic or something. Anyway here is the error: KeyNotFoundException: The given key was not present in the dictionary. I've googled the error and found many results, but nothing very helpful. I'm working on creating a beginner rpg style inventory tutorial and I think it doesn't recognize the .json file or something. The first code block is my Items.json.txt and secondly is the Items Database. So there's the error and here's my code:
    Code (CSharp):
    1. [
    2.     {
    3.         "id": 0,
    4.         "title": "Steel Gloves",
    5.         "value": 6
    6.     }
    7.     {
    8.         "id": 1,
    9.         "title": "Wooden Sword",
    10.         "value": 60
    11.     }
    12. ]
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using LitJson;
    4. using System.Collections.Generic;
    5. using System.IO;
    6.  
    7. public class ItemDatabase : MonoBehaviour{
    8.     private List<Item> database = new List<Item>();
    9.     private JsonData itemData;
    10.  
    11.     void Start()
    12.     {
    13.         itemData = JsonMapper.ToObject (File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.json.txt"));
    14.         ConstructItemDatabase ();
    15.  
    16.         Debug.Log (database [1].Title);  
    17.  
    18.     }
    19.  
    20.     void ConstructItemDatabase()
    21.     {
    22.         for (int i = 0; i < itemData.Count; i++)
    23.         {
    24.             database.Add(new Item((int)itemData[i]["id"], itemData[i]["title"].ToString(), (int)itemData[i]["value"]));
    25.         }
    26.     }
    27. }
    28.  
    29. public class Item
    30. {
    31.     public int ID { get; set; }
    32.     public string Title { get; set; }
    33.     public int Value { get; set; }
    34.  
    35.     public Item(int id, string title, int value)
    36.     {
    37.         this.ID = id;
    38.         this.Title = title;
    39.         this.Value = value;
    40.     }
    41. }
    42.  
    43.  
    44.  
    45.  
    46.  
    47.  
    48.  
    49.  
    50.  
    51.  
    52.  
    53.  
    54.  
    55.  
    56.  
    57.  
    58.  
    59.  
    60.  
    61.  
    62.  
    63.  
    64.  
     
  2. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    What line is the error on? The message seems pretty straightforward; do you know what a key is?
     
  3. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    This is absolutely all that was in my console. No, I'm not sure what a key is.
     
  4. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    This
    doesn't have a line number in it. Usually error messages say something like (20/10) to indicate the line/character where the error happened, although there are occasionally error messages that don't say.

    A "key" is the label for looking up a value. So like your identifiers "id" and "title"; the error is indicating that somewhere the code is attempting to access a key that doesn't exist. Looking at it more closely, that json chunk looks malformed (for starters, there should be a comma between each object) so the JsonMapper is probably returning an empty JsonData.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    To expand on what @jhocking said, if you just double-click the error in the console, it will not only open the file where the error occurred, but it will put the cursor right on the very line with the error. So, do that.

    In addition, if you select the error in the Console and then Copy, what you will find on your clipboard is not just the text of the error message, but also the complete call stack (the chain of what-called-what to get to where the problem was). This can be very helpful in more complex code.

    Finally, this will be much easier to debug if you learn how to use the debugger. Instead of just giving you an error message, you can have it stop where the problem occurs, and let you poke around in the variables, and even type expressions (like itemData or itemData) to see what they evaluate to. Another way to debug is to throw in a bunch more Debug.Log statements and study the results, but that is usually much slower than just using the debugger.
     
    Last edited: Aug 6, 2018
  6. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    KeyNotFoundException: The given key was not present in the dictionary.
    System.Collections.Generic.Dictionary`2[System.Int32,System.Int32[]].get_Item (Int32 key) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
    LitJson.JsonReader.Read ()
    Rethrow as JsonException: Invalid token '123' in input string
    LitJson.JsonReader.Read ()
    LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader)
    LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader)
    LitJson.JsonMapper.ToWrapper (LitJson.WrapperFactory factory, System.String json)
    LitJson.JsonMapper.ToObject (System.String json)
    ItemDatabase.Start () (at Assets/Scripts/ItemDatabase.cs:13)
    --------------
    I found this, but at line 13, as you can see from the code above, doesn't make much sense to me.
     
  7. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    I just think it's something having to do with Items.json.txt
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yep, as you can see in the error details, the real problem appears to be Invalid token '123' in input string.

    That's a pretty cryptic error given that '123' doesn't appear in your JSON anywhere that I can see. But your JSON really is invalid, as you can see by pasting it into the big field at JSONLint.com and clicking Validate. So, maybe fixing it will make the problem go away.

    Incidentally, if you're not too wedded to JSON yet, you might want to consider GRFON instead. It's considerably easier for humans to read and write (correctly).
     
  9. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    er did you miss where I said:
    So yes, I already told you something that's wrong with your JSON.
     
  10. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    ok, thanks!
     
  11. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    And when you say between each object, where do u mean?
     
  12. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Curly braces {} denote objects in JSON. More generally, there are plenty of resources online for how to correctly structure JSON (indeed, I think joeS linked to one).
     
  13. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    tedthebug and jhocking like this.
  14. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    Do I paste the code or the error in the big field on jsonlint?
     
  15. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    You would paste the Json. The point of JsonLint is to check if your json data is valid (ie. written correctly).
     
  16. HummingBiyrd

    HummingBiyrd

    Joined:
    Aug 4, 2018
    Posts:
    1
    So I just got the same issue, and have validated that it isn't my json that is the problem its something in the, for loop. The code is basically identical. Any ideas on what could be happening.
     
  17. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Could you paste the json for us to check?