0

I am trying to add the content of my BindingList into a txt file. However, I always get the following error:

System.NullReferenceException: Object reference not set to an instance of an object. 

What am I doing wrong? The text file is empty if that helps.

Code:

BindingList<Student> StudentCollection = new BindingList<Student>();

private void btnAddStudent_Click(object sender, EventArgs e)
{
    Student StudentSave = new Student
    {
        ID = txtStudentID.Text,
        FirstName = txtFirstName.Text,
        LastName = txtLastName.Text,
        Age = nudAge.Value,
        Height = nudHeight.Value,
        Schoolclass = txtSchoolClass.Text,
        Gender = cbxGender.Text,
    };

    cbxStudentIDs.DataSource = StudentCollection;
    cbxStudentIDs.DisplayMember = "ID";
    StudentCollection.Add(StudentSave);

}
public class Student
{
    public string ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public decimal Age { get; set; }
    public decimal Height { get; set; }
    public string Schoolclass { get; set; }
    public string Gender { get; set; }

}

private void Form1_Load(object sender, EventArgs e)
{
    string studentCollectionString = File.ReadAllText(FilePath);
    StudentCollection = JsonConvert.DeserializeObject<BindingList<Student>>(studentCollectionString);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    string studentCollectionString = JsonConvert.SerializeObject(StudentCollection);
    File.WriteAllText(FilePath, studentCollectionString);
}
zappee
  • 20,148
  • 14
  • 73
  • 129
  • Which object is throwing the null reference exception? If you can provide either the stacktrace or run the debugger, and step through each line. – mrdnk May 05 '20 at 07:53
  • After this Line ``` StudentCollection = JsonConvert.DeserializeObject>(studentCollectionString);``` –  May 05 '20 at 08:02
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – CSharpie May 05 '20 at 08:18
  • Hi, you are not suppose to directly assign StudentCollection to an result coz you will get the Null Exception. try the solution that I answered, its working for me https://stackoverflow.com/a/61608673/1740399 – Nikhil Nambiar May 05 '20 at 08:20

2 Answers2

1

Issue

This is where you will get the exception, as you mentioned text file is empty and StudentCollection will be set to null

StudentCollection = JsonConvert.DeserializeObject<BindingList<Student>>(studentCollectionString);

Solution

You should change the code like this: the ? will ensure that operation proceeds only if you have valid result in the text file.

JsonConvert.DeserializeObject<BindingList<Student>>(studentCollectionString)?.ToList().ForEach(a => StudentCollection.Add(a));
Nikhil Nambiar
  • 376
  • 1
  • 4
  • 8
  • This is the full Code for Form1_Load: private void Form1_Load(object sender, EventArgs e) { string studentCollectionString = File.ReadAllText(FilePath); JsonConvert.DeserializeObject>(studentCollectionString)?.ToList().ForEach(a => StudentCollection.Add(a)); } – Nikhil Nambiar May 05 '20 at 08:17
  • You sir, are a Legend! Thanks so much :) –  May 05 '20 at 08:24
0

Ensure your Filepath does exist if not exist you should create the file then write to file, also ensure that your json is not empty

https://learn.microsoft.com/en-us/dotnet/api/system.io.file.create?view=netcore-3.1