-3

I've got the following coding where I am trying to step through a couple of if statements, but I've got a problem with setting my label's content to 0;

private void UnderRunBumper()
{
    lblGVMAmount.Content = 0; //Issue here
    if (Convert.ToInt32(txtExternalLength.Text) >= 6000)
        lblUnderRunBumper.Content = "Under-Run Bumper";

    else if (Convert.ToInt32(lblGVMAmount.Content.ToString()) >= 8000)
        lblUnderRunBumper.Content = "Under-Run Bumper";

    else if (cmbBodyType.SelectedIndex == 6 || cmbBodyType.SelectedIndex == 7 || cmbBodyType.SelectedIndex == 8 || cmbBodyType.SelectedIndex == 9 || cmbBodyType.SelectedIndex == 10)
        lblUnderRunBumper.Content = "Under-Run Bumper";
    else lblUnderRunBumper.Content = "";
}

I get the error:

Object reference not set to an instance of an object.

I don't understand. Why can't I set my label's value to 0?

CareTaker22
  • 1,182
  • 1
  • 15
  • 30
  • why are you posting [The same Question from 6 hours earlier](http://stackoverflow.com/questions/36337084/setting-label-content-causes-issues) – MethodMan Mar 31 '16 at 21:25
  • Is lblGVMAmount null when you try to set its Content property to 0? – Matt Dalzell Mar 31 '16 at 21:25
  • @MADsc13nce Yes it is :) – CareTaker22 Mar 31 '16 at 21:26
  • @MethodMan It's not the same question as before. My issue here is not converting the label's value to int, but not being able to set my label's content to ANY value (string/int/double) without my application crashing. – CareTaker22 Mar 31 '16 at 21:28
  • You can't set the property of a null object. Do you initialize lblGVMAmount somewhere else in the code? You need to figure out why it is null when UnderRunBumper() is called. – Matt Dalzell Mar 31 '16 at 21:30
  • Betting you call this method before `InitializeComponent` – Jonesopolis Mar 31 '16 at 21:34
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jason Watkins Mar 31 '16 at 21:38

1 Answers1

1

In a WPF application (or winforms), the UserControl or Window builds itself using a call called InitializeComponent, that's automatically added to the constructor in the code behind.

This call is what initializes your view, and instantiates the objects on it. Don't try to touch UI controls before this method completes.

E.g.

public partial class MyView : UserControl
{
    public MyView()
    {
        var x = myButton; //<Button Name="myButton" /> in xaml
                          // x is null

        InitializeComponent();

        x = myButton;  //x is valid

    }
}
Jonesopolis
  • 23,589
  • 10
  • 63
  • 106
  • Thanks for the answer and explanation! :D I've found the issue, but do no know why it happens. I use my 'UnderRunBumper()' method in a textbox's TextChanged event and this is what is causing my crash for some reason – CareTaker22 Mar 31 '16 at 21:44
  • 1
    Maybe the text is changed during the `InitializeComponent` call, before the label is instantiated. Just speculation. Check your call stack and you'll know. This is a good chance to learn invaluable debugging skills. – Jonesopolis Mar 31 '16 at 21:47
  • I think you are totally right. It's the only explanation that makes sense to me. But thank you for the help and for not being negative like some of the peeps that comment here. – CareTaker22 Mar 31 '16 at 21:49