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.

10
  • Have you tried break pointing or try/catching to see where it is throwing the error? Commented Apr 19, 2016 at 8: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 Commented Apr 19, 2016 at 8:39
  • When you step through in break mode, can you test each value and see if any are null? Both gridcontrol and query. Commented Apr 19, 2016 at 8: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? Commented Apr 19, 2016 at 8: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 Commented Apr 19, 2016 at 8:46

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

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()
ObservableCollection<Personnel> _Personnel = new ObservableCollection<Personnel>(); 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<Personnel>(context.Personnels); PersonCollection.Source = this._Personnel; }
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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.