# "Item is null" despite if(item)

**URL:** <https://discourse.codecombat.com/t/item-is-null-despite-if-item/5131>\
**Category:** Uncategorized\
**Created:** [October 4, 2015, 7:35pm UTC](https://discourse.codecombat.com/t/item-is-null-despite-if-item/5131 "2015-10-04T19:35:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pevensie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/pevensie/32/3166_2.png) [@Pevensie](https://discourse.codecombat.com/u/Pevensie)\
**Post date:** [October 4, 2015, 6:39pm UTC](https://discourse.codecombat.com/t/item-is-null-despite-if-item/5131/1 "2015-10-04T18:39:14Z")

</div>

I’m having problems with my code. It keeps telling me that I have an error on line 15, which is `var itemPos = item.pos;`,but I’m not sure what’s wrong.

```
loop {
var flag = this.findFlag();
var item = this.findNearestItem();
if (flag) {
    var pos = flag.pos;
    var fX = pos.x;
    var fY = pos.y;
    this.moveXY(fX, fY);
    this.buildXY("fire-trap", fX, fY);
    this.pickUpFlag(flag);
} else if (item); {
    var itemPos = item.pos;
    var itemX = itemPos.x;
    var itemY = itemPos.y;
    this.moveXY(itemX, itemY);
    }

```

}

Thanks!

---

<div class="post-metadata">

**Author:** ![UltCombo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ultcombo/32/3822_2.png) [@UltCombo](https://discourse.codecombat.com/u/UltCombo)\
**Post date:** [October 4, 2015, 7:19pm UTC](https://discourse.codecombat.com/t/item-is-null-despite-if-item/5131/2 "2015-10-04T19:19:04Z")

</div>

This line:

```python
} else if (item); {

```

Should not contain a semicolon before the opening curly brace. It should be:

```python
} else if (item) {

```

I don’t see anything wrong besides that. Sometimes the linter gets thrown off a bit.

---

<div class="post-metadata">

**Author:** ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)\
**Post date:** [October 4, 2015, 7:33pm UTC](https://discourse.codecombat.com/t/item-is-null-despite-if-item/5131/3 "2015-10-04T19:33:25Z")

</div>

_I apologize in advance, I spent the last three hours explaining stuff and what you read now is exactly what I feel like_

> Dear J\_F\_B\_M, why is there an error?

`if (item);`

This line does nothing. It is an empty statement. It is equal to:

```python
if(item) {
    // Do nothing
}

```

This is because a single `;` is a valid JS-Statement. In fact, `;;;;;` is a total of 5 statements (which all do nothing).

* * *

> But J\_F\_B\_M, there is a curly bracket behind that…

Jup, you’re right. In other languages (like Java, which looks similar but is different) this would be important. Here it isn’t (in the same way). The curly brackets enclose a **block statement**. A block statement encloses zero or more other statements (which can also be more block statements).

* * *

> But J\_F\_B\_M, why do you tell us this boring stuff?

You haven’t known up until now about it, but you already used block statements quite often. An `if`-statement can only be followed by one other statement. But sometimes this is not enough, we want multiple statements “inside” an if. The solution: A block statement.

* * *

> Come on, get to the point. What is my problem?

```python
if (flag) {
    // Bla bla
} else if (item); {
    // More Bla
}

```

This code is equal to the following code (I added some intendations

```python
if (flag) {
    // Bla bla
} else if (item)
    ;

{
    // More Bla (including var itemPos = item.pos;)
}

```

As the block statement is unimportant here we could also remove it:

```python
if (flag) {
    // Bla bla
} else if (item)
    ;

// More Bla (including var itemPos = item.pos;)

```

**Now there is definitely a problem.**  
`More Bla` is not inside the if. At some point your code will run like this:

```plaintext
Is there a flag? -> No, go to else
Else, is there an item? -> No, don't execute the ;-statement

Cool, "if-else" is over, so the next statement is...
"var itemPos = item.pos;"
But "item == null"... better throw a cryptic error instead.

```

You can see now that even though it looks like it at first you never actually check if `item==null` (at least not in a meaningful way).

* * *

> And why did you post that after UltCombos answer?

I started typing first, but needed longer. Also my answer is more exhaustive.

---

<div class="post-metadata">

**Author:** ![UltCombo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ultcombo/32/3822_2.png) [@UltCombo](https://discourse.codecombat.com/u/UltCombo)\
**Post date:** [October 4, 2015, 7:57pm UTC](https://discourse.codecombat.com/t/item-is-null-despite-if-item/5131/4 "2015-10-04T19:57:58Z")

</div>

@J_F_B_M Nice explanation! I was not expecting such an extensive answer to this commonplace problem, well done.

By the way, I’d like to add that using “standalone” blocks does not make much sense in JavaScript prior to ECMAScript 2015 (which CodeCombat does not support yet, anyway) as block scoping was not quite supported¹ until then. Quoting [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block#No_block_scope):

> Although “standalone” blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don’t do what you think they do, if you think they do anything like such blocks in C or Java. For example:
> 
> ```js
> var x = 1;
> {
> var x = 2;
> }
> console.log(x); // logs 2
> 
> ```
> 
> This logs 2 because the var x statement within the block is in the same scope as the var x statement before the block. In C or Java, the equivalent code would have outputted 1.

* * *

¹ It is technically possible to (ab)use `try/catch` blocks’ [exception identifier](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch#The_exception_identifier) to simulate block scoping, but that is not related to the main topic here. 😁

---

<div class="post-metadata">

**Author:** ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)\
**Post date:** [October 4, 2015, 8:50pm UTC](https://discourse.codecombat.com/t/item-is-null-despite-if-item/5131/5 "2015-10-04T20:50:38Z")

</div>

> [@UltCombo](#):
>
> such an extensive answer to this commonplace problem

Actually I think this is a pretty uncommon problem. Most people do not tend to place semicoli in places where they are syntactically correct but semantically nonsense. And even less people place a semicolon in a place where it actually changes the semantic.

* * *

> [@J\_F\_B\_M](#):
>
> […] In other languages (like Java, which looks similar but is different) this would be important. Here it isn’t […]

> [@UltCombo](#):
>
> […] that using “standalone” blocks does not make much sense in JavaScript […]

Those two lines (and their surroundings) belong together 😄

---

<div class="post-metadata">

**Author:** ![UltCombo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ultcombo/32/3822_2.png) [@UltCombo](https://discourse.codecombat.com/u/UltCombo)\
**Post date:** [October 4, 2015, 10:44pm UTC](https://discourse.codecombat.com/t/item-is-null-despite-if-item/5131/6 "2015-10-04T22:44:20Z")

</div>

> [@J\_F\_B\_M](#):
>
> Actually I think this is a pretty uncommon problem. Most people do not tend to place semicoli in places where they are syntactically correct but semantically nonsense. And even less people place a semicolon in a place where it actually changes the semantic.

That is true.

Perhaps I didn’t express myself well. I meant that I believe the specific “semicolon between a control flow statement and a block statement” issue to be a common one as virtually every developer has made this mistake sometime in their lives. And this often happens at the very beginning of their learning. That is why I replied with a simple “how to fix it” answer rather than a very detailed one that may overload their beginner programming minds. Borrowing Nick’s words:

> [@Level: A Mayhem of Munchkins Help](https://discourse.codecombat.com/t/level-a-mayhem-of-munchkins-help/4909/3):
>
> The act of fixing the code is way better for learning than anything else.

(Note: I actually find your answer very comprehensive, detailed and easy to understand, but a complete beginner may find otherwise. Either way, it will be there in case they want to revisit it in the future. It also provides useful insight that helps actually understanding the programming language syntax.)

As for how common this issue really is nowadays, I’m not sure but take in consideration that one of the main reasons for the rise of the [OTBS](https://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS) and [Stroustrup](https://en.wikipedia.org/wiki/Indent_style#Variant:_Stroustrup) coding styles is that putting the opening curly brace in the same line as the control flow statement makes it harder to accidentally insert an unwanted semicolon between them.
