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)

Open in new window


Error is "An unhandled exception of type 'System.NullReferenceException' occurred"
LVL 6
r3nderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy PooleCommented:
If not IsNothing(mstrDataReceived) and Not String.IsNullOrEmpty(mstrDataReceived) and  mstrDataReceived.Length>11 Then  EMPPINENTERED = mstrDataReceived.Substring(8, 11)

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.
r3nderAuthor Commented:
thanks but same error
Éric MoreauSenior .Net ConsultantCommented:
you might be mixing the operators (Not + Or):
If Not String.IsNullOrEmpty(mstrDataReceived) andalso mstrDataReceived <> "0" Then
            EMPPINENTERED = mstrDataReceived.Substring(8, 11)

Open in new window

The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

it_saigeDeveloperCommented:
Actually Randy, I think you meant:
If Not IsNothing(mstrDataReceived) AndAlso Not String.IsNullOrEmpty(mstrDataReceived) AndAlso  mstrDataReceived.Length>11 Then  EMPPINENTERED = mstrDataReceived.Substring(8, 11)

Open in new window

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)

Open in new window

-saige-
it_saigeDeveloperCommented:
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

Open in new window

Produces the following output -Capture.JPG-saige-
r3nderAuthor Commented:
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

Open in new window

Randy PooleCommented:
It's a pain switching between c# and VB.NET all the time :P  Yes, it_saige has posted what I should of.
it_saigeDeveloperCommented:
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-
it_saigeDeveloperCommented:
@Randy - We all do this. ;)

-saige-
Randy PooleCommented:
If mstrDataReceived IsNot Nothing AndAlso Not String.IsNullOrEmpty(mstrDataReceived) AndAlso mstrDataReceived.Length > 11 Then
            EMPPINENTERED = mstrDataReceived.Substring(8, 11)
        Else
             'DONOTHING
        End If

Open in new window


You have them switched
it_saigeDeveloperCommented:
@Randy - Case in point.

Well spotted.  ;)

-saige-
r3nderAuthor Commented:
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

Open in new window

it_saigeDeveloperCommented:
Randy pointed out the newest error in your logic.

-saige-
it_saigeDeveloperCommented:
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

Open in new window

-saige-
Randy PooleCommented:
Public Function ProcessXEVT() As String
        Dim hasData As Boolean
        hasData = mstrDataReceived IsNot Nothing AndAlso Not String.IsNullOrEmpty(mstrDataReceived)
        If hasData 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
            Dim slength As Integer = mstrDataReceived.Length.ToString()
            MessageBox.Show(slength.ToString())
            If mstrDataReceived.Length > 11 Then
                EMPPINENTERED = mstrDataReceived.Substring(8, 11)
            End If
        End If
    End Function

Open in new window

Try this to simplify

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
r3nderAuthor Commented:
Thanks Randy
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.