B4J Question TextReader ReadLine error

madru

Active Member
Licensed User
Hi,

I need some help please, can somebody explain why this doesn't work ?
If I remove the IF / Endif it does

THX


B4X:
Dim tr AsTextReader
tr.Initialize(File.OpenInput(getFilePath(f),getFileName(f)))
Dim st As String = tr.ReadLine
DoWhile st <> Null
  Log(st)
  st = tr.ReadLine.Trim
    If    st.StartsWith(":") = True And st.Length > 10 Then
'          AddGroupSepStr(st) ' parse Group
    Else
'          FillChannelData(st) ' parse Channel
    End If
Program started.
Error occurred on line: 143 ====> st = tr.ReadLine.Trim
java.lang.NullPointerException
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:605)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:226)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:60)
at b4j.example.main._loaddatdialog(main.java:134)
at b4j.example.main._button1_action(main.java:126)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:612)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA$2.run(BA.java:165)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
 

giga

Well-Known Member
Licensed User
Did you verify the file path and file name? Are you sure the OpenInput folder/file is right?
 

Beja

Expert
Licensed User
I see you write AsTextReader
Make a space between "As" and "TextReader"
Hope this will fix it.
 

Daestrum

Well-Known Member
Licensed User
Put a check for st = null before you attempt to check the length.
If st is null the length check will fail with an exception as length cannot be determined on an uninitialized variable.
null is not the same as a string with a value of ""

eg,
B4X:
st = ""
log(st.length)
'will print 0 as it's a valid operation

st = null
log(st.length)
'will throw a nullpointerexception as it's an invalid operation
Also you will not be able to perform an action the first line in the file as you read it before the loop. then immediately read it inside the loop. Move the read inside the do - loop to just before the loop line.
(You could change the do while st <> null to do until st = null ( I think b4j supports until))
 
Last edited:

madru

Active Member
Licensed User
Hi all,

tried all suggestions, still not working :(

B4X:
Dim tr AsTextReader
tr.Initialize(File.OpenInput(getFilePath(f),getFileName(f)))
Dim st As String = tr.ReadLine
'Do While st <> Null
    DoUntil st = Null
'   Log(st)
  st = tr.ReadLine.Trim
    If st <> Null Then
            If    st.StartsWith(":") = True And st.Length > 10 Then
    '          AddGroupSepStr(st)
        Else
    '      FillChannelData(st)
          End If
    EndIf
Loop
[/code

Program started.
Error occurred on line: 143
java.lang.NullPointerException
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:605)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:226)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:60)
    at b4j.example.main._loaddatdialog(main.java:134)
    at b4j.example.main._button1_action(main.java:126)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:612)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
    at anywheresoftware.b4a.BA$2.run(BA.java:165)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
 

Daestrum

Well-Known Member
Licensed User
Maybe this line is failing on a null value
B4X:
st = tr.ReadLine.Trim
Try it without the Trim

There is something odd happening
The following generated code is incorrect
B4X:
dim s as String
s = Null
' s should have a value of Null but in fact has the string "null" with a length of 4
 
Last edited:

madru

Active Member
Licensed User
should have tried that before, that is causing the error

Ok, we have ways around this error, but the issue should be investigated (fixed) by Erel

THX for the help :)
 

Erel

Administrator
Staff member
Licensed User
Note that in most cases there is no good reason to use TextReader.

If you want to process the lines then you can use:
B4X:
For Each line As String In File.ReadList(...)

Next
With that said, can you please upload a small example that demonstrates the issue?
 

madru

Active Member
Licensed User
Ere,

the Textreader behaves as it should, the problem is the
B4X:
trunk
that one does not initialize correctly

B4X:
Dim s AsString
s = Null
Log(s.Length)
' s should be 0 length but but is 4
 

Erel

Administrator
Staff member
Licensed User
This code is wrong:
B4X:
Dim tr AsTextReader
tr.Initialize(File.OpenInput(getFilePath(f),getFileName(f)))
Dim st As String = tr.ReadLine
DoWhile st <> Null
  Log(st)
  st = tr.ReadLine.Trim
    If    st.StartsWith(":") = True And st.Length > 10 Then
'          AddGroupSepStr(st) ' parse Group
    Else
'          FillChannelData(st) ' parse Channel
    End If
You are skipping one row each time as you call ReadLine twice every iteration.
 

Erel

Administrator
Staff member
Licensed User
As Null is treated like any other object it is converted to a string with the value of "null". This has nothing to do with the issue you encountered.
 
Top