Object reference not set to an instance of an object
ForEverProgrammation
Posted messages
8
Status
Member
-
Anonymous user -
Anonymous user -
(The object reference is not set to an instance of an object)
Hi
Usually, I can always handle this kind of exception, but this time I don't understand. In the example below, it generates this exception (which is my case)
the place where it generates the exception is from line 3, it's strange because earlier it worked perfectly and it was exactly the same code. I've really looked into it, but it had nothing to do with it. (I can't explain it any better)
Hi
Usually, I can always handle this kind of exception, but this time I don't understand. In the example below, it generates this exception (which is my case)
private void DarkTheme() { mainTextBox.BackColor=Color.FromArgb(34,34,34); mainTextBox.ForeColor = Color.White; this.BackColor = Color.FromArgb(64, 64, 64); pnlBtnsMain.BackColor = Color.FromArgb(64, 64, 64); } the place where it generates the exception is from line 3, it's strange because earlier it worked perfectly and it was exactly the same code. I've really looked into it, but it had nothing to do with it. (I can't explain it any better)
6 answers
-
Hello
at the moment when it crashes.
You explore each object in the line to see which one is worth "null".
Be careful, a property of an object is also an object.
From this information, you need to trace the code chronologically between this moment and the moment of its declaration to find out why it hasn't been initialized.
As a reminderThing myThing;//declaration myThing = new Thing(); //initialization myThing = AValue; //another initialization
--
When I was little, the Dead Sea was just sick.
George Burns -
Hello,
You need to investigate to see if mainTextBox is uninitialized or set to null for one of your object's instances before the function call.
You should be able to find it while debugging. -
I did as Whismeril told me, but the colors are already initialized (
private void LightTheme() //I switched to light theme { mainTextBox.BackColor = Color.White; mainTextBox.ForeColor = SystemColors.ControlText; this.BackColor = SystemColors.Control; pnlBtnsMain.BackColor = SystemColors.Control; }) and it's the same, it's starting from line three, I understood what you suggested, it's like below:private void DarkTheme() { Color aColor; aColor = new Color(); aColor = System.Drawing.Color.FromArgb(64, 64, 64); mainTextBox.BackColor = aColor //... } //Just below private void LightTheme() { Color anotherColor; anotherColor = new Color(); anotherColor = System.Drawing.Color.FromArgb(255,255,255,255); mainTextBox.BackColor = anotherColor; //... }and it's not working...
What can I do?
--
// I helped u and I need help -
I told you to start by looking at what is null....
I didn't say it was BackColor, it could very well be mainTextBox as Dalfab suggested
it could also (though very unlikely) be anotherColor, but in that case it means that the FromArgb method has a problem.
--
When I was little, the Dead Sea was just sick.
George Burns -
I will see if recreating the project can help.
--
// Je t'ai aidé et j'ai besoin d'aide -
Hello
before recreating the project, did you try to do what was suggested to you?
If so, who is null?
Also, I don't understand anything in your screenshot
--
When I was little, the Dead Sea was only sick.
George Burns
