Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > Visual Studio 2005
|
Visual Studio 2005 For discussing Visual Studio 2005. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 26th, 2009, 01:15 PM
Registered User
 
Join Date: Mar 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Null Reference exception could occur...

[IMG]file:///C:/DOCUME%7E1/raldana/Local%20Settings/Temp/moz-screenshot.jpg[/IMG][IMG]file:///C:/DOCUME%7E1/raldana/Local%20Settings/Temp/moz-screenshot-1.jpg[/IMG]Hi, I am learning visual studio .net with the following book:

Beginning Visual Basic® 2005
Thearon Willis and Bryan Newsome

On chapter 5 : Deleting from an ArrayList

Try it Out, Step 3

I write the following code accordingly to the instruction and I am getting a warning:

Property 'SelectedCustomer' does not return a value on all code paths. A null reference exception could occur at run time when the result is used

The question is why and how to fix this null issue?

Public ReadOnly Property SelectedCustomer() As Customer
Get
If lstCustomers.SelectedIndex <> -1 Then
‘Return the selected customer
Return lstCustomers.Items(lstCustomers.SelectedIndex)
End If
End Get
End Property

Thank you,

Ricardo
 
Old March 26th, 2009, 01:30 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hello.

This is happening because if the selected index is -1, then you will be returning a null pointer (There is no else). It's said that not all code path returns a value because of that.

To solve it, just add return null after the end if. Be aware that if there is not a selected index, this in fact will return null, so you will have to check for a null pointer when you call this function.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old March 26th, 2009, 01:43 PM
Registered User
 
Join Date: Mar 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Gonzalo,

I am very new with VB.net, I try

Code:
Get
            If lstCustomers.SelectedIndex <> -1 Then
                'Return the selected customer
                Return lstCustomers.Items(lstCustomers.SelectedIndex)

            End If

            Return DBNull

        End Get
but, it says that cannot be use with as an expresion.(i try it as well with Return Null, null is no longer supported), so I write "Return Nothing" will that be ok?
 
Old March 26th, 2009, 01:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

That's perfect. null, nothing, everything depends of the language...
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
The Following User Says Thank You to gbianchi For This Useful Post:
jrmrcol (March 26th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Null reference exception was unhandled labby C# 2005 0 April 24th, 2007 06:14 AM
Null Reference Exception was unhandled labby C# 2005 1 February 9th, 2007 03:15 PM
Null reference exception when using DataAdapter Samantha Karen Webb C# 1 July 21st, 2006 09:12 AM
NULL reference exception Dwizz VB.NET 2002/2003 Basics 3 June 21st, 2005 07:15 AM
null exception error abhit_kumar JSP Basics 4 January 5th, 2005 05:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.