Click here to Skip to main content
14,490,096 members
Rate this:
Please Sign up or sign in to vote.
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()
        {
           
        }
    }
Posted
Updated 23-Jul-19 1:25am
v12
Comments
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.
Danny96 23-Jul-19 5:14am
   
yes you are right, I traced my code fixed my errors, but I am having a difficulty with (de)serialization, I updated my post, I think it is more accurate now
Richard MacCutchan 23-Jul-19 5:28am
   
There is no point saying "I think it is more accurate now". You need to show us the exact code that you are running, and explain exactly what problem you are having, and where it occurs. If the issue is with the data that you are processing then we need to see that data also.
Danny96 23-Jul-19 7:18am
   
I have just uploaded the whole code, and stated the error line
Richard MacCutchan 23-Jul-19 7:50am
   
You are still trying to deserialize from a file that does not contain any data. You cannot create a new file and immediately expect to read some useful information from it.

I refer you back to my previous suggestion: stop just throwing random lines of code around and expecting something to work. Start from the beginning:
1. get the file name.
2. if the file exists then try to read its content.
2a. check that you have actually read some valid data
3. if the file does not exist then create a new file.
3a. do not try to read from it, it will be empty
Danny96 23-Jul-19 8:06am
   
I solved it :))))) thank you so much
BillWoodruff 23-Jul-19 10:02am
   
too bad I can't vote comments up !
Richard MacCutchan 23-Jul-19 10:21am
   
No worries Bill. I still appreciate your comments.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
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.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100