Android Question Socket is null inside Connected event (Successful = True) — what can cause this?

knutf

Active Member
Licensed User
Longtime User
Hello

I got java.lang.NullPointerException when extracting inputStream/outputStream from Socket in Socket connected event.

It was this line that threw the error:
B4X:
pStream.InitializePrefix(pSocket.InputStream,True,pSocket.OutputStream,"pStream")

Here is the complete connected event:
Private Sub pSocket_Connected (Successful As Boolean)
    Log($"pSocket_Connected Successful: ${Successful}"$)
    pConnecting = False
    If Successful Then
        If pStayConnected Then
            pConnected = True
            CallSubDelayed(mTarget,mEventName & "_Connected")
            pStream.InitializePrefix(pSocket.InputStream,True,pSocket.OutputStream,"pStream")
            Dim ser As B4XSerializator
            Dim Bytes() As Byte = ser.ConvertObjectToBytes("Authenticate connection")
            If pConnected Then pStream.Write(Cip.Encrypt(Bytes,pCipherPassword))
            
            Log($"pRequestsEncrypted.size:${pRequestsEncrypted.Size}"$)
            For Each id In pRequestsEncrypted.Keys
                pRequestStartTimes.Put(id,DateTime.Now)
                Dim data() As Byte = pRequestsEncrypted.Get(id)
                pStream.Write(data)
                pLastTimeDataIsSent = DateTime.Now
                pLastDataSentWasAPing = (pRequestTargets.Get(id)=Me)
            Next
            
            broadcast.dbConnected(pStream)
        Else
            pSocket.Close
        End If
    End If
End Sub

The stacktrace is
B4X:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.InputStream java.net.Socket.getInputStream()' on a null object reference
at anywheresoftware.b4a.objects.SocketWrapper.getInputStream(SocketWrapper.java:239)
at knutf.tractorOffice.dbconnection._psocket_connected(dbconnection.java:369)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at anywheresoftware.b4a.BA$2.run(BA.java:395)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6623)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)


At this point I’m trying to understand how pSocket can be null inside the Connected event, even though Successful = True and the event should only fire after the socket is initialized.

Has anyone seen this before?

A few things I’m unsure about:

  • Can the Java socket be disposed or closed before InitializePrefix runs, even when Successful is true?
  • Is there any scenario where the wrapper triggers Connected but the underlying socket is already null?
  • Should I explicitly check pSocket.IsInitialized or pSocket.Connected inside the event, or would that just mask a deeper issue?
If anyone has insight into what could cause getInputStream() to return null here, I’d really appreciate suggestions on how to debug or prevent it.
 

knutf

Active Member
Licensed User
Longtime User
Comment out line seven (CallSubDelayed), does it make a difference?
It's hard to say, because the app works almost all the time without this error occurring, even when line seven is not commented out. I don't think CallsubDelayed interferes with the socket at all while the code in the pSocket_Connected sub is running. All it does is queue the sub call until the current code has finished running. What this sub does when it is finally called is indeed to read/write the socket. But as I wrote, this does not happen until after the code that causes the error has finished executing. If the code is commented out, the app does not work and it therefore makes no sense to run the app for many hours to see if the rare error occurs.
 
Upvote 0
Top