Error instantiating System class.Nullreferenceexception: Undefined object reference for an instance of an object

Asked

Viewed 2,081 times

2

Good morning, can anyone find the mistake I’m making by instantiating? I get that message back:

Method error: Render : System.Nullreferenceexception: Reference to object not defined for an object instance. in Autophaserun.Enabler.Enabled() in Autophaserun.AutoPhaseRun.Render() in Poehud.Plugins.Baseplugin. _Render()

Class:

using PoeHUD.Poe.Components;
using System;
using PoeHUD.Plugins;
using System.Windows.Forms;
using PoeHUD.Controllers;
public bool Enabled() {
        if (GameController.Area.CurrentArea.IsHideout 
            || GameController.Area.CurrentArea.IsTown 
            || !GameController.Player.IsAlive 
            || !Settings.Enable 
            || GameController.Game.IngameState.Data.LocalPlayer.GetComponent<Life>().Buffs.Exists(b => b.Name == "grace_period")
            || Control.IsKeyLocked(Keys.CapsLock) == false)
            return false;

        if (GameController.Game.IngameState.IngameUi.InventoryPanel.IsVisible
            || GameController.Game.IngameState.IngameUi.OpenLeftPanel.IsVisible
            || GameController.Game.IngameState.IngameUi.AtlasPanel.IsVisible
            || GameController.Game.IngameState.IngameUi.OpenRightPanel.IsVisible
            || GameController.Game.IngameState.IngameUi.SyndicatePanel.IsVisible
            || GameController.Game.IngameState.IngameUi.IncursionWindow.IsVisible
            || GameController.Game.IngameState.IngameUi.TreePanel.IsVisible
            || GameController.Game.IngameState.IngameUi.HiddenSkillBar.IsVisible
            || GameController.Game.IngameState.UIRoot.GetChildFromIndices(1, 111).ChildCount > 0)
            return false;

        else
            return true;
    }

Main:

using PoeHUD.Poe.Components;
using System;
using PoeHUD.Plugins;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using PoeHUD.Controllers;
public class AutoPhaseRun : BaseSettingsPlugin<AutoPhaseRunSettings>
    {
        private DateTime lasttime = new DateTime();
        Enabler enabler = new Enabler();

        public override void Render()
        {
            try
            {
                bool teste = enabler.Enabled();
                if (teste == false)
                    return;

                if ((DateTime.Now - lasttime).TotalSeconds > Settings.delay.Value)
                {
                    var buffs = GameController.Game.IngameState.Data.LocalPlayer.GetComponent<Life>().Buffs;
                    if (!buffs.Exists(b => b.Name == "new_phase_run"))
                    {
                        Keyboard.KeyPress(Settings.pressedKey.Value);
                        lasttime = DateTime.Now;
                    }
                }
            }
            catch (Exception e)
            {
                LogMessage(e.Message, 3);
                LogMessage(e.Source, 3);
                throw;
            }

        }
    }
  • Could include the usings?

  • See if any hint in this answer helps you: https://answall.com/a/369628/35358

  • Create a break-point in the first if within the method bool Enabled()(guess is a method whose class has been omitted) and pass the mouse pointer for each instance compared within the two ifs, or create a Quick watch for all identifiers, surely some is null.

1 answer

1

Debug and see which instance should be instantiated and is not.

If you are making this exception then it is because you are trying to use an object that is not instantiated.

For example

String s = null;
var i = s.Length;

ERROR: [System.Nullreferenceexception: Object Reference not set to an instance of an Object.

  • The only thing I discovered is that if in the class I comment everything and leave only "Return true" it works but still can’t find the error...

  • 1

    @Thiagopiardi, the Mauroalmeida answer is correct, debugging you will find exactly the line of the error, it may even be in the if comparisons, see if that’s where it breaks, debug your code.

  • The problem is that I can’t run this code on VS because it is a dll

  • How come you can’t run and can see the error message? compiled and executed in the console?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.