Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# List<> with custom class returns null

I am developing a game in Unity and I came upon a problem.
I am fetching data from file and I wanna store them into List<> with my own class Words.

public class Words
{
    private readonly string _myWord;
    private readonly int _damage;

    public Words()
    {
        _myWord = "";
        _damage = 0;
    }

    public Words(string word, int damage)
    {
        _myWord = word;
        _damage = damage;
    }

    public override string ToString()
    {
        return _myWord + ":" + _damage + "\n";
    }
}

I load the data and store it to the object of my custom class


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using UnityEditor;

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Serialization;

public class WorkingWithTextFiles : MonoBehaviour
{
    [SerializeField] private TextAsset myFile;
    private List<Words> _wordsList;
    [SerializeField] private string _seperatorParts;
    [SerializeField] private string _seperatorEndOfLine;
    
    // Start is called before the first frame update
    private void Start()
    {
        try
        { 
            var textFromFile = myFile.ToString(); //gets contents of file
            var numberOfWords = int.Parse(textFromFile.Split("\n")[0]) ; //returns number of words - given in file on first line
            textFromFile = textFromFile.Remove(0,3); //deletes number and /n
                
                var word = textFromFile.Split(_seperatorParts)[0]; //gets only the word
                textFromFile = textFromFile.Remove(0, word.Length + 1); //removes the word + separator
                var damage = textFromFile.Split(_seperatorEndOfLine)[0]; //gets only the damage
                textFromFile = textFromFile.Remove(0, damage.Length + 3); //removes the damage + separator + \n
                
                var givenWord = new Words(word, int.Parse(damage));
                
                _wordsList.Add(new Words(word, int.Parse(damage)));
                //Debug.Log(_wordsList.Count);
                Debug.Log(_wordsList.ToString());
                
        }
        catch (Exception e)
        {
           Debug.Log(e);
        }
       
    }

    
}

I take data from file and store it into word and damage. Then I make givenWord from them. They store okay and upon printing they return fine.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Ond the other hand the _wordsList is null.
It’s null when I print out it’s size, it’s null when I print it out using Debug.Log.

>Solution :

Your WorkingWithTextFiles class seemingly never initializes its _wordsList field.

Since a list is a reference type and you never initialize the field, the value it has will always be null. you should initialize it on object creation with private List<Words> _wordsList = new();.
This way, the list you want to add items to actually exists when you call .Add().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading