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 Fatal.strings · Sep 05, 2015 at 03:18 PM · c#serializationnull reference exception

Null Reference Exception on a static array (Deserialize) - what am I doing wrong

Its giving me a null reference error even though I'm using a static array variable

any help is greatly appreciated

heres the entire Script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic; 
 using System.Runtime.Serialization.Formatters.Binary; 
 using System.IO;
 
 
 public static class SaveFileHandler {
 
     public static SaveValues[] SaveGames;
     public static bool File1, File2, File3, File4, File5, File6, File7, File8, File9, File10, FileA;
 
 
     public static void CheckFiles(){
         if(File.Exists(Application.dataPath + "/SaveFiles/Slot I.Save")){
             File1 = true;
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot I.Save", FileMode.Open);
             SaveFileHandler.SaveGames[1] = (SaveValues)bf.Deserialize(file);
             file.Close();
 
         }
     
         if(File.Exists(Application.dataPath + "/SaveFiles/Slot II.Save")){
             File2 = true;
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot II.Save", FileMode.Open);
             SaveFileHandler.SaveGames[2] = (SaveValues)bf.Deserialize(file);
             file.Close();
         }
 
         if(File.Exists(Application.dataPath + "/SaveFiles/Slot III.Save")){
             File3 = true;
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot III.Save", FileMode.Open);
             SaveFileHandler.SaveGames[3] = (SaveValues)bf.Deserialize(file);
             file.Close();
         }
     
         if(File.Exists(Application.dataPath + "/SaveFiles/Slot IV.Save")){
             File4 = true;
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot IV.Save", FileMode.Open);
             SaveFileHandler.SaveGames[4] = (SaveValues)bf.Deserialize(file);
             file.Close();
         }
 
         if(File.Exists(Application.dataPath + "/SaveFiles/Slot V.Save")){
             File5 = true;
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot V.Save", FileMode.Open);
             SaveFileHandler.SaveGames[5] = (SaveValues)bf.Deserialize(file);
             file.Close();
         }
 
         if(File.Exists(Application.dataPath + "/SaveFiles/Slot VI.Save")){
             File6 = true;
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot VI.Save", FileMode.Open);
             SaveFileHandler.SaveGames[6] = (SaveValues)bf.Deserialize(file);
             file.Close();
         }
 
         if(File.Exists(Application.dataPath + "/SaveFiles/Slot VII.Save")){
             File7 = true;
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot VII.Save", FileMode.Open);
             SaveFileHandler.SaveGames[7] = (SaveValues)bf.Deserialize(file);
             file.Close();
         }
 
         if(File.Exists(Application.dataPath + "/SaveFiles/Slot VIII.Save")){
             File8 = true;
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot VIII.Save", FileMode.Open);
             SaveFileHandler.SaveGames[8] = (SaveValues)bf.Deserialize(file);
             file.Close();
         }
 
         if(File.Exists(Application.dataPath + "/SaveFiles/Slot IX.Save")){
             File9 = true;
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot IX.Save", FileMode.Open);
             SaveFileHandler.SaveGames[9] = (SaveValues)bf.Deserialize(file);
             file.Close();
         }
 
         if(File.Exists(Application.dataPath + "/SaveFiles/Slot X.Save")){
             File10 = true;
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot X.Save", FileMode.Open);
             SaveFileHandler.SaveGames[10] = (SaveValues)bf.Deserialize(file);
             file.Close();
         }
 
         if(File.Exists(Application.dataPath + "/SaveFiles/Slot AUTO.Save")){
             FileA = true;
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + "/SaveFiles/Slot AUTO.Save", FileMode.Open);
             SaveFileHandler.SaveGames[11] = (SaveValues)bf.Deserialize(file);
             file.Close();
         }
     }
 
 
     public static void Save(int x) {
         string address = ""; // Somehow its an error unless i assign it something...
         if(x == 1){
             address = "/SaveFiles/Slot I.Save";
         }
         if(x == 2){
             address = "/SaveFiles/Slot II.Save";
         }
         if(x == 3){
             address = "/SaveFiles/Slot III.Save";
         }
         if(x == 4){
             address = "/SaveFiles/Slot IV.Save";
         }
         if(x == 5){
             address = "/SaveFiles/Slot V.Save";
         }
         if(x == 6){
             address = "/SaveFiles/Slot VI.Save";
         }
         if(x == 7){
             address = "/SaveFiles/Slot VII.Save";
         }
         if(x == 8){
             address = "/SaveFiles/Slot VIII.Save";
         }
         if(x == 9){
             address = "/SaveFiles/Slot IX.Save";
         }
         if(x == 10){
             address = "/SaveFiles/Slot X.Save";
         }
         if(x == 11){
             address = "/SaveFiles/Slot AUTO.Save";
         }
 
         SaveFileHandler.SaveGames[x] = new SaveValues();
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.dataPath + address); //you can call it anything you want
         bf.Serialize(file, SaveFileHandler.SaveGames);
         file.Close();
 
 
 
 
     }   
 
     public static void Load(int x) {
         string address= "";
         if(x == 1){
             address = "/SaveFiles/Slot I.Save";
         }
         if(x == 2){
             address = "/SaveFiles/Slot II.Save";
         }
         if(x == 3){
             address = "/SaveFiles/Slot III.Save";
         }
         if(x == 4){
             address = "/SaveFiles/Slot IV.Save";
         }
         if(x == 5){
             address = "/SaveFiles/Slot V.Save";
         }
         if(x == 6){
             address = "/SaveFiles/Slot VI.Save";
         }
         if(x == 7){
             address = "/SaveFiles/Slot VII.Save";
         }
         if(x == 8){
             address = "/SaveFiles/Slot VIII.Save";
         }
         if(x == 9){
             address = "/SaveFiles/Slot IX.Save";
         }
         if(x == 10){
             address = "/SaveFiles/Slot X.Save";
         }
         if(x == 11){
             address = "/SaveFiles/Slot AUTO.Save";
         }
 
         if(File.Exists(Application.dataPath + address)) {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + address, FileMode.Open);
             SaveFileHandler.SaveGames[x] = (SaveValues)bf.Deserialize(file);
             file.Close();
 
             Debug.Log("Shards: "+ SaveFileHandler.SaveGames[x].shard);
         }
     }
 
 
 }
 


Comment
Add comment · Show 1
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 getyour411 · Sep 05, 2015 at 07:01 AM 0
Share

How about a line# of error and the callStack from console?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PixelParts · Sep 06, 2015 at 12:45 PM

I'm guessing you're missing an array initialization:

SaveGames = new SaveValues[12];

Callstack would help.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

CustomEditor for an ScriptableObject asset only works after recompile. 1 Answer

Is it possible to create a custom gettter/setter on SerializedProperty ? 0 Answers

Serialized Fields go bye bye on Android 0 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