Solved

NUll Reference exception was not handled

Posted on 2006-10-21
4
437 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
ID: 17781008
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
ID: 17781021
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
ID: 17781221
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
ID: 17781310
catch (System.NullReferenceException caught)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
A short film showing how OnPage and Connectwise integration works.

919 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

22 Experts available now in Live!

Get 1:1 Help Now