0

I'm getting a error Object reference not set to an instance of an object. I think i'm not initialize something.

This is the code; the error is on line if (btn.Background.Equals(new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)))):

private void vbOpenGuiaMaestro_Tapped(object sender, TappedRoutedEventArgs e)
{
    Button btn = default(Button);
    if (btn.Background.Equals(new SolidColorBrush(Color.FromArgb(0, 0, 0, 0))))
    {
         btn.Background = materiaColor;
         btn.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
         stckTeachersGuideClosed.Visibility = Visibility.Visible;
         stckTeachersGuideOpened.Visibility = Visibility.Collapsed;
    }
    else
    {
         btn.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
         btn.Foreground = new SolidColorBrush(Color.FromArgb(255, 140, 140, 140));
    }
 }
rae1
  • 6,066
  • 4
  • 27
  • 48
Mikasuki
  • 35
  • 1
  • 8
  • btn is null? Could you please make sure it's the right control? – AD.Net Mar 20 '14 at 13:47
  • What is the value of btn if you put a breakpoint on that line? I'm guessing it's null. – Dutts Mar 20 '14 at 13:47
  • 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) – default Mar 20 '14 at 13:48
  • 1
    @Default no it isn't a duplicate. This is a specific question about the written code. Your link is a general theoretical question. – Recipe Mar 20 '14 at 13:50
  • @Recipe just looking at the Related questions I see 10 questions with a similar title. The one I linked has the most complete answer (IMO) and that is why I tagged that one. Maybe not an exact duplicate, but this kind of question has been asked here **many** times before and by just browsing some of the other ones the OP should be able to resolve his issue. To me, yet another *"Object reference not set"* question just adds noise to SO. – default Mar 20 '14 at 13:55

4 Answers4

6
default(Button)

gives you null as Button is a reference type. I guess you wanted

Button btn = (Button)sender;
Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • his problem occurs before initializing it to default(Button) – Sasha Mar 20 '14 at 13:49
  • 1
    @OleksandrPshenychnyy No, the first if is just a copy of the actual line, this is just unclear because of the formatting – Luke Marlin Mar 20 '14 at 13:50
  • 1
    This should probably check whether the type of sender actually is `Button` before casting. – rae1 Mar 20 '14 at 13:52
  • There will be no cast exception if this event handler is bound to a Button. sender will always be Button. – BlueM Mar 20 '14 at 13:58
  • Ok is fine the error is fix bot now I receiving another error: Unable to cast object of type 'Windows.UI.Xaml.Controls.StackPanel' to type 'Windows.UI.Xaml.Controls.Button'. – Mikasuki Mar 20 '14 at 13:59
  • Not good, sounds like your button is inside the stack panel (?) but without the actual xaml it is not easy to help here. – Wiktor Zychla Mar 20 '14 at 14:04
1

Problem is your sender is StackPanel and you're casting it to Button. Wrong!

StackPanel stackPanel = sender as StackPanel;
if(stackPanel != null)
{
        if (stackPanel.Background.Equals(new SolidColorBrush(Color.FromArgb(0, 0, 0, 0))))
        {
            stackPanel.Background = materiaColor;               
            stckTeachersGuideClosed.Visibility = Visibility.Visible;
            stckTeachersGuideOpened.Visibility = Visibility.Collapsed;
        }
        else
        {
            stackPanel.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
        }
}
SirH
  • 535
  • 2
  • 7
0

You error is here Button btn = default(Button);. This is setting the button to null since that's the default for the class.

You should instead be casting the sender object as Button and then verifying the sender is not null (in case the event was not originated by a button).

var btn = sender as Button;
if (btn != null)
{
    ...
}
rae1
  • 6,066
  • 4
  • 27
  • 48
0

In the sender object, you have a reference to the control on the UI that triggered this event. It is of type object but you can cast it to your needed type (in this case, Button).

So get a reference to the button like this :

var btn = (Button) sender;

Alternatively, you can also use :

var btn = sender as Button;

The difference is that in the first case, if the cast is unsuccesful, it will throw you an InvalidCastException. In the 2nd way, it will silently try to cast it even if the type you specify was not the intended one but will crash further on when the object will be misused.

VasileF
  • 2,856
  • 2
  • 23
  • 36