Java Question Unable to read local file.

Holland Kendall

New Member
Licensed User
I am trying to develop an application which captures and then modifies a file on a Windows PC. I have been successful in transferring the text file over to the Smart Phone but when I try to open it I get this: Error occurred on line: 108 (Main) java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 Line 108 is this: "Log(DataRead.Get(0))"
Below is the code involved.
Sub BtnTest_click
Dim User As String
Dim Pass As String
Dim Filename As String
Dim FileLocation As String
Dim LocalDirectory As String
Dim DataTemp As String
Dim DataRead As List
Dim DataReadLast As List
Dim StringItem As StringUtils
Dim OneLine As String
Dim Lstlabel As Label
Dim I,J As Int
Filename = "Removals.txt"
LocalDirectory = "NetworkJournal"
User = "Optical_User"
FileLocation = "smb://"
' SMB1.DownloadFile(FileLocation,Filename,File.DirRootExternal & "/" & LocalDirectory,Filename)
If File.Exists(File.DirRootExternal,Filename) Then
' File.Exists(File.DirRootExternal & "/" & LocalDirectory,Filename) Then
Log("File " & Filename & " exists.")
Log("File " & Filename & " does not exist.")
End If
''DataRead = File.ReadList(File.DirRootExternal & "/" & LocalDirectory,Filename)
DataRead = File.ReadList(File.DirRootExternal,Filename)
' DataRead = File.ReadList(File.DirDefaultExternal,Filename)
' DataRead = File.ReadList(File.DirDefaultExternal,"Journalfile.txt")
Log("The name of the file is:" & Filename)
Log("File.DirRootExternal =" & File.DirRootExternal)

It died here but there is more code after this.

Here is what Logger shows:
Logger connected to: samsung SM-S327VL
--------- beginning of main
--------- beginning of system
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
File Removals.txt exists.
The name of the file is:Removals.txt
File.DirRootExternal =/storage/emulated/0
Error occurred on line: 108 (Main)
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

Can you tell me why the size of the file is marked as zero even though the file does exist?


Well-Known Member
Licensed User
Longtime User
never make use of a list without testing if it's initialized. i realize you initialize your dataread list, BUT you assign it subsequently as the return value of File.ReadList(). if that fails, readdata is may no longer initialized (that depends on how the method was written). your log(datarea.get(0)) call makes ill-advised assumptions.

dataread.size is not directly related to the size of the file; it refers to the size of dataread (a list, not a file).

when the downloadfile method completes, it raises an event. you need to consume it. i don't see that in your code.

anyway, you need to handle that event before moving on. a "wait for" might be appropriate, but, in any case, you need to wait until the download signals
its completion before doing anything else. your code infers that the download was immediate and successful, but i don't see (from the snippet you posted) that anybody told you everything was ok. you acted like it was, which - my guess would be - it wasn't. i/o doesn't work that way.

that the file may or may not exist in dirrootexternal is not necessarily relevant to this discussion (it may have already been there or the download may have started but failed). you should see what is triggered by the oncompletion event. also readlist reads a text file as a list. is that what you wanted (just asking)?
if the file wasn't saved in list format, that might return unexpected results as well. did you try reading it as a string? before doing that, deal with the completion event.
Last edited: