Hi,
I want to check the value of the combobox in one form from another.
I have this code in button2_Click event
Blagajna bl = (Blagajna)Application.OpenForms["Blagajna"];
ComboBox cb = (ComboBox)bl.Controls["comboBoxVal"];
String kom = (cb.SelectedItem).ToString();
and
Blagajna bl = new Blagajna();
in partial class definition.
I get this errorr: "Object reference not set to an instance of an object."
Could anybody help me please what is the problem with the code?
Thanks in advance.
Loading
NelPosted Jun 26, 2012, 4:17 AM
VulpesPosted Jun 25, 2012, 9:07 AM
There's also an easier way to do it which is to change the combobox's default Modifiers property from private to internal. You can then simply do:
Jignesh TrivediPosted Jun 25, 2012, 8:36 AM
I thing your cb.SelectedItem property is null and you call is override method .Tostring()
try..
String kom = Convert.ToString(cb.SelectedItem);
But find out why SelectedItem is null.
hope this will help.
NelPosted Jun 25, 2012, 7:35 AM
FOR AN INSTANCE OF Blagajna.
I debug the line and get null for kom, but I don't understand why, when the combo in the form Blagajna which I call in the second form has value "EUR" or some other but not null:(
S GPosted Jun 25, 2012, 7:28 AM
This error is occur because u forget to make the instane of object. When we create an object we use the keyword "new" . So in u r program i dont know on which line u get an error . But trace the line of the error and give its instance. Currently it can get a null reference. There is chances that the error at combobox. Do this . I will help you exactly.
NelPosted Jun 25, 2012, 7:27 AM
I get the error in this line:
String kom = (cb.SelectedItem).ToString();
Jignesh TrivediPosted Jun 25, 2012, 7:13 AM
this type of error come when you access property of null object or casting null to other object.
can you please share which line you got error.
thx.