1

I have a WPF project with a main Window and a UserControl.

In the Usercontrol I set a GridControl.ItemsSource using gridcontrol.ItemsSource = query.tolist(), but when loading the Usercontrol it throws an Exception:

"The object reference not set to an object"

In spite of the query returning 40 rows of data.

The code is executed in the MainWindow but the UserControl throws the exception.

Ilan
  • 2,762
  • 1
  • 13
  • 24
pooooooneh
  • 19
  • 7
  • Have you tried break pointing or try/catching to see where it is throwing the error? – Blinx Apr 19 '16 at 08:28
  • ye dear friend i did it alot the error throw when i cal usercontrol.but if i remove gridcontrol.itemsource = query.tolist() it does'nt have any problem and usercotrol load with empty gridcontrol – pooooooneh Apr 19 '16 at 08:39
  • When you step through in break mode, can you test each value and see if any are null? Both gridcontrol and query. – Joe Apr 19 '16 at 08:40
  • If you break point at that line (presuming you're using something like visual studio) are either the `GridControl` or `query` null? Is the call to `InitializeComponent()` being made before this line? – Blinx Apr 19 '16 at 08:41
  • yes the erro happen on this line of code when i call usercontrol: panel2 = DockLayoutManager_M.DockController.AddDocumentPanel(DocumentGroup_M, new Uri(@"Test1;component\UserControl2.xaml", UriKind.Relative));but if i remove gridcontrol source setting code from usercontrol.it works – pooooooneh Apr 19 '16 at 08:46
  • A `NullReferenceException` will mean that either the `GridControl` or `query` is null. Is this line after any `InitializeComponent()` call? – Blinx Apr 19 '16 at 08:49
  • i change my code alittel now i have the error on this line of code: gridcontrol.itemsource = query.tolist(); the query is not null but when i keep mouse in gridcontrol writes this.gridcontrol null – pooooooneh Apr 19 '16 at 08:55
  • This will mean that `gridcontrol` hasn't been initialised . In the constructor for the `UserControl` you should have a call to `InitializeComponent()` is this before or after the line that throws the error? – Blinx Apr 19 '16 at 08:58
  • i had my cod in pubic personnel (){} and the initializecomponent was in private Personnel(){}.now i add an initializecomponent befor my cod and i recieved another error: no default constructor found for usercontrol.this error happen on the line i call usercontrol.what can i do – pooooooneh Apr 19 '16 at 09:04
  • Are you able to post the code for your constructor? – Blinx Apr 19 '16 at 09:08

1 Answers1

3

Consolidating the conversation from the comments on the question:

A NullReferenceException is thrown on the line

gridcontrol.ItemsSource = query.ToList();

as gridcontrol is null.

For more information on this Exception see What is a NullReferenceException, and how do I fix it?

I'm guessing that the line in question is before the call to InitializeComponent() in the constructor.

This method initialises all of the controls in the UserControl. Therefore, if trying to use a Control before a call to this method, it will throw a NullReferenceException.

What you will want to see is:

public YourUserControl()
{
    InitializeComponent();
    gridcontrol.ItemsSource = query.ToList();
}
Community
  • 1
  • 1
Blinx
  • 400
  • 4
  • 17
  • thanks alot i do that and now my gridcontrol works.i have a collection view source it has the same error collectionviewsource is null and i set it after initialize method the code is : collectionviewsource.source = an observablecollection() – pooooooneh Apr 19 '16 at 09:55
  • ObservableCollection _Personnel = new ObservableCollection(); CollectionViewSource PersonCollection = new CollectionViewSource(); DevExpress.Xpf.Docking.DockLayoutManager dockmanager; public UC_Personnel(DevExpress.Xpf.Docking.DockLayoutManager _dockManager){ InitializeComponent(); PersonCollection = (CollectionViewSource)this.Resources["MyResource"]; this.dockmanager = _dockManager; this._Personnel = new ObservableCollection(context.Personnels); PersonCollection.Source = this._Personnel; } – pooooooneh Apr 19 '16 at 09:58
  • Glad that the first issue has been fixed, if you could mark this answer as correct that would be great. This new issue seems like it may not be parsing the resource correctly. I can't really say more without any more details. You may want to consider raising a new question. – Blinx Apr 19 '16 at 10:15
  • I solved the problem.thank all of you for your help.it is 3 days i am involved with this errors now some body who i don't know helped me.thank you aloooooooooooooooooot – pooooooneh Apr 19 '16 at 10:20