Showing posts with label null. Show all posts
Showing posts with label null. Show all posts

Thursday, March 29, 2012

Please clarify how null nodeset result processes?

Hi, I continue to have trouble with this. Using the following xml
sample:

<Station name="Station1" line_stop="yes"/
OR when NO line_stop attribute exists.

In asp.net page, I use xpath to determine IF the line_stop attribute
exists (or am TRYING to!)

Dim xDoc as XmlDocument
Dim xNode As XmlElement =
xDoc.SelectSingleNode("//Station[@dotnet.itags.org.name='Station1'][@dotnet.itags.org.line_stop='yes']")

If Not xNode.Value Is Nothing Then
If xNode.Value = "yes" Then
do this...
End If
End If

If there is no line_stop attribute, I get an error for "Object reference
not set to an instance of an object" at the If Not xNode.Value
line...obviously returning no node.

Please tell me where I'm going wrong.

Thanks,
Kathy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!Kathy, you might get better responses in the .framework group, since this
doesn't seem to be specific to ASP.NET.

--
John Saunders
Internet Engineer
john.saunders@.surfcontrol.com

"Kathy Burke" <kathyburke40@.attbi.com> wrote in message
news:%23xJL5a0aDHA.2412@.TK2MSFTNGP10.phx.gbl...
> Hi, I continue to have trouble with this. Using the following xml
> sample:
> <Station name="Station1" line_stop="yes"/>
> OR when NO line_stop attribute exists.
> In asp.net page, I use xpath to determine IF the line_stop attribute
> exists (or am TRYING to!)
> Dim xDoc as XmlDocument
> Dim xNode As XmlElement =
> xDoc.SelectSingleNode("//Station[@.name='Station1'][@.line_stop='yes']")
> If Not xNode.Value Is Nothing Then
> If xNode.Value = "yes" Then
> do this...
> End If
> End If
> If there is no line_stop attribute, I get an error for "Object reference
> not set to an instance of an object" at the If Not xNode.Value
> line...obviously returning no node.
> Please tell me where I'm going wrong.
> Thanks,
> Kathy
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!
check if the xNode is nothing first

"Kathy Burke" <kathyburke40@.attbi.com> wrote in message
news:%23xJL5a0aDHA.2412@.TK2MSFTNGP10.phx.gbl...
> Hi, I continue to have trouble with this. Using the following xml
> sample:
> <Station name="Station1" line_stop="yes"/>
> OR when NO line_stop attribute exists.
> In asp.net page, I use xpath to determine IF the line_stop attribute
> exists (or am TRYING to!)
> Dim xDoc as XmlDocument
> Dim xNode As XmlElement =
> xDoc.SelectSingleNode("//Station[@.name='Station1'][@.line_stop='yes']")
> If Not xNode.Value Is Nothing Then
> If xNode.Value = "yes" Then
> do this...
> End If
> End If
> If there is no line_stop attribute, I get an error for "Object reference
> not set to an instance of an object" at the If Not xNode.Value
> line...obviously returning no node.
> Please tell me where I'm going wrong.
> Thanks,
> Kathy
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!
Doesn't "If Not xNode.Value Is Nothing Then" check that xNode is
nothing?

Thanks.

Kathy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
"Kathy Burke" <kathyburke40@.attbi.com> wrote in message
news:ei17Bx0aDHA.2024@.TK2MSFTNGP12.phx.gbl...
> Doesn't "If Not xNode.Value Is Nothing Then" check that xNode is
> nothing?

No. That checks to see if xNode.Value is Nothing. If xNode is Nothing, the
above "If" statement will throw a NullReferenceException.

To see if xNode is Nothing try:

If Not xNode is Nothing Then

--
John Saunders
Internet Engineer
john.saunders@.surfcontrol.com
Hi again,

I've tried adding:

If Not n16 Is Nothing Then
If n16.Value = "yes" Then
Dim strNext As String = "yes"
End If
End If

but still get a NullReferenceException on the first line of the above
snippet. In this particular case, I SHOULD get "nothing" as a result
since the attribute 'line_stop' doesn't exist for the element
selected...but the code should then continue, yes?

Any suggestions for how I can proceed?

Kathy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Wednesday, March 21, 2012

Please Help me

Below code is giving me error like ''function doesnt return in all code paths A null reference exception could occur"

As i m new to this tech i am not getting that

PublicSharedFunction getLookupCampusList(ByVal eidAsString, _

ByVal isAdminAsBoolean)As SqlDataReader

'Dim getLookup As SqlDataReader = Nothing

Dim paraValueAsObject() = {eid}

Dim myRoleAsString = appRole(eid)

If isAdminThen

If myRole ="A"Then

Try

Return SQLHelper.ExecuteReader("sp_locationDes", paraValue)

Catch exAs Exception

Throw ex

EndTry

ElseIf myRole ="E"Then

Try

Return SQLHelper.ExecuteReader("sp_GetUsingCommonDB", paraValue)

Catch exAs Exception

Throw ex

EndTry

ElseIf myRole ="B"Then

Try

Return SQLHelper.ExecuteReader("sp_getByEmpLoc", paraValue)

Catch exAs Exception

Throw ex

EndTry

EndIf

Else

Try

Return SQLHelper.ExecuteReader("sp_GetEmpID", paraValue)

Catch exAs Exception

Throw ex

EndTry

EndIf

End Function

If you indent your code to highlight the control-flow, logic errors become easier to find.

This error means that the compiler found a logic path that did not make use of a return statement.

If a function says it returns a given object type, unless it exits via an unhandled exception, it must return that object type.

If isAdminThen
If myRole ="A"Then
Return SQLHelper.ExecuteReader("sp_locationDes", paraValue)
ElseIf myRole ="E"Then
Return SQLHelper.ExecuteReader("sp_GetUsingCommonDB", paraValue)
ElseIf myRole ="B"Then
Return SQLHelper.ExecuteReader("sp_getByEmpLoc", paraValue)
EndIf
Else
Return SQLHelper.ExecuteReader("sp_GetEmpID", paraValue)
EndIf

I removed your try-catch commands for two reasons. The first reason was to make the above code snippet easier to read.

What will be returned if isAdmin is true and myRole = "NotInThisList"?

And if you say to yourself, but, "NotInThisList" isn't a valid value for myRole, remember that the compiler does not know that. :)

So, you need to add a logic path to handle some myRole value other than A, E, or B, or you need to change the ElseIf on "B" to be an Else.

The second reason I removed your try catch commands were that they were redundant. They did nothing but catch an exception and re-throw it.

If you never bothered to do that you would get the same result - an exception that needs to be handled by the calling routine. So, either add useful code that fixes the exception or logs it into your catch block, or don't bother to put them in.


Thanks Alot Its working


Glad it helped! Now you need to mark the answer so others don't wander in here to help you when you no longer need it. :)