Error msg - empty array

sheriffporter

Member
Licensed User
Longtime User
Need help getting rid of this error. I am using code below to list a directory on my droid, then read each file and delete after reading them. When the directory is empty I get an error "java.io.IOException: Invalid argument" I know I am causing this by trying to read an empty array, just not sure how to get rid of it. I want the code to continue of the dir is empty. Any ideas?

B4X:
 Dim Grabbed As String
    LocalFiles = File.ListFiles (localDir) 'LocalDir is my droid inbox directory
    For i = 0 To LocalFiles.Size-1 
       Activity.Title = LocalFiles.Size & " Returns Waiting"
    Grabbed = LocalFiles.Get (i)    
   
     Next
       ListView1.Visible = False 'hide the listview which is my menu page
       lbl1.Visible = True      'show the text label for text file
        lbl1.Text = File.ReadString(localDir, Grabbed)  ' read the first text file
       File.Delete(localDir, Grabbed)               'delete the file after it is read
   End If
 

admac231

Active Member
Licensed User
Longtime User
If I understand what you;re asking then this should work:
B4X:
Dim Grabbed As String
LocalFiles = File.ListFiles (localDir) 'LocalDir is my droid inbox directory

If LocalFiles.Size > 0 Then
   For i = 0 To LocalFiles.Size-1 
      Activity.Title = LocalFiles.Size & " Returns Waiting"
      Grabbed = LocalFiles.Get (i)    
   Next
End If

ListView1.Visible = False 'hide the listview which is my menu page
lbl1.Visible = True        'show the text label for text file

If File.Exists(localDir,Grabbed) Then
   lbl1.Text = File.ReadString(localDir, Grabbed)  ' read the first text file
   File.Delete(localDir, Grabbed)                    'delete the file after it is read
End If
End If
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…