Android Question Strange exception - java.util.NoSuchElementException

leitor79

Active Member
Licensed User
Longtime User
Hello,

I'm getting and exception in a super common line of code.

2017-08-28 22_43_17.png
2017-08-28 22_43_27.png


Note that mTmp is initialized.

The code is executed 4 times and the exception rises in the last loop (mTmp.Size = 0)

This code belongs to the main module.

mTmp is declared in Sub Globals.

The function where the error rises in a resumable sub.


Thank you very much!
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
It's hard to tell from what you've posted. Can you post all the relevant code (use the code tags & copy & paste the code from the B4A IDE).

- Colin.
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi Colin, thank you for your answer!

I'll try to resume it; it's a big project, and since I don't know what is causing this issue I don't exactly know what "relevant" is.

I start pasting the simplified code function:


B4X:
private Sub ProblematicSub As ResumableSub
   

    Dim DB As clsDB
    Dim T As CustomType
   
    ProgressDialogShow2("...Calculating...", False)
    Sleep(0)
   
    DB.Initialize

    iMin = 999999
    iCantidadSeriesMaxima = 0
   
    For Each key As Int In Starter.Map.ListKeys
   
        T = Starter.Map.Get(key)
       
        Dim mTMP As Map
        mTMP = DB.LeerValores(T.ID, Global)
       
        If mTMP.Size > iCantidadSeriesMaxima Then
            iCantidadSeriesMaxima  = mTMP.Size
        End If
   
        If iVistaActual = Starter.Vista.Value And mTMP.GetKeyAt(0) < iMin Then
            iMin = mTMP.GetKeyAt(0)
        End If
           
        mValores.Put(T.ID, mTMP)


    Next
   
    ProgressDialogHide
    Sleep(0)
    Return Null
   
   
End Sub

Thank you very much!
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hello,

I've continued looking for clues and I've found a comment here regarding avoid using GetKeyAt and GetValueAt (for performance issues?). Since I was already in despair, I've replaced the GetKey for another method to get the minimum value (that always comes a first).

So, I've replaced this block of code:

B4X:
If iVistaActual = Starter.Vista.Value And mTMP.GetKeyAt(0) < iMin Then
            iMin = mTMP.GetKeyAt(0)
        End If

With this one:

B4X:
If iVistaActual = Vista.Value Then
                For Each key2 As Int In mTmp.Keys
                    If key2 < iMin Then
                        iMin = key2
                    End If
                Next
               'in spite of the minimum value always comes at the first place (element 0), i didn't put a break because 1-there are always a few elements, 2-i can't assure that the map will be always iterated in a given order... or could I?
            End If


Thank you very much!
 
Last edited:
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
(i didin't understood the tip about using the " and abs)
It was a formatting error - I was trying to tell you to use code tags, but the forum took it literally & used code tags...

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
So in the code that you posted, which line is causing the exception?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The code is executed 4 times and the exception rises in the last loop (mTmp.Size = 0)
B4X:
        If iVistaActual = Starter.Vista.Value And mTMP.GetKeyAt(0) < iMin Then
   iMin = mTMP.GetKeyAt(0)
End If

If mTmp.Size is 0 and iVistaActual happens to equal Starter.Vista.Value, then the if condition is going to evaluate mTmp.GetKeyAt(0) which should then throw your java exception, since there should be no map entry at index 0 since size is 0. Hope that makes sense...
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Thank you very much, OliverA!

It makes perfect sense.

What did not, is that the reported error line (811) is not mTMP.GetKeyAt(0) but a few lines above (!?). That disorientated me.


Thank you very much!
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
What is not, is that the reported error line (811) is not mTMP.GetKeyAt(0) but a few lines above (!?). That disorientated me.
Just to clarify (and you may have gotten that from my write-up, although it's a little ambiguous), the mTmp.GetKeyAt(0) that I'm talking about is the one located after the And of the If condition (the mTMP.GetKeyAt(0) < iMin part), not the iMin = mTMP.GetKeyAt(0) inside of the If condition.
 
Upvote 0
Top