What's new
  • Visit Rebornbuddy
  • Visit Panda Profiles
  • Visit LLamamMagic
  • Visit Resources
  • Visit Downloads
  • Visit Portal

Can't find cause of null exception.

strix

New Member
Joined
Feb 13, 2010
Messages
442
Reaction score
18
I've tried everything and still can't find whats wrong with this piece of code:
PHP:
private WoWUnit GetClosestValidCastingTarget
{
    get
    {
        WoWUnit unit = null;

        foreach (KeyValuePair<string,UnitSpellcastingInfo> tar in SpellTracker.Targets)
        {
            if (tar.Value.UnitExists && 
                (unit.Distance < tar.Value.Unit.Distance || unit == null) &&
                tar.Value.Included && //tar.Value.IsUnitEnemy &&
                ((tar.Value.IsCasting && tar.Value.msCastTimeLeft < Settings.CastMillisecondsLeft) ||
                    (tar.Value.IsChannelling && tar.Value.msCastTimeElapsed > Settings.ChannelMillisecondsElapsed)) &&
                tar.Value.Interruptible &&
                InterrupterContainer.Spells.ContainsKey(tar.Value.SpellID) &&
                InterrupterContainer.Spells[tar.Value.SpellID].Include &&
                InterrupterContainer.Spells[tar.Value.SpellID].TagGroupIDs.Any(tag =>
                    InterrupterContainer.Tags.ContainsKey(tag) && InterrupterContainer.Tags[tag].Include) &&
                !InterrupterContainer.Spells[tar.Value.SpellID].TagGroupIDs.Any(tag =>
                InterrupterContainer.Tags.ContainsKey(tag) && InterrupterContainer.Tags[tag].EntirelyExclude)
                )
                unit = tar.Value.Unit;
        }
                    
        return unit;
    }
}

it's throwing
Code:
[1:08:29 AM:879] System.NullReferenceException: Object reference not set to an instance of an object.
   at InterrupterUltimate.InterrupterUltimate.get_GetClosestValidCastingTarget() in e:\A\Honorbuddy\Plugins\Interrupter Ultimate\InterrupterUltimate.Helpers.cs:line 163
   at InterrupterUltimate.InterrupterUltimate.ChooseSpell() in e:\A\Honorbuddy\Plugins\Interrupter Ultimate\InterrupterUltimate.Spells.cs:line 12
   at InterrupterUltimate.InterrupterUltimate.Pulse() in e:\A\Honorbuddy\Plugins\Interrupter Ultimate\InterrupterUltimate.cs:line 56
   at Styx.Plugins.PluginWrapper.Pulse()
Line 163 being " if (tar.Value.UnitExists && "

SpellTracker.Targets is dictionary, all variables have assigned values inside constructor, tried also assigning them in declaration and some other things, can't seem to find the right one.
Any ideas?
 
Last edited:
I've tried everything and still can't find whats wrong with this piece of code:
PHP:
private WoWUnit GetClosestValidCastingTarget
{
    get
    {
        WoWUnit unit = null;

        foreach (KeyValuePair<string,UnitSpellcastingInfo> tar in SpellTracker.Targets)
        {
            if (tar.Value.UnitExists && 
                (unit.Distance < tar.Value.Unit.Distance || unit == null) &&
                tar.Value.Included && //tar.Value.IsUnitEnemy &&
                ((tar.Value.IsCasting && tar.Value.msCastTimeLeft < Settings.CastMillisecondsLeft) ||
                    (tar.Value.IsChannelling && tar.Value.msCastTimeElapsed > Settings.ChannelMillisecondsElapsed)) &&
                tar.Value.Interruptible &&
                InterrupterContainer.Spells.ContainsKey(tar.Value.SpellID) &&
                InterrupterContainer.Spells[tar.Value.SpellID].Include &&
                InterrupterContainer.Spells[tar.Value.SpellID].TagGroupIDs.Any(tag =>
                    InterrupterContainer.Tags.ContainsKey(tag) && InterrupterContainer.Tags[tag].Include) &&
                !InterrupterContainer.Spells[tar.Value.SpellID].TagGroupIDs.Any(tag =>
                InterrupterContainer.Tags.ContainsKey(tag) && InterrupterContainer.Tags[tag].EntirelyExclude)
                )
                unit = tar.Value.Unit;
        }
                    
        return unit;
    }
}

it's throwing
Code:
[1:08:29 AM:879] System.NullReferenceException: Object reference not set to an instance of an object.
   at InterrupterUltimate.InterrupterUltimate.get_GetClosestValidCastingTarget() in e:\A\Honorbuddy\Plugins\Interrupter Ultimate\InterrupterUltimate.Helpers.cs:line 163
   at InterrupterUltimate.InterrupterUltimate.ChooseSpell() in e:\A\Honorbuddy\Plugins\Interrupter Ultimate\InterrupterUltimate.Spells.cs:line 12
   at InterrupterUltimate.InterrupterUltimate.Pulse() in e:\A\Honorbuddy\Plugins\Interrupter Ultimate\InterrupterUltimate.cs:line 56
   at Styx.Plugins.PluginWrapper.Pulse()
Line 163 being " if (tar.Value.UnitExists && "

SpellTracker.Targets is dictionary, all variables have assigned values inside constructor, tried also assigning them in declaration and some other things, can't seem to find the right one.
Any ideas?

Firstly, the null reference can be anywhere in that entire cluttered if statement.

What exactly are you attempting to do?

Using something like Singular's "NearbyUnfriendlyUnits" collection you can do something like the following:

Code:
private WoWUnit GetClosestValidCastingTarget { get { return Unit.NearbyUnfriendlyUnits.FirstOrDefault(u=> u.IsCasting && u.CanInterruptCurrentSpellCast); } }

If you're trying to run some update dictionary crap in a property, you're definitely using properties wrong.
 
I still don't know what was wrong there, but i managed to get it working like this (InterruptAll is just addition, working without it):
PHP:
private WoWUnit GetSpellcastingUnit
{
    get
    {
        var result = (from UnitSpellcastingInfo sc in SpellTracker.Targets.Values.Where(v =>
                v.UnitExists &&
                v.Unit != null &&
                v.Included && v.IsUnitEnemy &&
                ((v.IsCasting && v.msCastTimeLeft < Settings.CastMillisecondsLeft) ||
                    (v.IsChannelling && v.msCastTimeElapsed > Settings.ChannelMillisecondsElapsed)) &&
                v.Interruptible &&
                (Settings.InterruptAll ||
                    (InterrupterContainer.Spells.ContainsKey(v.SpellID) &&
                    InterrupterContainer.Spells[v.SpellID].Include &&
                    InterrupterContainer.Spells[v.SpellID].TagGroupIDs.Any(tag =>
                        InterrupterContainer.Tags.ContainsKey(tag) && InterrupterContainer.Tags[tag].Include) &&
                    !InterrupterContainer.Spells[v.SpellID].TagGroupIDs.Any(tag =>
                    InterrupterContainer.Tags.ContainsKey(tag) && InterrupterContainer.Tags[tag].EntirelyExclude)))
                )
            where sc.Unit.IsValid
            orderby sc.Unit.DistanceSqr ascending
            select sc.Unit);

        return result.Count() > 0 ? result.FirstOrDefault() : null;
    }
}
Above if unit is casting i need to crosscheck with my own databases (Spells and Tag Groups, i decided to use dictionary there) including time elapsed/left and if it's channel or cast.


Doesn't matter how i tried to achieve that, simple WoWUnit.IsCasting is not working when you want to check targets around you and i really spent couple days trying different ways to get it working properly.
I ended up writing my own spellcast tracker, based on Lua responses and WoW's UnitIDs, which is updated for every enabled UnitID in each pulse.
 
Last edited:
I still don't know what was wrong there, but i managed to get it working like this (InterruptAll is just addition, working without it):
PHP:
private WoWUnit GetSpellcastingUnit
{
    get
    {
        var result = (from UnitSpellcastingInfo sc in SpellTracker.Targets.Values.Where(v =>
                v.UnitExists &&
                v.Unit != null &&
                v.Included && v.IsUnitEnemy &&
                ((v.IsCasting && v.msCastTimeLeft < Settings.CastMillisecondsLeft) ||
                    (v.IsChannelling && v.msCastTimeElapsed > Settings.ChannelMillisecondsElapsed)) &&
                v.Interruptible &&
                (Settings.InterruptAll ||
                    (InterrupterContainer.Spells.ContainsKey(v.SpellID) &&
                    InterrupterContainer.Spells[v.SpellID].Include &&
                    InterrupterContainer.Spells[v.SpellID].TagGroupIDs.Any(tag =>
                        InterrupterContainer.Tags.ContainsKey(tag) && InterrupterContainer.Tags[tag].Include) &&
                    !InterrupterContainer.Spells[v.SpellID].TagGroupIDs.Any(tag =>
                    InterrupterContainer.Tags.ContainsKey(tag) && InterrupterContainer.Tags[tag].EntirelyExclude)))
                )
            where sc.Unit.IsValid
            orderby sc.Unit.DistanceSqr ascending
            select sc.Unit);

        return result.Count() > 0 ? result.FirstOrDefault() : null;
    }
}
Above if unit is casting i need to crosscheck with my own databases (Spells and Tag Groups, i decided to use dictionary there) including time elapsed/left and if it's channel or cast.


Doesn't matter how i tried to achieve that, simple WoWUnit.IsCasting is not working when you want to check targets around you and i really spent couple days trying different ways to get it working properly.
I ended up writing my own spellcast tracker, based on Lua responses and WoW's UnitIDs, which is updated for every enabled UnitID in each pulse.

The code I pasted works great. I use it for interrupts in a few rotations in Singular. (Though, I did forget the .OrderBy(u=>u.Distance) linq statement)
 
Back
Top