Code Validation (mildly) flawed...

I have one warning in my Java file, regarding the following snippet of code:

 0)
{
    Collection unselectedOptions = new ArrayList();
    for (Iterator i = *items.iterator(); *i.hasNext();)
    .
    .
    .
]]>

IntelliJ is telling me that items.iterator() would throw a null pointer exception. But as you can see, it won't enter the loop if it is null, because of the null/size check before it.

I know I can't expect IntelliJ to look throughout my code for all instances, but this kind of error makes me double/triple check everything IntelliJ tells me.

I just thought I would post this, in case it really WAS a bug.

Thanks,

Clint

5 comments
Comment actions Permalink

I didn't check, but it might go away you use

numberOfInstructionOptions != 0

instead of

numberOfInstructionOptions > 0.

It would surprise me very much if the dataflow engine knew
!(0 > 0), but I'm sure it knows !(0 != 0).

--Dave Griffith

0
Comment actions Permalink

I think learning to avoid situations like this will lead you to better
code. I think your code is misleading and hard to read because of that
conditional check. IDEA, and myself, would stop complaining if you
changed it like this:

if (items != null) {
boolean isReplace = options.isInstruction();

if (isReplace && !items.isEmpty())
{
Collection unselectedOptions = new ArrayList();
for (Iterator i = *items.iterator(); *i.hasNext();)

Clint wrote:

I have one warning in my Java file, regarding the following snippet of code:

 int numberOfInstructionOptions = items == null ? 0 : items.size();
> boolean isReplace = options.isInstruction();
> 
> if (isReplace && numberOfInstructionOptions > 0)
> {
>     Collection unselectedOptions = new ArrayList();
>     for (Iterator i = *items.iterator(); *i.hasNext();)
>     .
>     .
>     .
> ]]>

IntelliJ is telling me that items.iterator() would throw a null pointer exception. But as you can see, it won't enter the loop if it is null, because of the null/size check before it.

I know I can't expect IntelliJ to look throughout my code for all instances, but this kind of error makes me double/triple check everything IntelliJ tells me.

I just thought I would post this, in case it really WAS a bug.

Thanks,

Clint

0
Comment actions Permalink

Moreover, if "items" is a member variable (i.e., not local) and you're not synchronizing on it, it could have it's value set to null between the null check and the iterator() call.

0
Comment actions Permalink


Good points... the reason that it's written this way is that later on in the code I use that size, so I was trying to simplify my code usage. Normally, I'd be writing that particular if statement differently. I may still. :)

I guess there's a fine line between code structure issues and whether or not something is indeed a possible error. It was just something that jumped out at me.

0
Comment actions Permalink


This fixed the warning. Is this something that should be fixed in IntelliJ?

0

Please sign in to leave a comment.