Android Question Inexplicable problem using "Contain" function.

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to All
I met now a situation that it is rather weird. I am listing some files, with the aim to delete them. After listing all of them, I must delete only those with extension "png". Btw none of them fulfills the condition, and, moreover, I don't delete them (the delete instruction is commented out). The weird is that, if I reproduce the problem in a simple test program, nothing bad happens, as should be. Therefore I can only post the image of what happens. The code is easily understandable, as long as the log displayed. As I said, same strings hard coded in a test program, give no problem.. I cannot say anything more..
 

Attachments

  • Immagine.png
    Immagine.png
    79.7 KB · Views: 61

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Tr
Then it is difficult.
BTW, sz.Trim does not assign back to the variable
Yes. Anyway trim is not important. I just added it thinking that some "strange" char has entered the list. But, to add more mistery, I have put the instructions inside a sub. The bug moves, as shown in this image. Not first time I met a strange thing. I have no hope to resolve it. Maybe some trick. But the worry is: may I have done something wrong elsewhere and consequences appear here? Sometimes in C/C++ you can do something that may have "unpredictable" consequences..
 

Attachments

  • Immagine1.png
    Immagine1.png
    118 KB · Views: 49
Upvote 0

Mahares

Expert
Licensed User
Longtime User
have "unpredictable" consequences
It seems like you are making the same mistake as in this thread:
See post #3 of that thread for answer
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I suspect the actual error occurs at line 3436.
The list cannot be cleared.
Provided that I commonly clear the lists with no problem, I just tried, and yes, the program doesn't crash. So, may we say that it is resolved? Maybe.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
It seems like you are making the same mistake as in this thread:
See post #3 of that thread for answer
I don't delete anything. Look at the code. I don't agree that I am doing any mistake. A mistake is, somewhere, and probably my mistake, but not in this code.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
The error shows that you are dealing with the Non dynamic list.
You cannot use Clear or Remove.
Why my list is not dynamic? Just declared locally.. Maybe you are right, but it seems strange to me. Anyway, your hint was successful and if this is the mistake, ok. Btw observe that, in first formulation, the list was not inside a sub. Anyway thanks.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Attach the test program. here I do same operations, with no problem.. with a very pleasant clear of the list ... Sorry. I just want to joke. I highly appreciate your contribute ...
 

Attachments

  • TestContain.zip
    10 KB · Views: 38
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Finally: in first post, the error was not in a sub and the crash was on the Contain call. Btw I don't pass any list to the sub. The List is local to the sub itself.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
File.ListFiles returns the list of files and pass to your locally defined list.
Dim ll As List = array is not creating a new object but make a reference to the original object.
If the original object cannot be remove, so as your new define list.
You need to use a loop to add the item one by one to your new list.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Ok. I agree with you: the returned list is read only, and I understand the fact that I cannot clear it. There is not reason to "incomodate" dynamic Lists etc. It is read only.. Ok. The misunderstanding arose from the crash happening on the call to Contain on the last element, without having yet called the Clear. Look at my first post. Thanks a lot.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Attach the test program. here I do same operations, with no problem.. with a very pleasant clear of the list ... Sorry. I just want to joke. I highly appreciate your contribute ...
In your sample project, you are creating a dynamic list.

If you are passing an array which created a non dynamic list, then you will get an error.

B4X:
Private Sub Button2_Click
    Dim ll As List = Array As String("ETRS89_308.230725.MP.GF-GEOTABLET_HATCH_PISCINAS.dxf", _
    "Comporta.def", _
    "General.jpg", _
    "JNCQ_SALES TENT NOTES To VDLA 24052023 GEOIDE.dxf", _
    "masterplan_DT73.dxf")
  
    For Each sz As String In ll
        Log(sz)
        If sz.Contains(".png") Then
                      
            'File.Delete(WorkDir,sz)
        End If
    Next
    ll.clear
End Sub

The error has nothing to do with the Contains method. You can verify by commenting out the line.
B4X:
'If sz.Contains(".png") Then
              
    'File.Delete(WorkDir,sz)
'End If
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
It is clear. Still I ask: why the crash on Contain in my first post? This misleaded me. Only this. If crash were in the following Clear, I could have understood the problem before. Anyway, it was useful what you pointed out. I didn't care about these problems and I can only learn doing errors. Thanks again.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
It is clear. Still I ask: why the crash on Contain in my first post? This misleaded me. Only this. If crash were in the following Clear, I could have understood the problem before. Anyway, it was useful what you pointed out. I didn't care about these problems and I can only learn doing errors. Thanks again.
This may caused by deleting the lines of code without stopping the debug mode (hot code swap) so the debugger may pointing to the wrong line of code.

To solve the problem, just create a dynamic list.
B4X:
    'Dim ll As List = File.ListFiles(File.DirAssets) ' <-- Error
   
    ' This create a dynamic list
    Dim assetfiles As List = File.ListFiles(File.DirAssets)
    Dim ll As List
    ll.Initialize
    For Each fname As String In assetfiles
        ll.Add(fname)
    Next
   
    For Each sz As String In ll
        If sz.Contains(".png") Then
            Log("PNG file detected!")
        End If
    Next
    ll.clear

Edit: by the way, you may use
B4X:
If sz.EndsWith(".png") Then
to check the file extension.
 
Last edited:
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I guess there is no need to create another list. I just removed the Clear and the program works well. The weird is only the fact that the wrong Clear produces (in that case) the crash on another instruction, namely the Contain, fooling a simple user like me. No actual problem. Thanks.
 
Upvote 0

b4x-de

Active Member
Licensed User
Longtime User
@aeric @GiovanniPolese Thank you! Here you both gave a perfect example for creating helpful answers. (More and more I'm missing this ability or willingness in other threads of this forum.) I had read about the non-dynamic list before, but wasn't aware of the consequences. Thanks again for clarification!
 
Upvote 0
Top