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

Jignesh TrivediRank #16 · 50.3K reputationPosted Jun 25, 2012, 3:13 AM EDT
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.
NelRank #2,060 · 702 reputationPosted Jun 25, 2012, 3:27 AM EDT
I get the error in this line:
String kom = (cb.SelectedItem).ToString();
S G91 reputationPosted Jun 25, 2012, 3:28 AM EDT
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.
NelRank #2,060 · 702 reputationPosted Jun 25, 2012, 3:35 AM EDT
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:(
Jignesh TrivediRank #16 · 50.3K reputationPosted Jun 25, 2012, 4:36 AM EDT
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.
VulpesRank #55 · 29.2K reputationPosted Jun 25, 2012, 5:07 AM EDT
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:
NelRank #2,060 · 702 reputationPosted Jun 26, 2012, 12:17 AM EDT