Solved

NUll Reference exception was not handled

Posted on 2006-10-21
4
439 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Export import database 4 51
Wav problem 4 25
Reading the Web.Config using IIS 7.5? 4 35
C# bracket error 3 30
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

816 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

11 Experts available now in Live!

Get 1:1 Help Now