Accessed none trying to read property (widget reference)

Hello! In my main menu widget, I am trying to make it so when the Settings button is clicked, the game first checks whether the Credits widget is already open, and if it is, the Credits widget will close. It would work the same when trying to access the Credits - checking if the Settings menu is already open, etc.

However, I keep getting these errors when I try to click between the Settings and Credits buttons in-game:

Here is what the blueprint looks like:

I’m not sure what I could be doing wrong as I have tried to set both CreditsWidgetRef and MainMenuSettingsRef variables in the Event Pre Construct node. I have also made it so that both of these variables are set to Instance Editable and Expose on Spawn. If anyone has any ideas, it would be great to hear them. Thank you =)

In your current setup, the set variable nodes run before the widgets are even created, hence “Accessed None”. Instead, the Set nodes should be used after creating the widgets like so :

2 Likes

Hey @d_aydream!

Extrone’s answer above is on the right track, but I’m not sure it will work as it is still calling the variables before they’ve been set.

You could try using “IsValid?” checks to check if that widget is there or not- because the thing is, if the widget is not set as the reference, or it’s been removed from parent (widget death) it will give null reference exceptions because they’re not there anymore.

Also it could be that you’re using flip-flops. If you’re getting these errors when flipping back and forth between them it could be that you’re getting

1A
2A
1B
2B

Which would mean when you click settings the second time, it’s trying to run path B and remove the widget from parent, but it was removed from parent when you clicked the credits button the first time.

Let me know if I can help elaborate on that!

2 Likes

Hello! Thank you for both of your replies! =) Extrone’s answer cleared up most of the errors but one error still remained:

So, I tried your method - I firstly tried temporarily removing the flip flop node having read your explanation and that fixed another issue I had where it took 2 clicks for either the Settings or Credits buttons to react, so thank you! I then tried to add in the IsValid? nodes - I hope that I have put them in the right place. It appears that with my current code the widgets do not in fact exist when the OnClicked code is being called as when I run the game and try to click the Settings and Credits buttons, nothing appears anymore. I’m not quite sure where in my blueprint to set the MainMenuSettingsRef variable and the CreditsWidgetRef variable though.

Great! Glad we were both able to help!

You should be able to completely remove the “Is in Viewport” Checks with the branch, and move the “isvalid” check to replace them. IsValid=True, IsNotValid=False.

The difference here is that an IsValid check doesn’t call a hard reference on the object, so it doesn’t give a null reference exception if it doesn’t exist. So you only will remove the widget if it exists, doing this. Because otherwise you’re telling it to remove something it can’t remove because it isn’t there and that causes an NRE. :slight_smile:

1 Like

Thank you for the explanation, that clears things up a lot, and your solution has worked perfectly. Thank you very much!! =)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.