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
Please sign in to leave a comment.
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
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:
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.
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.
This fixed the warning. Is this something that should be fixed in IntelliJ?