Click here to Skip to main content
15,894,405 members

Serialize and deserialize with JSON writing to “.txt” problem

Danny96 asked:

Open original thread
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
    {
        
        //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);

        }


[Serializable]
    
    class Class1
    {
        [JsonProperty(PropertyName = "flag")]
        public bool flag { get; set; }


        [JsonProperty(PropertyName = "flag2")]
        public bool flag2 { get; set; }

        //indication whether the method is used or not
        [JsonProperty(PropertyName = "SET TRUE USED")]
        public bool M1 { get; set; }

        [JsonProperty(PropertyName = "SET FALSE USED")]
        public bool M2 { get; set; }
        public Class1()
        {
           
        }
    }
Tags: C#, JSON, Serialization

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900