0

whenever I try to add a tileSlider to a grid, an NRE is caused. I can't explain why, hopefully someone of you can help me with that.

EDIT2: tileSlider is a custom control provided here: http://tileslider.codeplex.com/ I tried to use it in my app and failed to implement it in c#-code behind.

code:

    private void addNormalTiles()
    {
        foreach (KeyValuePair<TileSlider, int[]> slider in (App.Current as App).normalTiles)
        {
            TileSlider x = slider.Key;
            x.Size = 228;
            Grid.SetColumnSpan(x, 2);
            Grid.SetRowSpan(x, 2);
            Grid.SetRow(x, slider.Value[0]);
            Grid.SetColumn(x, slider.Value[1]);
            ContentPanel.Children.Add(x);
        }
    }

thanks

EDIT:

   System.NullReferenceException wurde nicht behandelt. 
   Message=NullReferenceException StackTrace: at     
   ScottIsAFool.Controls.TileSlider.TileSlider_Loaded(Object sender, RoutedEventArgs e)
   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate       
   handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs,
   Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName) 
roqstr
  • 554
  • 3
  • 8
  • 23
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) –  Jul 02 '12 at 12:02

3 Answers3

2

I think one of these two panel issues might be your problem. From the documentation remarks;

  • Do not use this collection with derived Panel classes; use the InternalChildren collection instead.

  • This property returns null if the Panel is data bound.

http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.aspx

IvoTops
  • 3,463
  • 17
  • 18
1

Well it's hard to know given the code you've written. A stack trace would help. Possible options:

  • App.Current might not be an App (a cast is better than using as here, given that you're using it unconditionally)
  • App.Current might be null
  • App.Current.normalTiles
  • slider.Key could be null
  • Grid could be null
  • slider.Value could be null
  • ContentPanel could be null
  • ContentPanel.Children could be null

The stack trace should give you some help determining which of those is the case. Judicious use of the debugger (put a breakpoint on the foreach statement) should help too.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • better? System.NullReferenceException wurde nicht behandelt. Message=NullReferenceException StackTrace: at ScottIsAFool.Controls.TileSlider.TileSlider_Loaded(Object sender, RoutedEventArgs e) at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName) – roqstr Jul 02 '12 at 12:14
  • @roqstr: The important part is getting a *line number* so you know which line it failed at. On the other hand, your method isn't mentioned in that stack trace... what's TileSlider_Loaded? – Jon Skeet Jul 02 '12 at 12:16
  • seems to be part of the tileSlider class, packed in a DLL and not touched by me yet. – roqstr Jul 02 '12 at 12:16
  • @roqstr: Please add all of this information to your question, including what TileSlider is in the first place, and how that stack trace relates to the code you've posted. – Jon Skeet Jul 02 '12 at 12:17
  • @roqstr: It's entirely possible that the DLL isn't validating its input as well as it should, but that you still have a bug around populating the slider to start with. I suggest you fetch the source of TileSlider and then debug against that. – Jon Skeet Jul 02 '12 at 12:32
1

Stick a breakpoint at the top of the method, step through it line by line and review the value of each object just before you run that line (either in your watch window, or by hovering the mouse over the instance). You are looking for when the value of that variable / instance is null.

I would start with:

(App.Current as App).normalTiles

This line could have 3 things wrong with it: App.Current is null, App.Current as App yields null because .Current is null or not an App, or normalTiles is null.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • it also crashes when I just use this: ContentPanel.Children.Add(new TileSlider()); – roqstr Jul 02 '12 at 12:05
  • @roqstr In that case: `ContentPanel` could be null or `Children` could be null, assuming you mean you are getting a NRE when you say "crashes". – Adam Houldsworth Jul 02 '12 at 12:06
  • cant be the contentPanel or children, because this is working: ContentPanel.Children.Add(new TextBox()); – roqstr Jul 02 '12 at 12:09
  • @roqstr I'm not going to sit here and piecemeal debug an app by guessing for you. I have no idea why it crashes with a `TileSlider`, is this a custom control? – Adam Houldsworth Jul 02 '12 at 12:10
  • ofc, added through a dll and should work fine, at least the exampleproject does. – roqstr Jul 02 '12 at 12:12