Hi everyone,
I developed in my app the comunication with a fiscal printer. I do it opening a socket and writing the comands in the stream.
I load the comands in a strings array and then I write them to the fiscal printer in a loop.
I have to wait the response from the fiscal printer (with the status ) with a Wait For. It works but sometimes (1/10 times ) I get an error on the Wait For:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
Here the Sub:
How can I fix this?
Thank you
I developed in my app the comunication with a fiscal printer. I do it opening a socket and writing the comands in the stream.
I load the comands in a strings array and then I write them to the fiscal printer in a loop.
I have to wait the response from the fiscal printer (with the status ) with a Wait For. It works but sometimes (1/10 times ) I get an error on the Wait For:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
Here the Sub:
B4X:
'I load "Comm_Array" with the commands to send to the fiscal printer.
'"Comm_Array" is a global object
MySocket.Initialize("MySocket")
MySocket.Connect(IpMisuratore,PortaMisuratore,1000)
Wait For MySocket_Connected(Successful As Boolean)
If Successful = False Then
CallSub2(Me,"Print_Completed",False)
DoEvents
Return
End If
'inizializzo lo stream
If MySocket.IsInitialized Then
MyStream.Initialize(MySocket.InputStream,MySocket.OutputStream,"MyStream")
Else
MySocket.Close
CallSub2(Me,"Print_Completed",False)
DoEvents
Return
End If
'Check the fiscal printer status
FiscalPrinter_CheckStatus'(MySocket,MyStream)
Wait For FiscalPrinter_StatusChecked(OK As Boolean)
If OK = False Then
MyStream.Close
MySocket.Close
CallSub2(Me,"Print_Completed",False)
DoEvents
Return
End If
MyStream.Write(GET_FP_Bytes("<COMMANDS>"))
Wait For MyStream_newdata(BufferChiusura() As Byte)
Dim Text As String = BytesToString(BufferChiusura, 0, BufferChiusura.Length, "UTF8")
If Not(IsNumber(Text)) Then
MyStream.Close
MySocket.Close
CallSub2(Me,"Print_Completed",False)
DoEvents
Return
End If
ProgressivoChiusura = Text +1
MyStream.Write(GET_FP_Bytes("<COMMANDS>"))
Wait For MyStream_newdata(BufferProg() As Byte)
Dim Text As String = BytesToString(BufferProg, 0, BufferProg.Length, "UTF8")
'13/12/2018
If Not(IsNumber(Text)) Then
MyStream.Close
MySocket.Close
CallSub2(Me,"Print_Completed",False)
DoEvents
Return
End If
ProgressivoScontrino = Text +1
'Comm_Array is the array with all the commands
Dim Text As String
For e = 0 To Comm_Array.Length-1
Log(Comm_Array(e))
MyStream.Write(GET_FP_Bytes(Comm_Array(e)))
'<--- At this wait for I get the exception
'java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
Wait For MyStream_newdata(BufferRiga() As Byte)
Text = BytesToString(BufferRiga, 0, BufferRiga.Length, "UTF8")
If CheckStatus(Text) = False Then
MyStream.Close
MySocket.Close
CallSub2(Me,"Print_Completed",False)
DoEvents
Return
End If
Next
MyStream.Close
MySocket.Close
CallSub2(Me,"Print_Completed",True)
How can I fix this?
Thank you