0

So I have three buttons that have its own functionalities,

  • Generate a TextBox dynamically
  • Retrieve texts from the textbox and store them in a text file
  • Delete the text files.

    Generate textbox button:

    // Generate notes
            int curr = 0;
            private void guna2Button1_Click(object sender, EventArgs e) {
                int top = 25;
                int h_p = 170;
                curr++;
                    var new_note = new Guna2TextBox() {
                        Text = "Title\n",
                        Name = "Note" + curr,
                        Multiline = true,
                        AcceptsTab = true,
                        AcceptsReturn = true,
                        WordWrap = false,
                        ScrollBars = ScrollBars.Vertical,
                        Width = 220,
                        Height = 110,
                        BorderRadius = 8,
                        Font = new Font("Bahnschrift", 13),
                        ForeColor = Color.White,
                        FillColor = ColorTranslator.FromHtml("#1E1E1E"),
                        BorderColor = ColorTranslator.FromHtml("#2C2C2C"),
                        Location = new Point(450,top)
                    };
                top += h_p;
                flowLayoutPanel1.Controls.Add(new_note);
            }
    

    Stores TextBox texts into a text file button:

        // Save notes
        private void guna2Button7_Click(object sender, EventArgs e) {
            for(int j=1; j<flowLayoutPanel1.Controls.Count+1; j++) {
                var notes = ((Guna2TextBox)flowLayoutPanel1.Controls["Note" + j]);         
                var messages = notes.Text;
                var titles = notes.Lines[0];
                string notes_path = @"C:\NOTES_LOC\DATA_LOC\" + titles + ".txt"; 
                try {
                    if(titles != "") { 
                        using(FileStream fs = File.Create(notes_path)) {
                            Byte[] notes_texts = new UTF8Encoding(true).GetBytes(messages);
                            fs.Write(notes_texts,0,notes_texts.Length);
                         }
                   } else {
                       MessageBox.Show("Title cannot be empty", "Flow Notes System");
                    }
                } catch (Exception eq) {
                    MessageBox.Show(eq.ToString(),"Notes System");
                }
            }
        }
    

    Delete stored text file button:

    // Delete notes
        private void guna2Button5_Click(object sender, EventArgs e) {
            DirectoryInfo dirs_notes_source = new DirectoryInfo(@"C:\NOTES_LOC\DATA_LOC\");
            string dirs_notes = @"C:\NOTES_LOC\DATA_LOC\";
            string garb_path = @"C:\NOTES_LOC\GARBAGE_LOC\";
            if(flowLayoutPanel1.Controls.Count != 0) {
                flowLayoutPanel1.Controls.Clear();
    
                foreach(string dir_path in Directory.GetDirectories(dirs_notes,"*",SearchOption.AllDirectories)) {
                    Directory.CreateDirectory(dir_path.Replace(dirs_notes,garb_path));
                }
    
                foreach(string retv_notes in Directory.GetFiles(dirs_notes,"*.*",SearchOption.AllDirectories)) {
                    File.Copy(retv_notes,retv_notes.Replace(dirs_notes,garb_path),true);
                }
    
                foreach (FileInfo notes in dirs_notes_source.GetFiles()) {
                    notes.Delete();
                }
            }
            else {
                MessageBox.Show("No note found","Flow Notes System");
            }
        }
    

    Now let me explain my problem, when I generated the TextBox by pressing the button that supposed to generates it I tested out my "Save" which supposed to retrieve the text from the TextBox and store it in a txt file button and it works, then I tried to tested out my "Delete Notes" button and it works, but the real problem comes in when I tried to save a newly generated Note (TextBox) to a txt file after deleting the previously created note where I got this error popped up saying: Object reference not set to an instance of an object.

    ************** Exception Text **************
    System.NullReferenceException: Object reference not set to an instance of an object.
       at FlowNotes.Form1.guna2Button7_Click(Object sender, EventArgs e) in C:\Users\USER\source\repos\FlowNotes\FlowNotes\Form1.cs:line 136
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at Guna.UI2.WinForms.Guna2Button.OnClick(EventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    

    On debugging process:

    var messages = notes.Text;
    
    System.NullReferenceException: 'Object reference not set to an instance of an object.'
    notes was null.
    

    Even though when the var messages = notes.Text isn't null the error above keeps showing.

    • Use proper sentences instead of a 7 line uninterrupted sentence. Post the actual full exception text, not just the Message. A NullReferenceException would point at the actual line that caused it. It's always thrown because the code tried to make a call on an empty variable or parameter. Where is the exception thrown? Did you try debugging the application? Did you inspect the variables or fields at this point? One of them is null – Panagiotis Kanavos Aug 31 '22 at 09:58
    • Recent VS versions will even show you which variable or property was null if they can – Panagiotis Kanavos Aug 31 '22 at 09:58
    • @PanagiotisKanavos Though when I remodify the `message` variable to something like `var messages = "Testing";` the error still occurred, I'm not sure what's going on... –  Aug 31 '22 at 10:06
    • The line `var messages = notes.Text;` tries to call `Text` on the `notes` variable. If `notes` is null, you'll get an error every time you try to use it. – Panagiotis Kanavos Aug 31 '22 at 10:13
    • @PanagiotisKanavos The thing is the same error still occurred even when the `var messages = notes.Text` is not null. –  Aug 31 '22 at 12:50
    • Ok my problem is solved, apparently there this line of code that causing the `messages` variable value to be null. –  Sep 01 '22 at 07:05

    0 Answers0