Click here to Skip to main content
15,746,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:


    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();//holds method's attribute flags
        Class1 c2 = new Class1(); //holds method flags whether the used or not

        string path_combined;
        public Form1()
            input += ".txt";
            //default path + new filename
            path_combined = Path.Combine(root, input);

            if (!File.Exists(path_combined))

            //flag situation
            string json2 = File.ReadAllText(path_combined);
            c1 = JsonConvert.DeserializeObject<Class1>(json2);

            //Method used or not
            string json = File.ReadAllText(path_combined);
            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);


    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()
Updated 23-Jul-19 1:25am
Richard MacCutchan 23-Jul-19 3:47am    
You create a new ArrayList and List<string>, and then try to use their content. But since both are new objects they do not contain anything.
Danny96 23-Jul-19 3:52am    
I simplified my code, got rid of all arraylist stuff, it is more clear now. But I am having error at the same line, it says access denied, I don't know why
Richard MacCutchan 23-Jul-19 4:34am    
Look at the string that has been created with the full path to the file to see why it is not valid. Maybe you forgot to add the .txt extension for the filename, maybe the filename does not exist, maybe it contains an invalid character, etc. Please try and do the basic debugging for yourself.
Danny96 23-Jul-19 4:45am    
I updated my code, I realized that I forgot to put .txt extension, by the way the filename does not exist because I want to create a new file with that name
Richard MacCutchan 23-Jul-19 4:48am    
Well, you cannot file.readalltext(path_combined); on a file which does not exist.

I would suggest you stop coding and start with a piece of paper and write down, in logical order, what you are trying to do. As it stands it looks like you are just throwing statements in and hoping that something will work.

1 solution

But I am having error at the same line, it says access denied, I don't know why

Access denied is pretty obvious: the user that the code is executing under does not have the rights to the file or folder it it trying to open.

You can't fix that; it's a feature of the OS, and the only way to change it is to either use the OS to give access to the file / folder, or (better) keep your files in a more suitable place.

To be honest, hardcoding your paths is a bad idea to start with; hard coding them to a specific user and a debug only folder is really a bad idea! It's bad enough using the EXE path (which is trivial to get at run time) because that will fail in production, but to hardcode to a folder you really, really don't want to even exist on a client computer is just dumb.

See here: Where should I store my data?[^] for some better ideas.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

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