- Home /
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
Answer by vickychaudhary8955 · Mar 27 at 01:19 AM
did you get any answers coz i am facing the same thing and if you can help me out it will be grateful.
Your answer
Follow this Question
Related Questions
Can not output CSV file 1 Answer
TextAsset read non txt files 3 Answers
Can't read lines from file by using StreamReader on Android platform 2 Answers
Read and interpret data from .txt in real time, line by line 0 Answers
How does a TextAsset work? 1 Answer