# nil values which should not be nil.. \\platform\\resources\\init.lua?

**URL:** <https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146>\
**Category:** Games\
**Created:** [August 21, 2020, 9:02pm UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146 "2020-08-21T21:02:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![DigiNick](https://forums.solar2d.com/user_avatar/forums.solar2d.com/diginick/32/218_2.png) [@DigiNick](https://forums.solar2d.com/u/DigiNick)\
**Post date:** [August 21, 2020, 9:02pm UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146/1 "2020-08-21T21:02:23Z")

</div>

Hi all,

Just got this error twice tonight, in completely different places in my code and referencing somewhere I’ve never seen before and cannot find:

\corona\corona\platform\resources\init.lua:202 / 189…?

I don’t think this value is / should be nil either.

Anyone got any ideas?

Thanks!!

![initlua202](https://forums.solar2d.com/uploads/default/original/1X/60e055cb09908362dfe8b9b21f337458355403a7.jpeg)

![initlua202_2](https://forums.solar2d.com/uploads/default/original/1X/78a2685716dfca465d570442877ea150abd25dcf.jpeg)

---

<div class="post-metadata">

**Author:** ![anon63346430](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@anon63346430](https://forums.solar2d.com/u/anon63346430)\
**Post date:** [August 21, 2020, 9:35pm UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146/2 "2020-08-21T21:35:44Z")

</div>

That just means the error is being raised from a function that is fired from a timer or enterFrame event. The error is still in your code though.

For example, the second one means you are trying to apply a force to an object that doesn’t exist.

---

<div class="post-metadata">

**Author:** ![DigiNick](https://forums.solar2d.com/user_avatar/forums.solar2d.com/diginick/32/218_2.png) [@DigiNick](https://forums.solar2d.com/u/DigiNick)\
**Post date:** [August 21, 2020, 9:42pm UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146/3 "2020-08-21T21:42:35Z")

</div>

Hi SGS,

Thanks for the response.  
I don’t think I’m using any enterFrame’s or timers in this particular situation.

I’ve tried my best to trap the nil on the second example - the code is:

 ![initlua202_applyLinearImpulse](https://forums.solar2d.com/uploads/default/original/1X/eac831067b19e358168e2119d32329abcf260827.jpeg)

```
if e.sType == 8 then
	playSFX("gertrudeattackSound")
	if e ~= nil and e.rotation ~= nil and e.x ~= nil and e.y ~= nil then
		e:applyLinearImpulse(Sin((360 - e.rotation)*RadtoDeg) * 3750, Cos((360 - e.rotation)*RadtoDeg)* 3750, e.x, e.y)
	end
else

```

Could it still be that?

Nick

---

<div class="post-metadata">

**Author:** ![depilz](https://forums.solar2d.com/user_avatar/forums.solar2d.com/depilz/32/143_2.png) [@depilz](https://forums.solar2d.com/u/depilz)\
**Post date:** [August 21, 2020, 10:40pm UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146/4 "2020-08-21T22:40:35Z")

</div>

Why do you have so many checks in this if statement?

> [@DigiNick](#):
>
> ```auto
> if e ~= nil and e.rotation ~= nil and e.x ~= nil and e.y ~= nil then
> e:applyLinearImpulse(Sin((360 - e.rotation)*RadtoDeg) * 3750, Cos((360 - e.rotation)*RadtoDeg)* 3750, e.x, e.y)
> end
> 
> ```

If all those are really necessary maybe you are not removing your objects correctly, that could explain the wrong call to `applyLinearImpulse`, I’m just not sure about the castTime.

---

<div class="post-metadata">

**Author:** ![roaminggamer](https://forums.solar2d.com/user_avatar/forums.solar2d.com/roaminggamer/32/134_2.png) [@roaminggamer](https://forums.solar2d.com/u/roaminggamer)\
**Post date:** [August 22, 2020, 12:32am UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146/5 "2020-08-22T00:32:40Z")

</div>

I have a function I use when there is the possibility an object reference may be invalid (added onto display.\*):

```auto
function display.isValid( obj )
  return( obj.removeObject ~= nil and type(obj.removeObject) == "function" )
end

```

You could do this:

```auto
if( display.isValid(e) ) then
		e:applyLinearImpulse(Sin((360 - e.rotation)*RadtoDeg) * 3750, Cos((360 - e.rotation)*RadtoDeg)* 3750, e.x, e.y)
	end

```

**Tip:** Use better variable names. ‘e’ is pretty bad for future legibility.

---

<div class="post-metadata">

**Author:** ![DigiNick](https://forums.solar2d.com/user_avatar/forums.solar2d.com/diginick/32/218_2.png) [@DigiNick](https://forums.solar2d.com/u/DigiNick)\
**Post date:** [August 22, 2020, 3:08pm UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146/6 "2020-08-22T15:08:03Z")

</div>

Hiya,

I’ve added the extra checks to trap this error above - obviously to no avail - and left them in for the screenshot / snippet so you could see nothing in the applyLinearImpulse statement could be nil.

Ordinarily I’d just have the `If object ~= nil` - in this case the object is e. I use e as a shortening for enemy as it’s typed so often in my game - but yes agree @roaminggamer it’s a poor variable name!! I’m not so bad elsewhere in code - promise!! 😃

Just FYI this object is the players familiar - Gertrude - she’s created at game load and not destroyed at any point. Does this help?

Thanks again for all the feedback and sorry for any confusion,

Nick

PS I’m starting to see init.lua referenced more and more suddenly - in this because the information included in error messages has been increased? I’ve just recently upgraded to version 2020.3609.

---

<div class="post-metadata">

**Author:** ![roaminggamer](https://forums.solar2d.com/user_avatar/forums.solar2d.com/roaminggamer/32/134_2.png) [@roaminggamer](https://forums.solar2d.com/u/roaminggamer)\
**Post date:** [August 22, 2020, 3:56pm UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146/7 "2020-08-22T15:56:50Z")

</div>

Checking if the object reference is `nil` won’t catch your issue.

The reference is NOT `nil` at the time you’re trying to use it. It is invalid. i.e. It has value in it that references a non-existent object.

What is on lines:

- 4557
- 4651
- 4725  
in `main.lua`? As per the dialog, that is where the issue is or very near it.

You will get errors **similar** to that, this happens:

```auto
local obj = display.newCircle( ... )
-- Add body, etc. here

timer.performWithDelay( 1000, function() obj:applyImpulse( ... ) end )

display.remove( obj )

```

1-second later you’ll get a error.

This is effectively what is happening in your game. You have to hunt down:

- Where you’re destroying objects and do a cleanup: Cancel listeners and timers.
- Where you’re getting these errors (doing the actual action that is complained about) and check for valid objects.

---

<div class="post-metadata">

**Author:** ![DigiNick](https://forums.solar2d.com/user_avatar/forums.solar2d.com/diginick/32/218_2.png) [@DigiNick](https://forums.solar2d.com/u/DigiNick)\
**Post date:** [August 22, 2020, 4:06pm UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146/8 "2020-08-22T16:06:44Z")

</div>

Awesome advice - thank you.  
I cancel timers on destroyed enemies but now you mention it I think there will be some listeners hanging around!  
Might not have time today but I will do as you suggest and let you know how I get on.  
With help from you gurus I’m getting there!  
Thanks again 🙂

PS 4557 is the line in question - the others are lines in AI functions which call the other AI functions.

Only problem here is as mentioned above the object in question is never destroyed…! 🤔

---

<div class="post-metadata">

**Author:** ![DigiNick](https://forums.solar2d.com/user_avatar/forums.solar2d.com/diginick/32/218_2.png) [@DigiNick](https://forums.solar2d.com/u/DigiNick)\
**Post date:** [August 23, 2020, 3:30pm UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146/9 "2020-08-23T15:30:40Z")

</div>

I’ve been working on this today.  
Still no further forward. I’ve removed listeners from other objects that needed it after your advice above - although I thought I had read somewhere, many moons ago, that sprite listeners were removed with sprites. Still better to be safe than sorry.  
This object referenced above however is not removed at any point, and the code is not run through a timer, so I’ve no idea.  
The only thing I can think of now is that this error has only started appearing after upgrade to the latest version of Solar 2D…

---

<div class="post-metadata">

**Author:** ![DigiNick](https://forums.solar2d.com/user_avatar/forums.solar2d.com/diginick/32/218_2.png) [@DigiNick](https://forums.solar2d.com/u/DigiNick)\
**Post date:** [August 23, 2020, 4:20pm UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146/10 "2020-08-23T16:20:26Z")

</div>

I’ve sussed it.  
I’m now removing the physics body with a timer set to 50ms to stop the “can’t change physics bodies whilst number crunching error”.  
Thus when summoning the entity whilst it still exists I’m actually now removing the physics body 50ms after creating it!  
Can now resolve no problem.  
Thanks so much for the assistance!!! 🙂

---

<div class="post-metadata">

**Author:** ![roaminggamer](https://forums.solar2d.com/user_avatar/forums.solar2d.com/roaminggamer/32/134_2.png) [@roaminggamer](https://forums.solar2d.com/u/roaminggamer)\
**Post date:** [August 23, 2020, 5:33pm UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146/11 "2020-08-23T17:33:22Z")

</div>

The only display object listeners that are not removed automatically, are **Global Event Listeners**.

- Those added with `Runtime:addEventListener()`
- Calls to `timer.performWithDelay()`

---

<div class="post-metadata">

**Author:** ![DigiNick](https://forums.solar2d.com/user_avatar/forums.solar2d.com/diginick/32/218_2.png) [@DigiNick](https://forums.solar2d.com/u/DigiNick)\
**Post date:** [August 23, 2020, 5:35pm UTC](https://forums.solar2d.com/t/nil-values-which-should-not-be-nil-platform-resources-init-lua/352146/12 "2020-08-23T17:35:09Z")

</div>

Thank you for letting me know.  
Every day is a school day!!  
😃
