# Hoarding Gold help

**URL:** <https://discourse.codecombat.com/t/hoarding-gold-help/3572>\
**Category:** Adventurer\
**Created:** [April 16, 2015, 10:47pm UTC](https://discourse.codecombat.com/t/hoarding-gold-help/3572 "2015-04-16T22:47:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![noxlux](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/noxlux/32/2431_2.png) [@noxlux](https://discourse.codecombat.com/u/noxlux)\
**Post date:** [April 16, 2015, 10:47pm UTC](https://discourse.codecombat.com/t/hoarding-gold-help/3572/1 "2015-04-16T22:47:39Z")

</div>

Having trouble with “Hoarding Gold”. I’m getting an error on line 5 that says, "cannot read property ‘pos’ of null. What am I doing wrong? It doesn’t seem to me to be any different than any other time when my character has to read an X,Y position and move to it. Usually I use something like:

```python
    var coin = findNearest(findItem());
    coinPosition = coin.pos;
    coinX = coin.x;
    coinY = coin.y;
    this.moveXY(coinX, coinY)

```

I tried using the above first but couldn’t make it work, so I tried toying with it to get what I’ve got now, and it still doesn’t work.

```python
    var totalGold = 0;
loop {
    var coin = this.findNearest(this.findItems());
    if (totalGold < 25) {
    this.moveXY(coin.pos.x, coin.pos.y);
    totalGold = 0 + coin.value;
    } else if (totalGold >= 25) {
        break;}
}
this.moveXY(58, 33);
this.say("Done collecting gold!");
this.say(totalGold);

```

---

<div class="post-metadata">

**Author:** ![Serg](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/serg/32/3260_2.png) [@Serg](https://discourse.codecombat.com/u/Serg)\
**Post date:** [April 16, 2015, 11:25pm UTC](https://discourse.codecombat.com/t/hoarding-gold-help/3572/2 "2015-04-16T23:25:16Z")

</div>

**cannot read property ‘pos’ of null.**

Now, it’s not often explained clearly, but, you’re examining an item that doesn’t exist.

You are trying to check the ‘pos’ of something that doesn’t exist, specifically.

In this example, and in the future it’s important to check if your functions are actually returning something useful.

```python
var coin = this.findNearest(this.findItems())
if(coin) {
  //Move to coin.
} else {
  //It didn't find any nearby coins!
}

```

---

<div class="post-metadata">

**Author:** ![noxlux](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/noxlux/32/2431_2.png) [@noxlux](https://discourse.codecombat.com/u/noxlux)\
**Post date:** [April 17, 2015, 12:28am UTC](https://discourse.codecombat.com/t/hoarding-gold-help/3572/3 "2015-04-17T00:28:11Z")

</div>

Ah, I see now, I just needed to add something to test if there was a coin. I altered it to look for a coin AND look at the current value.

Appreciate your help, the problem was not very obvious to me.
