Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi all,

I have this problem which i hope you can help me to solving ..

I have a listbox ,comboBox,textBox.

when i select an item from the listbox and then populate comboBox

and choose from it appere datain textbox..

the code is working fine in the first time ,,But when i try to select another item from list box this exception show up,,[Object reference not set to an instance of an object]

this Listbox code>>
C#
private void stdlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            comboBox1.ItemsSource = null;
            selectedId = ((DataRowView)stdlist.SelectedItem).Row.ItemArray[0].ToString();
            adapter1 = new SqlDataAdapter("SELECT RESULTS.student_ID, STAGES.stage_name, RESULTS.test_ID FROM RESULTS INNER JOIN" +
 " TESTS ON RESULTS.test_ID = TESTS.test_ID INNER JOIN " +
 " STAGES ON TESTS.stage_ID = STAGES.stage_ID" +
 " WHERE  (RESULTS.student_ID = @student_ID) ORDER BY RESULTS.student_ID", connect);

            adapter1.SelectCommand.Parameters.AddWithValue("@student_ID", selectedId);
            dt1 = new DataTable();
            adapter1.Fill(dt1);
            
            comboBox1.ItemsSource = dt1.DefaultView;


the error in this code,,
C#
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            date.Text = "";
            correctxt.Text = "";
            falsetxt.Text = "";
            mark.Text = "";
         // the error show up here,,
   selectedTst = ((DataRowView)comboBox1.SelectedItem).Row.ItemArray[0].ToString();


the problem is ..I can't select another item from ListBox ..because the combobox is in handler..
how can i exit the handler?? i mean how can i select another item from ListBox??
thank you,,
Posted
Updated 14-May-12 14:08pm
v2
Comments
Sergey Alexandrovich Kryukov 14-May-12 20:35pm    
In what line, exactly?
--SA
TageTage 14-May-12 21:42pm    
in this line..
selectedTst = ((DataRowView)comboBox1.SelectedItem).Row.ItemArray[0].ToString();
VJ Reddy 16-May-12 6:34am    
Thank you for accepting the solution :)

Run it using the debugger; it will show you exact like where the exception was thrown. Put a break point on this line, restart the application, reproduce the same steps to run into the problem. When execution stops, examine members/variables you use in the next line. You de-reference some object which is not supposed to be null, but it's null. Review you logic: either make sure the member/variable is initialized by this point of execution or check it for null and don't do the offending operation. That's it. This kind of exception is one of the easiest to locate and fix.

Good luck,
—SA
 
Share this answer
 
Comments
TageTage 14-May-12 20:52pm    
I do so. Before. And found that the reason the item is still in use
i tried put a blank value to the source, but not resolved
I want to know what should i write to avoid the problem
Sergey Alexandrovich Kryukov 15-May-12 11:42am    
It totally depends on your logic. Design it the way... see above.
--SA
VJ Reddy 15-May-12 4:52am    
Good suggestion. 5!
Sergey Alexandrovich Kryukov 15-May-12 11:42am    
Thank you, VJ.
--SA
Maciej Los 15-May-12 10:18am    
Good answer, my 5!
The reason may be that while the ItemsSource is being assigned in the ListBox SelectionChanged event the SelectionChanged event of ComboBox may be firing and the above error may be occuring.

To avoid SelectionChanged event of ComboBox while ItemsSource is being populated declare a Boolean Flag and use it as below
C#
//Declare a boolean flag in the beginning of execution
bool loadingComboBox = false;

//In the SelectionChanged event handler of ListBox

loadingComboBox = true;
comboBox1.ItemsSource = dt1.DefaultView;
loadingComboBox = false;

//In the SelectionChanged event handler of ComboBox
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (loadingComboBox) return;
    date.Text = "";


Please give it a try. I cannot say certainly but it may be helpful.
 
Share this answer
 
Comments
TageTage 15-May-12 8:44am    
thank you.. i will try't now
Maciej Los 15-May-12 10:18am    
Yes, yes, yes! 5!
VJ Reddy 15-May-12 10:22am    
Thank you very much, losmac :)
Sandeep Mewara 15-May-12 10:34am    
Good 5!
VJ Reddy 15-May-12 10:41am    
Thank you, Sandeep :)
This is off topic, but you should start learning how to use the MVVM pattern. It will probably make this type of error less common.
 
Share this answer
 
Comments
TageTage 15-May-12 1:35am    
any help!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900