Reputation: 1325
I am trying to change a WPF I created (without using MVVM and DataBinding) into a WPF that does use those magnificient features.
I have set up a Person Model with multiple properties (too lengthy to list here) with all of the gets and sets and even the NotifyProtertyChanged attribute such as the ID and Full Name attributes.
Now I want to (launching the code with the click of a button to ensure I know when it starts for now while I am writing the code) have my WPF application go through a list of values, compare it to an online XML Document and retrieve a list of data from it for each person in the list matching the value looked for and of course set some of the model's properties.
An example: - Looking for "Lewitt" - The XML Document has 4 people with the Last name Lewitt so it adds those 4 people to my list (comprising items set as the Person model class i have set up before) and setting the values for each item's ID and FullName (shortened for the example).
This is my informationProvider Class that I implemented so far when search for a certain value (called searchstring):
public List<Person> GetPeople(string searchstring)
{
string url = string.Format("{0}{1}", BaseURL, HttpUtility.HtmlEncode(searchstring));
XDocument doc = XDocument.Load(url);
var names = (from s in doc.Root.Elements("Persons").Where(s => s.Element("Lastname").Value.Equals(searchstring, StringComparison.InvariantCultureIgnoreCase))
select new Person
{
ID = Convert.ToInt32(s.Element("id").Value),
fullName = s.Element("Firstname").Value + " " + s.Element("Lastname").Value
}).OrderBy(n => n.fullName).ToList();
return people;
}
I try to launch this process for my list of items as such through my button click (again, only for testing purposes) - I have set an example value of "Jane Doe" which does exist in teh XML Document we are looking in in the InformationProvider class:
List<Person> persons= new List<Person>();
persons= informationProvider.GetPeople(searchShowName);
My issue is that when I launch the code and click on teh button for testing, it throws an error stating "Object reference not set to an instance of an object." and highlights my line
persons= informationProvider.GetPeople(searchShowName);
how can I fix this?
Upvotes: 0
Views: 1066
Reputation: 32719
persons= informationProvider.GetPeople(searchShowName);
this throws error because information provider hasn't been instantiated.
by looking at your method GetPeople, it is safe to say that you can make it static like this
public static List<Person> GetPeople(string searchstring)
and then call it directly from the class name.
Upvotes: 1
Reputation: 50692
You need to make GetPeople static:
class InformationProvider
{
public static List<Person> GetPeople(string searchstring)
{
//...
}
}
or create an instance of the informationProvider class:
InformationProvider informationProvider = new InformationProvider();
List<Person> persons = informationProvider.GetPeople(searchShowName);
Note how I changed the casing and I did not create a redundant empty list.
Upvotes: 1