Object reference not set to an instance of an object

ForEverProgrammation Posted messages 8 Status Member -  
 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)
 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

  1. Anonymous user
     
    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 reminder
     Thing myThing;//declaration myThing = new Thing(); //initialization myThing = AValue; //another initialization 


    --
    When I was little, the Dead Sea was just sick.
    George Burns
    0
    1. ForEverProgrammation Posted messages 8 Status Member
       
      I'll take a look at all that tomorrow. Thank you.
      0
  2. Dalfab Posted messages 638 Registration date   Status Member Last intervention   102
     
    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.
    0
  3. ForEverProgrammation Posted messages 8 Status Member
     
    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
    0
  4. Anonymous user
     
    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
    0
    1. ForEverProgrammation Posted messages 8 Status Member
       
      Everything was created with the designer VS... Strange


      Like this what (I deleted some things in editing):
      0
  5. ForEverProgrammation Posted messages 8 Status Member
     
    I will see if recreating the project can help.

    --
    // Je t'ai aidé et j'ai besoin d'aide
    0
  6. Anonymous user
     
    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
    0