r3nder
asked on
if a string is null in any way I do not want this value set
If Not String.IsNullOrEmpty(mstrDataReceived) Or mstrDataReceived <> "0" Or IsNothing(mstrDataReceived) Then
EMPPINENTERED = mstrDataReceived.Substring(8, 11)
Error is "An unhandled exception of type 'System.NullReferenceExcep
ASKER
thanks but same error
you might be mixing the operators (Not + Or):
If Not String.IsNullOrEmpty(mstrDataReceived) andalso mstrDataReceived <> "0" Then
EMPPINENTERED = mstrDataReceived.Substring(8, 11)
Actually Randy, I think you meant:
Although, as a personal preference, I would use:
If Not IsNothing(mstrDataReceived) AndAlso Not String.IsNullOrEmpty(mstrDataReceived) AndAlso mstrDataReceived.Length>11 Then EMPPINENTERED = mstrDataReceived.Substring(8, 11)
Using And will check *ALL* of the logical equations whereas, AndAlso will short-circuit.Although, as a personal preference, I would use:
If mstrDataReceived IsNot Nothing AndAlso Not String.IsNullOrEmpty(mstrDataReceived) AndAlso mstrDataReceived.Length > 11 Then EMPPINENTERED = mstrDataReceived.Substring(8, 11)
-saige-
Proof of concept:
Module Module1
Sub Main()
Dim object1 As Object = Nothing
PerformLogicTest(object1, False)
PerformLogicTest(object1, True)
Console.ReadLine()
End Sub
Sub PerformLogicTest(ByVal obj As Object, ByVal useAndAlso As Boolean)
If Not useAndAlso Then
Try
Console.WriteLine("Using And to validate the object")
If Not IsNothing(obj) And Not String.IsNullOrEmpty(obj) And obj.Lenth > 11 Then
Console.WriteLine("The test was successful")
Else
Console.WriteLine("The test failed")
End If
Catch ex As Exception
Console.WriteLine("The test caused an exception")
End Try
Else
Try
Console.WriteLine("Using AndAlso to validate the object")
If Not IsNothing(obj) AndAlso Not String.IsNullOrEmpty(obj) AndAlso obj.Lenth > 11 Then
Console.WriteLine("The test was successful")
Else
Console.WriteLine("The test failed")
End If
Catch ex As Exception
Console.WriteLine("The test caused an exception")
End Try
End If
End Sub
End Module
Produces the following output --saige-
ASKER
This is what I have and it gets the same error mstrDataReceived = Nothing
If mstrDataReceived IsNot Nothing AndAlso Not String.IsNullOrEmpty(mstrDataReceived) AndAlso mstrDataReceived.Length > 11 Then
'DONOTHING
Else
EMPPINENTERED = mstrDataReceived.Substring(8, 11)
End If
It's a pain switching between c# and VB.NET all the time :P Yes, it_saige has posted what I should of.
This means that mstrDataRecieved is nothing, a null value. If mstrDataRecieved is a null value and it should not be then the problem lies in how mstrDataRecieved is either initialized or set.
The logic test is to ensure that you do not attempt to read data from a null object.
-saige-
The logic test is to ensure that you do not attempt to read data from a null object.
-saige-
@Randy - We all do this. ;)
-saige-
-saige-
If mstrDataReceived IsNot Nothing AndAlso Not String.IsNullOrEmpty(mstrDataReceived) AndAlso mstrDataReceived.Length > 11 Then
EMPPINENTERED = mstrDataReceived.Substring(8, 11)
Else
'DONOTHING
End If
You have them switched
@Randy - Case in point.
Well spotted. ;)
-saige-
Well spotted. ;)
-saige-
ASKER
The Entire function
Public Function ProcessXEVT() As String
If Not String.IsNullOrEmpty(mstrDataReceived) Then
If mstrDataReceived.Length >= 4 Then
Select Case mstrDataReceived.Substring(1, 3)
Case "S01"
'Beginning of packet(s) for signature data
strDesc = "Sig Data"
SigHeader()
If _SigData.SigLength <> "00000" Then
'bolPacketReceived = False
Else
If Not config.Item("OutputDeviceDialog").IsNullOrEmpty AndAlso CBool(config.Item("OutputDeviceDialog")) Then
Console.WriteLine(Now.ToString("yyyyMMddHHmmss: ") & "Canceling")
End If
RaiseEvent SignatureMenuButtonPressed(IPosDevice.DataType.Cancel)
End If
mstrSigData = ""
Case "S02"
'Beginning of packet(s) for signature data
strDesc = "Sig Packet"
mstrSigData &= mstrDataReceived.Substring(9, mintLenBinaryData)
'Console.WriteLine(mstrDataReceived.Substring(4, 1))
If mstrDataReceived.Substring(4, 1) = "N" Then
UpdateSignatureFile()
End If
Case "73."
strDesc = "PIN Data"
ParsePin(mstrDataReceived)
Case "81."
strDesc = "Card Data "
ParseCardSwipe()
Case Else
If mstrDataReceived.Length >= 5 Then
Select Case mstrDataReceived.Substring(1, 4)
Case "XEVT"
strDesc = "Event"
ParseXEVT(mstrDataReceived)
Debug.Write(EMPPINENTERED = mstrDataReceived.Substring(8, 11))
Dim mstrl As Integer = Convert.ToString(mstrDataReceived).Length
If mstrDataReceived IsNot Nothing AndAlso Not String.IsNullOrEmpty(mstrDataReceived) AndAlso mstrDataReceived.Length > 11 Then
Dim cmx As cMx900Series = New cMx900Series()
MessageBox.Show(cmx.EMPPINENTERED.ToString())
End If
End Select
End If
End Select
End If
End If
If mstrDataReceived IsNot Nothing AndAlso Not String.IsNullOrEmpty(mstrDataReceived) AndAlso mstrDataReceived.Length > 11 Then
Dim slength As Integer = mstrDataReceived.Length.ToString()
MessageBox.Show(slength.ToString())
End If
If mstrDataReceived IsNot Nothing Or Not String.IsNullOrEmpty(mstrDataReceived) Or mstrDataReceived.Length > 11 Then
'DONOTHING
Else
EMPPINENTERED = mstrDataReceived.Substring(8, 11)
End If
End Function
Randy pointed out the newest error in your logic.
-saige-
-saige-
Actually why not just set the value where you have already performed the test. Remember, DRY -
Public Function ProcessXEVT() As String
If Not String.IsNullOrEmpty(mstrDataReceived) Then
If mstrDataReceived.Length >= 4 Then
Select Case mstrDataReceived.Substring(1, 3)
Case "S01"
'Beginning of packet(s) for signature data
strDesc = "Sig Data"
SigHeader()
If _SigData.SigLength <> "00000" Then
'bolPacketReceived = False
Else
If Not config.Item("OutputDeviceDialog").IsNullOrEmpty AndAlso CBool(config.Item("OutputDeviceDialog")) Then
Console.WriteLine(Now.ToString("yyyyMMddHHmmss: ") & "Canceling")
End If
RaiseEvent SignatureMenuButtonPressed(IPosDevice.DataType.Cancel)
End If
mstrSigData = ""
Case "S02"
'Beginning of packet(s) for signature data
strDesc = "Sig Packet"
mstrSigData &= mstrDataReceived.Substring(9, mintLenBinaryData)
'Console.WriteLine(mstrDataReceived.Substring(4, 1))
If mstrDataReceived.Substring(4, 1) = "N" Then
UpdateSignatureFile()
End If
Case "73."
strDesc = "PIN Data"
ParsePin(mstrDataReceived)
Case "81."
strDesc = "Card Data "
ParseCardSwipe()
Case Else
If mstrDataReceived.Length >= 5 Then
Select Case mstrDataReceived.Substring(1, 4)
Case "XEVT"
strDesc = "Event"
ParseXEVT(mstrDataReceived)
Debug.Write(EMPPINENTERED = mstrDataReceived.Substring(8, 11))
Dim mstrl As Integer = Convert.ToString(mstrDataReceived).Length
If mstrDataReceived IsNot Nothing AndAlso Not String.IsNullOrEmpty(mstrDataReceived) AndAlso mstrDataReceived.Length > 11 Then
Dim cmx As cMx900Series = New cMx900Series()
MessageBox.Show(cmx.EMPPINENTERED.ToString())
End If
End Select
End If
End Select
End If
End If
If mstrDataReceived IsNot Nothing AndAlso Not String.IsNullOrEmpty(mstrDataReceived) AndAlso mstrDataReceived.Length > 11 Then
Dim slength As Integer = mstrDataReceived.Length.ToString()
EMPPINENTERED = mstrDataReceived.Substring(8, 11)
MessageBox.Show(slength.ToString())
End If
End Function
-saige-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Randy
Open in new window
You need to use and's and also put isnothing in the front. I have also added a condition to make sure the length of the string is long enough for your substring function.