Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Alright I'm doing this form that's supposed to change it's look when I choose a specific item from a drop down menu,

C#
private void ChangeGUI(int option)
   {
       switch (option)
       {
           case 0:
           case 1:
               labelAntal.Visible = true;
               txtAntal.Visible = true;
               labelWeight.Visible = false;
               txtWeight.Visible = false;
               break;

           case 2:
               labelAntal.Visible = false;
               txtAntal.Visible = false;
               labelWeight.Visible = true;
               txtWeight.Visible = true;
               break;
       }

   }


I keep getting a nullReferenceException on txtWeight.Visible , that is when i choose the 3'rd option from the drop down menu.

Can anyone see what I'm doing wrong here?:(
Posted
Comments
bowlturner 3-Jan-14 12:20pm    
What happens when you choose case 0 or 1?
[no name] 3-Jan-14 13:20pm    
Well I have 3 options on my drop down...the first two don't need a new form when choosen however the third does.
bowlturner 3-Jan-14 13:21pm    
Then like both the solution state, the txtWeight probably doesn't exist.
BillWoodruff 3-Jan-14 21:31pm    
"the first two don't need a new form when choosen"

This is critical information for anyone to understand the problem.
Sergey Alexandrovich Kryukov 3-Jan-14 14:10pm    
First of all, impossibly bad code. Why using integer numbers for options and then using then as immediate constants in switch expression, as "magical numbers"? At least use the enum type for options.
—SA

From that? No.
You need to look at variable itself, and make sure that it is attached to a genuine control instance.
Right click the name anywhere in your source code, and select "Find all references" from the context menu.

At least one of them should be an assignment to a new instance (it could well be in the designer.cs file) If there isn't one, then it isn't on your form! (And you need to look at your code and work out what it is and where it should be)
 
Share this answer
 
Comments
[no name] 3-Jan-14 14:31pm    
Hmm alright I solved it..It did exist but apparently the reason was that I'd given all the textfields the value null on initiation :s what a wierd mistake to make lol

thanks alot :)
OriginalGriff 3-Jan-14 14:33pm    
You're welcome!
(We all do silly things from time to time - and I generally end up reading what I meant to write instead of what I actually *did* write...:O )
Karthik_Mahalingam 4-Jan-14 2:45am    
good suggestion.
Does txtWeight exist in the third case?
It could be this text box is dynamically generated or generated on a form that is not active when you are choosing the third option.
 
Share this answer
 

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