Android Question Error Printing text file on Bluetooth printer

Eduardo Funabashi

Member
Licensed User
Longtime User
Hi,

I have code to print on a bluetooth printer.
First time I execute routine to print a text file, it prints withou any error.
But when I execute the same print subroutine the second time, I got exception error.
Please see attached image.
Can someone help me what I'm doing wrong ?

Best Regards
 

Attachments

  • b4a.png
    b4a.png
    157.2 KB · Views: 254

MarkusR

Well-Known Member
Licensed User
Longtime User
put the outputstream into a variable before and set a breakpoint there use debug step.
can u copy the line 248 here? i can show what i mean.
 
Upvote 0

Eduardo Funabashi

Member
Licensed User
Longtime User
Thank you DonManfred and MarkusR

Here is the SUB where I print text file and below the exception

this line throws exception second time it is executed
' start printer
Main.Printer.Initialize2(Main.BTConnection.OutputStream,"windows-1252")




B4X:
Sub Print_File_BT()
    Dim tr As TextReader
    Dim CAMINHO As String
    'Dim Printer As TextWriter
    Dim Out As OutputStream
   
    If File.ExternalWritable Then
        CAMINHO = File.DirDefaultExternal
    Else
        CAMINHO = File.DirInternal
    End If
   
    If File.Exists(CAMINHO, "conta.txt") = False Then
        Return
    End If
   
    ' start printer
    Main.Printer.Initialize2(Main.BTConnection.OutputStream,"windows-1252")  German/French chars
   
    Dim tr As TextReader
    tr.Initialize(File.OpenInput(CAMINHO, "conta.txt"))
    Dim st As String
   
    ' lê o arquivo
    Do While st <> Null
        st = tr.ReadLine
        If st <> Null Then
            Main.Printer.WriteLine(st)
        End If
    Loop
   
    Main.Printer.Flush
    Main.Printer.Close
    Main.BTConnection.Disconnect
   
    Msgbox(" Conta Impressa...", "")

    ' fecha tela
    Activity.finish
   
End Sub



First time SUB prints file normally .. no errors
Second time I got this exception



Error occurred on line: 0 (Emite_Mostra_Conta)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.keywords.Common.CallSubDebug(Common.java:1036)
at iFox.PdvRest.filetransfer._zera_updateprogress(filetransfer.java:392)
at iFox.PdvRest.filetransfer._astream_newstream(filetransfer.java:303)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:336)
at anywheresoftware.b4a.debug.Debug.CallSubNew(Debug.java:282)
... 18 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
... 19 more
Caused by: 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:263)
at iFox.PdvRest.emite_mostra_conta._print_file_bt(emite_mostra_conta.java:1129)
at iFox.PdvRest.emite_mostra_conta._zera_updateprogress(emite_mostra_conta.java:1286)
... 21 more
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i meant this, set a breakpoint and see if "out" is not null
(and please use the icon insert... code)
B4X:
Dim Out as OutputStream
Out = Main.BTConnection.OutputStream
Main.Printer.Initialize2(Out,"windows-1252")
 
Upvote 0
Top