MV typeError - cannot read property x of null

Hi everyone!

Can't get rid of this strange error. Here's console info:
error.png

After some UI images move around on a screen I get 'TypeError - cannot read property x of null'. Maybe someone can help me with this?
Based on that error trace, it looks like:
  1. The problem is in a Script command.
  2. The event running that command is not set to Trigger: Parallel.
Looks like you've got something like A.x, but A is actually null at the time.
Based on that error trace, it looks like:
  1. The problem is in a Script command.
  2. The event running that command is not set to Trigger: Parallel.
Looks like you've got something like A.x, but A is actually null at the time.
Thank you! I found the script that broke my game!
On the same note - maybe you know If there any way to check if some game entity exists? Like, if I want to script call A.x, can I make a condition branch asking if A.x is not null?
A.x being Null is NOT the problem reported.

A is being Null (does not exist), and that is why the reference to A.x has failed.

so what is A supposed to be?
You can simply check the value of A before trying to reference its properties, e.g.
  • This equals A.x if A is truthy, or A otherwise:
    JavaScript:
    A ? A.x : A
  • This equals null if A is specifically null, or A.x otherwise:
    JavaScript:
    A === null ? null : A.x
Like Andar says, though, in general you'd want to understand why A (whatever that is in your case) is null in the first place:
  • If it is never expected to be null, then you have a deeper problem.
  • If it is expected to be null in some cases, that lets you know what to put in the "else" clause.
With more detail about what "A" is (a map event maybe?) someone may be able to tell you why the error happened.
A.x being Null is NOT the problem reported.

A is being Null (does not exist), and that is why the reference to A.x has failed.

so what is A supposed to be?
In my case, A is script command for a bar to move around a screen. Seems like the bar in question is removed with Galv.VBAR.remove(1); somewhere else in my game, and it's the reason for this TypeError!
1776171687169.png

You can simply check the value of A before trying to reference its properties, e.g.
  • This equals A.x if A is truthy, or A otherwise:
    JavaScript:
    A ? A.x : A
  • This equals null if A is specifically null, or A.x otherwise:
    JavaScript:
    A === null ? null : A.x
Like Andar says, though, in general you'd want to understand why A (whatever that is in your case) is null in the first place:
  • If it is never expected to be null, then you have a deeper problem.
  • If it is expected to be null in some cases, that lets you know what to put in the "else" clause.
With more detail about what "A" is (a map event maybe?) someone may be able to tell you why the error happened.
I figured out why this error happens: I have Galv's plugin for creating progress bars, and one specific bar is deleted with Galv.VBAR.remove(1); somewhere else in my game. So, when I try to move this bar with script call, it gives me this error. With your help I can add condition branch to ask If bar exists, in case I would need to circumvent this problem instead of finding where exactly the bar gets deleted.

Thanks you both! 😢
if your bar is removed beforehand, than make a check before that.

if (!Galv.vbar(1)) or however (which I need to check), than create it first.
than mod it after.

unless you know sure it exist and not removed.

you may need to check where and how, if its an "hide/show" by switch.
check if that switch is ON instead, than mod it, else do nothing.
I haven't actually checked out Galv's plugin, but assuming the script call syntax in your screenshot is correct, you could rewrite it as:
JavaScript:
var bar = Galv.VBAR.mod(1);            // get reference to variable bar
if (bar) {                             // if bar exists
  bar.x -= 19;                         // subtract 19 from its X coordinate
  $gameVariables.setValue(33, bar.x);  // store its current X coordinate in game variable 33
}
Alternatively, if that event only handles bar positioning, simply turn off the event when the bar is not there.
I haven't actually checked out Galv's plugin, but assuming the script call syntax in your screenshot is correct, you could rewrite it as:
JavaScript:
var bar = Galv.VBAR.mod(1);            // get reference to variable bar
if (bar) {                             // if bar exists
  bar.x -= 19;                         // subtract 19 from its X coordinate
  $gameVariables.setValue(33, bar.x);  // store its current X coordinate in game variable 33
}
Alternatively, if that event only handles bar positioning, simply turn off the event when the bar is not there.
Thank you for this detailed example!

I found an event that is messing with this progress bar and tried to fix in, but the results are kinda strange.

So, if player activates an event while progress bar is present and variable A is set to 0, then this progress bar will move. But other event, that supposed to set variable A to 0 is pretty long complex event (trigger set on 'action button'). And it looks like this event is working correctly, but every so often it just misses the part with setting A to 0 (for some reason?????). I found no obvious reason why the same event should work like this. Is it possible that I made it too long and now it's taking too much CPU to properly finish it's execution? Could breaking this event into several pages with local switch activation help with any of it?
variable A is set to 0, then this progress bar will move.
but every so often it just misses the part with setting A to 0 (for some reason?????)

Variables and events don't work that way, not unless you include specific commands to make the event skip the control variable command.

Instead it sounds as if the variable in question is used in a different part of your game, and that this other use for the variable messes up with your interface.

if that o9ther use is a regular event command, then you can search for it with the event searcher.
however the event searcher cannot detect cases where a plugin is configured to use said variables.

and that is the most common cause for problems like this:
that you added a plugin that requires the use of a variable, but did not configure said plugin to use a reserved variable that is free - or if you did you forgot to name said variable so when you later made your other events you used the variable already linked to the plugin without intending to.

So if the event searcher doesn't find anything, you'll have to check every plugin configuration until you find the one that uses the problematic variable
I agree, it seems like the variable is being used elsewhere. In case you're not familiar with the Event Searcher, it can be accessed via the Tools menu at the top, or by clicking this toolbar button:

event-searcher-toolbar.png
Like Andar says, though, it won't detect use in script calls or plugins.
Hi all,
Variables and events don't work that way, not unless you include specific commands to make the event skip the control variable command.

Instead it sounds as if the variable in question is used in a different part of your game, and that this other use for the variable messes up with your interface.

if that o9ther use is a regular event command, then you can search for it with the event searcher.
however the event searcher cannot detect cases where a plugin is configured to use said variables.

and that is the most common cause for problems like this:
that you added a plugin that requires the use of a variable, but did not configure said plugin to use a reserved variable that is free - or if you did you forgot to name said variable so when you later made your other events you used the variable already linked to the plugin without intending to.

So if the event searcher doesn't find anything, you'll have to check every plugin configuration until you find the one that uses the problematic variable
&
I agree, it seems like the variable is being used elsewhere. In case you're not familiar with the Event Searcher, it can be accessed via the Tools menu at the top, or by clicking this toolbar button:

View attachment 364902

Like Andar says, though, it won't detect use in script calls or plugins.
I looked up through both event searcher and through all plugins, and found where the problem was! Thank you both for help and your input

Original thread: https://forums.rpgmakerweb.com/threads/typeerror-cannot-read-property-x-of-null.183196/