Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java "null" value never assigned to my Runnable

So I have this code:

    for(Runnable loadTask : mMeshLoadMap.values())
    {
        if(loadTask != null)
        {
            loadTask.run();
            loadTask = null;
        }
    }

My problem is the loadTask = null statement is never executed, Android Studio says "The value "null" assigned to loadTask is never used"...

Plus when I set a breakpoint at that line the debugger never reaches it. It can reach loadTask.run() but not loadTask = null And I am sure that loadTask.run() doesn't block soo long

like image 973
user2591935 Avatar asked Aug 17 '26 18:08

user2591935


1 Answers

It certainly can be assigned, but it is pointless. You'd only be nulling the reference named loadTask (which is out of scope outside the block). You cannot update the reference in the array. The For-Each Loop says (in part)

The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it.

like image 67
Elliott Frisch Avatar answered Aug 20 '26 09:08

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!