Android Question print error

ronell

Well-Known Member
Licensed User
Longtime User
what caused this error?

lastexception
(NullPointerException) java.lang.NullPointerException: Attempt to invoke virtual method 'java.io_OutputStream android.bluetooth.BluetoothSocket.getOutputStream()' on a null object reference

full error logs when i remove try catch
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io_OutputStream android.bluetooth.BluetoothSocket.getOutputStream()' on a null object reference
at anywheresoftware.b4a.objects.Serial.getOutputStream(Serial.java:261)
at b4a.G8.STLSK.main._pe_smssentstatus(main.java:2675)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.phone.PhoneEvents$ActionHandler.send(PhoneEvents.java:326)
at anywheresoftware.b4a.phone.PhoneEvents$10.handle(PhoneEvents.java:188)
at anywheresoftware.b4a.phone.PhoneEvents$16.onReceive(PhoneEvents.java:268)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:905)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at anywheresoftware.b4a.Msgbox.waitForMessage(Msgbox.java:198)
at anywheresoftware.b4a.Msgbox.msgbox(Msgbox.java:137)
at anywheresoftware.b4a.keywords.Common.Msgbox2(Common.java:440)
at b4a.G8.STLSK.main._pe_smssentstatus(main.java:2671)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.phone.PhoneEvents$ActionHandler.send(PhoneEvents.java:326)
at anywheresoftware.b4a.phone.PhoneEvents$10.handle(PhoneEvents.java:188)
at anywheresoftware.b4a.phone.PhoneEvents$16.onReceive(PhoneEvents.java:268)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:905)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5643)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io_OutputStream

code

B4X:
Try
 
            printer.Initialize2(btserial2.OutputStream,"CP1256")
            printer.WriteLine(Chr(28) & "p" & Chr(1) & "1")
          
            printer.WriteLine(getreceiptdate)
            printer.WriteLine(getreceipttime)
            printer.WriteLine("")
            printer.WriteLine("")

 
            printer.Flush


            ToastMessageShow("Printed",False)
            printer.Close
            btserial2.Disconnect

      
    Catch
     
                Log(LastException)

                    btserial2.Disconnect
         
                End Try


Sub getreceipttime As String
    Dim hour As String
    Dim Minutes As String
    Dim Seconds As String
    Dim Meridian As String
    Dim TheTime As String
      
    hour = DateTime.GetHour(DateTime.Now)
    Minutes = DateTime.GetMinute(DateTime.Now)
    Seconds = DateTime.GetSecond(DateTime.Now)

    If hour < 12 Then Meridian = " AM"
    If hour = 12 Then Meridian = " PM"
    If hour = 0 Then

        hour = 12
        Meridian = " AM"

    End If

    If hour > 12 Then

        hour = hour - 12
        Meridian = " PM"

    End If

    TheTime = NumberFormat(hour, 2, 0) & ":" & NumberFormat(Minutes, 2, 0) & ":" & NumberFormat(Seconds, 2, 0) & Meridian
  
    Return TheTime
End Sub

Sub getreceiptdate As String
    Dim month As String
    Dim day As String
    Dim year As String
    Dim getmonth As String
    Dim date As String
  
  
    month = DateTime.GetMonth(DateTime.Now)
    day = DateTime.GetDayOfMonth(DateTime.Now)
    year = DateTime.GetYear(DateTime.Now)
  
    Select month
      
        Case "1"
            getmonth = "January"
        Case "2"
            getmonth = "February"
        Case "3"
            getmonth = "March"
        Case "4"
            getmonth = "April"
        Case "5"
            getmonth = "May"
        Case "6"
            getmonth = "June"
        Case "7"
            getmonth = "July"
        Case "8"
            getmonth = "August"
        Case "9"
            getmonth = "September"
        Case "10"
            getmonth = "October"
        Case "11"
            getmonth = "November"
        Case "12"
            getmonth = "December"
          
    End Select
  
    date = getmonth&" - "&day& " - "&year
  
    Return date
End Sub
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Do you Use TextWriter o AsyncStreams to send date to Bluetooth?
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
printer variable is TextWriter?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Do not use Textreader/Textwriter with Network-Streams!
Use AsyncStreams instead. Make sure to handle the Events.
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
can you guys give me some code on how can i convert that to AsyncStreams ? just the part of the printing process

thanks in advance
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Follow the tutorial. Search the forum for examples. this is handled a few times.
I don´t have the time (and mood) to do your job.

Bluetooth is a Async process. Connect to the device. Wait for a successfully connection. Only Print if you are connected. Wait for an possible Error event. Close the connection.
 
Upvote 0
Top