-1

I am attempting to fill a rectangle, called Key, with a Local Resource (a LinearGradientBrush) called PrecipHour. When I run the code below, a nullreferenceexception is thrown - Can I not just cast a resource as a LinearGradientBrush?

C#

 key.Fill = (LinearGradientBrush)this.Resources["PrecipHour"];

XAML

<LinearGradientBrush x:Key="PrecipHour" EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FF661C00" Offset="0"/>
            <GradientStop Color="#FF0011BF" Offset="1"/>
            <GradientStop Color="#FF265CEC" Offset="0.838"/>
            <GradientStop Color="#FF2EFF00" Offset="0.445"/>
            <GradientStop Color="#FFF3FF00" Offset="0.253"/>
            <GradientStop Color="Red" Offset="0.125"/>
            <GradientStop Color="#FF65E040" Offset="0.626"/>
</LinearGradientBrush>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Kevin
  • 383
  • 2
  • 11
  • It should work. Where are you putting the resource? – Kevin Gosse Feb 12 '15 at 22:11
  • phone:PhoneApplicationPage.Resources – Kevin Feb 12 '15 at 22:27
  • Which is correct. Actually, after reading your code again, I realize that it can't possibly throw a null reference exception, unless `key` is null. Are you sure the exception is thrown by this line? You should execute the code step by step with the debugger and check the value of the variables to understand what's going on – Kevin Gosse Feb 12 '15 at 22:29
  • Commented every line in the method and executed the code. Ran fine. Uncommented the line in the question and it threw the exception. Clearly the Hour Precip contains is correct otherwise the rest wouldn't even fire. – Kevin Feb 12 '15 at 22: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) – Peter Duniho Feb 12 '15 at 22:39
  • This isn't a duplicate - I know what a nullreferanceexeption is, but I am trying to figure out why it is being called on something that doesn't seem to be possibly null. – Kevin Feb 12 '15 at 22:51

1 Answers1

1

A NullReferenceException means something is null that is being treated as non-null. If this were a cast problem, then you'd be getting an InvalidCastException. Your code is:

key.Fill = (LinearGradientBrush)this.Resources["PrecipHour"];

There are three possible things are having their properties accessed and could therefore be null:

  1. key
  2. this
  3. this.Resources

this can never be null, so we are left with 1 and 3. You can use a debugger, or even just Debug.Assert statements to figure out which of those is failing.

NextInLine
  • 2,126
  • 14
  • 23
  • I don't think `this.Resources` can possibly be null. Only `key` is left. My psychic powers tell me that @Kevin is trying to access it in the constructor of the page, before the call to `InitializeComponents` – Kevin Gosse Feb 12 '15 at 22:45
  • I'm not - it doesn't run before the user taps on two buttons. The line is contained in the method UpdateKey, which is run when the user taps on a button and fires a method which then fires UpdateKey. This gives the program much more time then it needs to initialize itself. – Kevin Feb 12 '15 at 22:49
  • I tried to do Application.Current.Resources.Count().ToString() to see if there was some kind of issue with Resources, and it threw a NotImplmenentedExeption? – Kevin Feb 12 '15 at 23:01
  • So when you have a chained command like Application.Current.Resources.Count().ToString() and can't tell where the exception is coming from, it's best to split it into separate lines (assign each to a variable) so we can tell where the exception is coming from (or use a debugger to figure it out). Just knowing that something in that sequence throws a NotImplmenentedExeption isn't very helpful. – NextInLine Feb 12 '15 at 23:59