(Sobbing out of frustration)
How do you avoid annoying NRE in this case? The output of the code is correct, but I wish to get rid of the NRE.
Background:
There are some buttons on a form, some of which represent seats of a classroom. The other buttons are irrelevant because I only need the seat buttons, and only the seat buttons have tags that has value.
So, I loop through all buttons and read their tags, if it is not empty, then keep that button in a list. The rest are ignored.
However, when the code is run, NRE pops up at the fourth line, the line starting with "try".
foreach (Button seatbutton in this.Controls)
{
string betta;
try { betta = seatbutton.Tag.ToString(); }
catch { betta = ""; }
if (!string.IsNullOrEmpty(betta))
{
seatbuttons.Add(seatbutton);
}
}
This is the shortest, most straightforward example of this type of NRE in my code. There are several more.
I have searched the web, and most responses are among the lines of: "Bad coding habits caused this."
As you can probably tell, I'm quite new at this whole thing and haven't even had the time to build habits yet. Can you guys help? Perhaps with some tips for GOOD coding habits?
T_T thanks!