Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I cannot run this code correctly. It says "Object reference not set to an instance of an object." I am using WPF format in C Sharp. Thanks.

What I have tried:

private void btnClear_Click(object sender, RoutedEventArgs e)
{
foreach (Control control in this.Controls)
{
if (control is TextBox)
{
TextBox textBox = control as TextBox;
textBox.Clear();
}
}


}
Posted
Updated 10-Aug-16 2:18am
Comments
Kats2512 10-Aug-16 3:22am    
The best way is to debug, object reference not set to an instance of an object is something that only you can fault find.

You are trying to use something that is null. It means you either set some textbox to null, or you never set it to anything at all.

I suggest you put a breakpoint on the btnClear_Click event and see where you code crashes, then review the stack trace.
Bernhard Hiller 10-Aug-16 3:26am    
Are you sure it happens within those lines? They look pretty safe...
Rather in a different event handler, e.g. invoked by the textBox.Clear statement.
Add a breakpoint at "foreach" and debug.
[no name] 10-Aug-16 3:32am    
Can you give me a help please? Does WPF Window has a property Controls?
Bernhard Hiller 10-Aug-16 3:49am    
:-)) But how did he get that compiled? He shows a run-time exception, not a compile-time exception. Is it a Voodoo compiler?
[no name] 10-Aug-16 3:53am    
No idea how to compile this, I have begun to doubt me :-)

I would say your code snippet does not even compile, because it is a mix between WPF and Windows Form.

WPF: private void btnClear_Click(object sender, RoutedEventArgs e)
WinForm: foreach (Control control in this.Controls). WPF "controls" do not have a property Controls...

Following does clear TextBox in WPF:
C#
private void Button_Click(object sender, RoutedEventArgs e)
{
    ClearTextBoxes(this);
}

private void ClearTextBoxes(DependencyObject parentDependencyObject)
{
    int visualChildrenCount = VisualTreeHelper.GetChildrenCount(parentDependencyObject);
    for (int i = 0; i < visualChildrenCount; i++)
    {
        // Retrieve child visual 
        DependencyObject dependencyObject = VisualTreeHelper.GetChild(parentDependencyObject, i);

        TextBox textBox = dependencyObject as TextBox;
        if (textBox != null)
        {
            textBox.Clear();
        }
        else
        {
            ClearTextBoxes(dependencyObject);
        }
    }
}

But I would suggest you to go for a more general solution like described here:
c# - Find all controls in WPF Window by type - Stack Overflow[^]
I hope it helps.
 
Share this answer
 
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
[no name] 10-Aug-16 3:42am    
Can you give me a hint please? The button click private void btnClear_Click(object sender, RoutedEventArgs e) Looks like WPF, but does WPF has this.Controls? What I'm missing? Thank you in advance.
OriginalGriff 10-Aug-16 3:54am    
That code looks pretty safe - you check that the "as" conversion should work before you try it, so it shouldn't fail - so you really do need to look at exactly what is going on here with the debugger. If that code is failing then there is something very odd going on, and you need to gather data to find out what before we can say "do this". I'd suspect it's not that code that produces the error, or you have simplified the code too much for demo reasons, and lost the problem!
[no name] 10-Aug-16 3:56am    
But it will also not compile .... confused :(
OriginalGriff 10-Aug-16 4:01am    
If it doesn't compile, then it doesn't give a null reference error! :laugh:
What error message do you get from the compiler, and where?
[no name] 10-Aug-16 4:07am    
Compiler Error (translated by me): "WPF Window does not have a definition 'Controls'".

In WPF one Needs to use VisualTreeHelper to Loop over the visual controls, at least that is what I do until now.
Thank you to those who replied. You were all such a heroes. Sorry I am still newbie in programming.
 
Share this answer
 
Comments
[no name] 10-Aug-16 8:22am    
Thank you for accepting. Nobody here is a hero :-)

_Please_ delete this solution to prevent from downvotes ;)
Regards

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