EDIT:
I fix the previous errors,
I understand my problem with the code is related to writing a blank file; when I click my button to set values to write a file;
It says my "c1" object is empty.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
at this line since I click this button:
c1.flag = true
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, it is just an object to provide communication with my other class.
-I create a new file with given path, then write the json object to a blank txt.
What I have tried:
[Serializable]
public partial class Form1 : Form
{
string input = Interaction.InputBox("Enter a serial number", "TEST", "Default", -1, -1);
String root = @"C:\Users\Dell\source\repos\SaveReloadDeneme\SaveReloadDeneme\bin\Debug";
Class1 c1 = new Class1();
Class1 c2 = new Class1();
string path_combined;
public Form1()
{
InitializeComponent();
input += ".txt";
path_combined = Path.Combine(root, input);
if (!File.Exists(path_combined))
{
File.Create(path_combined);
}
string json2 = File.ReadAllText(path_combined);
FormatJson(json2);
c1 = JsonConvert.DeserializeObject<Class1>(json2);
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);
}
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);
}
[Serializable]
class Class1
{
[JsonProperty(PropertyName = "flag")]
public bool flag { get; set; }
[JsonProperty(PropertyName = "flag2")]
public bool flag2 { get; set; }
[JsonProperty(PropertyName = "SET TRUE USED")]
public bool M1 { get; set; }
[JsonProperty(PropertyName = "SET FALSE USED")]
public bool M2 { get; set; }
public Class1()
{
}
}