Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Tiktaalik · Apr 18, 2013 at 06:40 PM · javascriptjsondeserialization

"Object reference not set to an instance of an object" when attempting to parse Dictionary of Lists with JSONFx, MiniJSON

I'm attempting to parse some JSON using the JSONFx plugin that I found here.

My JSON looks something like this.

 {
     "tweakableSliders" : [
         {
             "Name" : "DrPepper",
             "Min" : 50.0,
             "Default" : 62.0,
             "Max" : 100.0
         },
         {
             "Name" : "7up",
             "Min" : 22.0,
             "Default" : 120.0,
             "Max" : 250.0
         },
     ],
 
     "tweakableToggles" : [
         {
             "enableBananas" : false
         },
         {
             "enableApples" : true
         }  
     ]
 }

And so I'm wanting to get a Generic Dictionary of Generic Lists as a result. I'm trying to use the following line:

 var tweakable : TextAsset = Resources.Load("tweakable");
 var collection = JsonReader.Deserialize(tweakable.text) as Dictionary.<String, List.<Object> >;

But this gives me the error: NullReferenceException: Object reference not set to an instance of an object

If I do something like, var collection = JsonReader.Deserialize(tweakable.text) as Dictionary.<object>; it'll compile but if I attempt to cast that Object to a List and use it as a List I'll have the same error.

I'm very new to JSON and clearly not understanding something.

EDIT: I have the same issue with MiniJSON so I guess I'm not understanding the restrictions of these libraries/JSON or Unityscript (I'm a beginner)?

P.S. If there's some other JSON library that might be better for what I want to do please suggest it.

Comment
Add comment · Show 7
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 Tiktaalik · Apr 19, 2013 at 12:01 AM 0
Share

Actually I was apparently wrong?! I have no idea what I was doing before but I can cast the Object to a List..

 var collection = Json.Deserialize(tweakable.text) as Dictionary.<String, Object >;
 for (var item in collection.Values)
 {
     var itemCollection = item as List.<Object>;
     Debug.Log(itemCollection.Count);
 }

This will work, which is great, but I'm still left wondering why I can't write:

 var collection = Json.Deserialize(tweakable.text) as Dictionary. < String, List.<Object> >;

avatar image firestorm713q · Apr 19, 2013 at 12:11 AM 0
Share

Before I go into more detail, do you already understand what the error actually means in a general sense? Like can you fix that error if it weren't a JSON error?

avatar image Tiktaalik · Apr 19, 2013 at 12:13 AM 0
Share

$$anonymous$$y interpretation would be that the object is null, but I'm trying to call a function on null and so there's an error. I have a C++ background and I'm a newbie to Unityscript and Javascript so I may be wrong.

avatar image Tiktaalik · Apr 19, 2013 at 12:34 AM 0
Share

I can get it working for $$anonymous$$iJSON but I still can't get similar code to work with JsonFX

ie. The following gives me the error: NullReferenceException: Object reference not set to an instance of an object

 var reader = new JsonReader(tweakable.text);
 var collection = reader.Deserialize() as Dictionary.<String, Object>;
 
 for (var item : Object in collection.Values)
 {
     var list = item as List.<Object>;
     Debug.Log(list.Count);
 }
avatar image firestorm713q · Apr 19, 2013 at 07:32 PM 0
Share

Exactly. The object is referencing an object that simply isn't there. So I would check to see if your file is being received. $$anonymous$$aybe throw the whole thing in a try/catch block, and catch a filenotfound exception? That would at least tell you if you're having problems there. I'd need to see more of your script to give you a detailed answer, though.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

12 People are following this question.

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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Best Performance: Tag or GetComponent + Rendering/Diactivating 1 Answer

Help with OnCollision 2 Answers


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