So I have three buttons that have its own functionalities,
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.