Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by holy_jay · Apr 16, 2017 at 02:44 AM · c#unity 5json

Null Reference exception when loading json data to dictionary

i am trying to load a json file into a dictionary but i get a NullReferenceException error anytime it gets to the for loop. Here is the code I am using for loading the json file

 using UnityEngine;
 using UnityEngine.UI;
 using System.IO;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CategoryController : MonoBehaviour {
     private Dictionary<string, string> wordDictionary;
     private string fileName = "testdictionary.json";
 
     void Start () {
         LoadGameDictionary();
     }
     void LoadGameDictionary()
     {
         wordDictionary = new Dictionary<string, string>();
         string filePath = Path.Combine(Application.streamingAssetsPath, fileName);
 
         if (File.Exists(filePath))
         {
             Debug.Log("File found");
             string dataAsJson = File.ReadAllText(filePath);
             WordsData wordData = JsonUtility.FromJson<WordsData>(dataAsJson);
             for (int i = 0; i < wordData.items.Length; i++) 
             {
                wordDictionary.Add(wordData.items [i].key, wordData.items [i].value)
             }           
 
         }
         else
         {
             Debug.LogError("file not found");
         }
     }
 }

these are the data classes i am using

 [System.Serializable]
 public class WordItems
 {
     public string word;
     public string meaning;
 }
 
 [System.Serializable]
 public class WordsData{
     public WordItems[] items;
 }

thank you in advance...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Apr 16, 2017 at 02:47 AM

Well, how does your JSON file look like? It has to look like this:

 {
     "items":[
         {
             "word":"someWordHere",
             "meaning":"some meaning here"
         },
         {
             "word":"someOtherWordHere",
             "meaning":"some other meaning here"
         },
     ]
 }

If your JSON doesn't look like this you can't load it into that datastructure.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image holy_jay · Apr 16, 2017 at 10:00 AM 0
Share

$$anonymous$$y JSON file looks like:

     {
         "DIPLOBLASTIC":"Characterizing the ovum when it has two primary ger$$anonymous$$allayers.",
         "DEFIGURE":"To delineate. [Obs.]These two stones as they are here defigured. Weever.",
         "LO$$anonymous$$BARD":"Of or pertaining to Lombardy, or the inhabitants of Lombardy.",
         "BAHAIS$$anonymous$$":"The religious tenets or practices of the Bahais.",
         "FU$$anonymous$$ERELL":"See Femerell.",
         "ROYALET":"A petty or powerless king. [R.]there were at this time two other royalets, as only kings by hisleave. Fuller."
     }

and after changing it to look like yours:

 {
     "items":[
         {
             "word":"DIPLOBLASTIC",
             "meaning":"Characterizing the ovum when it has two primary ger$$anonymous$$allayers."
         },
         {
             "word":"DEFIGURE",
             "meaning":"To delineate. [Obs.]These two stones as they are here defigured. Weever."
         },
         {
             "word":"LO$$anonymous$$BARD",
             "meaning":"Of or pertaining to Lombardy, or the inhabitants of Lombardy."
         },
         {
             "word":"BAHAIS$$anonymous$$",
             "meaning":"The religious tenets or practices of the Bahais."
         },
         {
             "word":"FU$$anonymous$$ERELL",
             "meaning":"See Femerell."
         },
         {
             "word":"ROYALET",
             "meaning":"A petty or powerless king. [R.]there were at this time two other royalets, as only kings by hisleave. Fuller."
         },
     ]
 }
 
 I now get an error ArgumentException: JSON parse error: Invalid value
avatar image Bunny83 holy_jay · Apr 16, 2017 at 11:07 AM 0
Share

$$anonymous$$aybe the Unity main parser doesn't like empty values at the end. Re I've the last comma after the last entry

avatar image holy_jay Bunny83 · Apr 16, 2017 at 12:12 PM 0
Share

i removed it and now i am back to the nullReferenceException error

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JsonUtility with openweatherAPI 1 Answer

Unity json get data 2 Answers

How can i get all of my users data on FireBase and store it as an array using Unity and RestClient json 1 Answer

Issues with JSONUtility 0 Answers

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges