Solved

NUll Reference exception was not handled

Posted on 2006-10-21
4
435 Views
Last Modified: 2010-04-16
Hi there,
I have 2 lists boxes. The 1st one has multiple items. The 2nd listbox is empty. The user selects the items to be transferred to the 2nd listox and hits the ADD button. The process works the other way if the user wants to get rid of the items selected on the 2nd listbox. I just had a chance to check the adding items from listbox1 to listbox2. It works fine, however if the user doesn't choose ant item from the listbox1 and just hits the ADD button I get this following message;

"NUll Reference exception was not handled"
lstChosenSubs.Items.Remove(lstChosenSubs.SelectedItem.ToString());

I guess I need to use the try and catch to handle this exception,right? Any idea how should I use it?
Regards,
0
Comment
Question by:operatortobe
  • 3
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
Comment Utility
Simple check can solve the problem:

if ( lstChosenSubs.Items.Count > 0 &&  lstChosenSubs.SelectedItem != null )
{
    lstChosenSubs.Items.Remove(lstChosenSubs.SelectedItem.ToString());
}
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
We need to use exception handling in methods which suppose to throw exceptions - like different I/O methods. Null reference exception is just bug - if you catch it, you can hide your own bugs like using uninitialized references.
0
 

Author Comment

by:operatortobe
Comment Utility
Hi Alex,
I have tried your code and it works just fine. I have also modified it and applied it to Add button as well. Thanks much. I just wonder what if I wanted to use Try and catch instead... Would it be some thing like this;

try
  {
    if(lstSubjects.Items.Count > 0 && lstSubjects.SelectedItem != null)
            {

                lstChosenSubs.Items.Add(lstSubjects.SelectedItem.ToString());)
       }
   }
catch (System.FormatException caught)
     {
          result.Text = caught.Message;
  }
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
catch (System.NullReferenceException caught)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now