• 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 unity_lF98_nVJe9di1A · May 06, 2019 at 06:37 PM · filetextassetcsvstreamreader

Using OpenFilePanel at runtime to open a file as a TextAsset to read data is giving NullReferenceException

I am trying to open a csv file using the OpenFile Dialog window which I will convert to a TextAsset and then Split accordingly to read the data, but it keeps giving me a NullReferenceException.

 NullReferenceException: Object reference not set to an instance of an object
 CsvNameSpace.CsvReader+<SetAngles>d__34.MoveNext () (at Assets/CsvReader.cs:92)
 UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
 UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
 CsvNameSpace.CsvReader:Update() (at Assets/CsvReader.cs:80)
 
 

Here is my code:

 using System.Collections;
 using System.IO;
 using System.Text;
 using UnityEditor;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 namespace CsvNameSpace {
 
 
     public class CsvReader : MonoBehaviour
     {
         
 
 
         public string[] fieldsNext;
         public string[] fields;
         public TextAsset csvFile;
         public Button record;
         public GameObject rWrist;
         public GameObject rPalm;
         public GameObject indexMeta;
         public GameObject thumbMeta;
         public GameObject middleMeta;
         public GameObject ringMeta;
         public GameObject pinkyMeta;
         public GameObject index_1;
         public GameObject index_2;
         public GameObject index_3;
         public GameObject indexEnd;
         public GameObject thumb_1;
         public GameObject thumb_2;
         public GameObject thumbEnd;
         public GameObject middle_1;
         public GameObject middle_2;
         public GameObject middle_3;
         public GameObject middleEnd;
         public GameObject ring_1;
         public GameObject ring_2;
         public GameObject ring_3;
         public GameObject ringEnd;
         public GameObject pinky_1;
         public GameObject pinky_2;
         public GameObject pinky_3;
         public GameObject pinkyEnd;
 
         // Update is called once per frame
         int i = 0;
         long previoustime = 0;
 
         void Start()
         {
             record.onClick.AddListener(ChangeScene);
             
             //open file dialog
             var path = EditorUtility.OpenFilePanel("", "","CSV");
             if (path == "")
             {
                 //if no file is chosen then change back to recording scene
                 ChangeScene();
             }
             else
             {
                 StreamReader file = new StreamReader(path);
                 //load file as a TextAsset
                 csvFile = Resources.Load(path) as TextAsset;
                 Debug.Log(path);
             }
         }
 
         void Update()
         {
                 StartCoroutine(SetAngles());
                 i++;
             
         }
 
        
         IEnumerator SetAngles()
         {
             print("Time " + Time.time);
             if (i > 0)
             {
                 //put CSV file into array called records by obtaining each row
                 string[] records = csvFile.text.Split('\n');
       //ERROR OCCURS HERE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        
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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

105 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Text file read, random quote C# 1 Answer

Resources.Load not giving me all data for a .txt file 1 Answer

How to open/read/write CSV file once the app is built? (like Resources.Load but whit the built app at Application.dataPath) 2 Answers

Can not load csv file 0 Answers

Reading Data generated in Processing from a TextAsset 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges