What the code does is:
I create new files in each run manually by giving names of the files to save Ex: filename= "XYZ", file is saved as "XYZ.txt". When I close and open program data is fetched from txt to show the program's previous state. It continues where it left off.
I am having this error when I press a button;
System.NullReferenceException: 'Object reference not set to an instance of an object.'
for my c1 object.
My file is blank I know, the buttons are made to fill my text when I press them. What does it have to do with "c1" object, it is just an object to provide communication with my other class.
[Serializable]
public partial class Form1 : Form
{
//FILENAME
string input = Interaction.InputBox("Enter a serial number", "TEST", "Default", -1, -1);
//DEFAULT PATH ROOT
String root = @"C:\Users\Dell\source\repos\SaveReloadDeneme\SaveReloadDeneme\bin\Debug";
Class1 c1 = new Class1();//holds method's attribute flags
Class1 c2 = new Class1(); //holds method flags whether the used or not
string path_combined;
public Form1()
{
InitializeComponent();
input += ".txt";
//default path + new filename
path_combined = Path.Combine(root, input);
if (!File.Exists(path_combined))
{
File.Create(path_combined);
}
//flag situation
string json2 = File.ReadAllText(path_combined);
FormatJson(json2);
c1 = JsonConvert.DeserializeObject<Class1>(json2);
//Method used or not
string json = File.ReadAllText(path_combined);
FormatJson(json);
c2 = JsonConvert.DeserializeObject<Class1>(json);
}
private static string FormatJson(string json)
{
dynamic parsedJson = JsonConvert.DeserializeObject(json);
return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
}
// SET TRUE
private void Button1_Click(object sender, EventArgs e)
{
c1.flag = true;
c1.flag2 = true;
c2.M1 = true;
string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
File.WriteAllText(path_combined, json);
string json2 = JsonConvert.SerializeObject(c2, Formatting.Indented);
File.WriteAllText(path_combined, json2);
}