jenn5175 Posted September 18, 2003 Posted September 18, 2003 Okay, this is going to be remedial for most of you. I have a combobox which needed to be associated with both a code(varchar) and the display name (varchar). So I made a new class: public class cmboObject public _name as string public _code as string public sub new(byval name as string, byval code as string) _name = name _code = code end sub public readonly property Name() as string get return _name end get end property public readonly property Code() as string get return _code end get end property public overrides function ToString() as string return name end function end class I use the same comboboxes for multiple questions (the app is a survey showing one question at a time) so I made a variable to hold their choice once they move to the next question Dim brochureMake1 as cmboObject Dim brochureMake2 as cmboObject Then when the comboboxes were filled from a database I used: Dim cmboObjHolder as cmboObject while dtr.read() cmboObjHolder = new cmboObject(dtr("field1"), dtr("field2")) cmboMake1.Items.Add(cmboObjHolder) end while Now when they got to the combobox and hit "Next Question" I would simply save that cmboObject: brochureMake1 = cmboMake1.SelectedItem This above code works great. However, once the survey is submitted, I need to clear all of the variables to get ready for the next person taking the survey. I have tried everything to reset brochureMake1 and brochureMake2 to emptiness (tries = "", = nothing, etc) but keep getting "NullCastException". I am thinking I need to make a "set" function and just set name and code to "" but I cannot get it right. Any suggestions? Jenn Quote
Administrators PlausiblyDamp Posted September 18, 2003 Administrators Posted September 18, 2003 You could just add a method like public Sub Clear _name="" _code="" End Sub and then call it with cmboObjHolder.Clear Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jenn5175 Posted September 19, 2003 Author Posted September 19, 2003 That's almost exactly what I tried already. Here's my attempt and the failed result. Let me know if I implemented it incorrectly: 'cmboObject.vb - class defination file Public sub Empty() _name = "" _code = "" end sub 'frmMain.vb - main form file public sub ClearVariables() brochureMake1.Empty brochureMake2.Empty ... end sub When the survey first starts it calls ClearVariables() and everything is fine. However, once the survey is filled out once, when ClearVariables() is called again I get the following error (at the line calling Empty): A managed NullReferenceException occurred at Application::Run+0xf What am I doing wrong? I tried and it errors regardless of if anything was set in brochureMake1/2 or not. Quote
aewarnick Posted September 19, 2003 Posted September 19, 2003 NullReferenceException means that you tried to access a variable that is not initialized yet, not created. Check your code for anthing that you disposed of or forgot to create. Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.