False warning

I've written the following code but IntelliJ says "Condition 'lastElement != null' is always 'false'"...

 someList) {

        Long lastElement = null;
        for (Long element : someList) {
            if (lastElement != null && lastElement!=element) {
                throw new RuntimeException();
            }
        }

    }
]]>


EDIT: I'm using IntelliJ 8, #9144...

Edited by: Gerson K. M. on Nov 18, 2008 6:45 PM

2 comments
Comment actions Permalink

What am I missing? lastElement starts out null (thus the condition is false, at least initially). As a local variable we can know without doubt there is no way for it to take on any other value than null by simply inspecting the code of the method. Thus lastElement != null is always false.


Randall Schulz

0
Comment actions Permalink

Hello Gerson K. M.,

The warning is correct. Your code never changes the value of lastElement.

I've written the following code but IntelliJ says "Condition
'lastElement != null' is always 'false'"...

 private void doSomething(List someList) {
> Long lastElement = null;
> for (Long element : someList) {
> if (lastElement != null && lastElement!=element) {
> throw new RuntimeException();
> }
> }
> }
> ]]>

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Please sign in to leave a comment.