Android Question Wait For Error. WeakReference NullPointerException

LuigiTasca

Member
Licensed User
Longtime User
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:

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
 

LuigiTasca

Member
Licensed User
Longtime User
The CallSub are for the "Wait For" of this sub.

B4X:
PrintReceipt()
Wait For Print_Completed (Success As Boolean)
If Success Then
'[...]
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
If MySocket.IsInitialized Then
MyStream.Initialize(MySocket.InputStream,MySocket.OutputStream,"MyStream")
Else
MySocket.Close
CallSub2(Me,"Print_Completed",False)
DoEvents
Return
End
If
Shouldn't be "If MySocket.IsInitialized = False" ? (or, as I prefer :D: If Not(MySocket.IsInitialized)

Also, your code contains many "Return"; is it all the code of your routine?

Finally, try using CallSubDelayed instead of Callsub.
 
Upvote 0

LuigiTasca

Member
Licensed User
Longtime User
Shouldn't be "If MySocket.IsInitialized = False" ? (or, as I prefer :D: If Not(MySocket.IsInitialized)

Also, your code contains many "Return"; is it all the code of your routine?

Finally, try using CallSubDelayed instead of Callsub.

That's right, i check the socket to initialize the stream ;)

How can CallSubDelayed solve an error on a WaitFor in another part of the Sub? Why have I an WeakReference null pointer exception?
Anyway I tried to replace the CallSub with CallSubDelayed but the error persists.

work$ResumableSub_PrintReceiptresume (java line: 39630)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
at anywheresoftware.b4a.keywords.Common.WaitFor(Common.java:1713)
at com.MyCompany.myapp.work$ResumableSub_PrintReceipt.resume(work.java:39630)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:245)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:185)
at anywheresoftware.b4a.BA$2.run(BA.java:365)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5258)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
How can CallSubDelayed solve an error on a WaitFor in another part of the Sub?
In some cases you have to use CallSubDelayed when you call resumable subs, because the calling routine should be completed before the call.


Maybe after a coffee and a cigarette and reading again your code I will understand something, if the code posted is sufficient.

Fortunately, there is Erel online :D
 
Upvote 0
Top