One null reference down one still to go :/

03/22/2015 20:59 denominator#1
Code:
private static void Durability(Entity attacker, Entity attacked, Database.SpellInformation spell)
        {//duranano
            if (spell != null)
                if (!spell.CanKill)
                    return;
            #region Attack
            
            if (attacker != null)
                if (attacker.EntityFlag == EntityFlag.Player)
                {
                    for (byte i = 4; i <= 6; i++)
                    {
                        if (attacker.Owner.Equipment.Free(i))
                        {
                            var item = attacker.Owner.Equipment.TryGetItem(i);
                            i = 0; <<<---Added to get rid of the other null error
                            if (i == 5)
                            {
                                {
                                    if (Network.PacketHandler.IsArrow(item.ID))
                                    {
                                        continue;
                                    }
                                }
                                if (Kernel.Rate(30, 100))
                                {
                                    {
                                        if (item.Durability != 0)
                                        {
                                            item.Durability -= 5;
                                            Database.ConquerItemTable.UpdateDurabilityItem(item);
                                            item.Mode = Enums.ItemMode.Update;
                                            item.Send(attacker.Owner);
                                            item.Mode = Enums.ItemMode.Default;
                                        }
                                    }
                                }
                            }
                            if (i == 6)
                                break;
                        }
                        if (attacker.Owner.Equipment.Free(10))
                        {                            
                            var item = attacker.Owner.Equipment.TryGetItem(10);
                            
                            if (Kernel.Rate(30, 100))
                            {
                                
                                if (item.Durability != 0) <<<null reference here
                                {
                                    item.Durability -= 10;
                                    Database.ConquerItemTable.UpdateDurabilityItem(item);
                                    item.Mode = Enums.ItemMode.Update;
                                    item.Send(attacker.Owner);
                                    item.Mode = Enums.ItemMode.Default;
                                }
                            }
                        }
                    }
            #endregion
03/22/2015 21:05 pro4never#2
Why would you be performing so many calculations before checking for NREs? Obviously you shouldn't be passing junk values into the function but if you're having issues and don't mind breaking coding conventions then just check == null and return at the very start of the function.

EG:

if(skill == null || attacker == null || attacked == null || attacker.EntityFlag != EntityFlag.Player || !spell.CanKill)
return;

That way you exit the function as soon as possible if there's junk data entered.



The core issue is that you're pulling items and never checking if the item exists...


Honestly this is why functions like TryGet are so useful cause they reduce the possibility of issues like this. That being said all you have to do is check if item != null




Now - I'm not going to rant on it as long as I want to but my good god... BREAKPOINTS.

If you have an issue in a function you place a breakpoint at the start and then step through the code until you see what item is null. It would take you 30-40 seconds to step through the function and by mousing over item you'd go "ooh shit. item is null" and you could add a check.

Breakpoints are pretty much foolproof... you place them... you step through and you read what variables are set to and make sure nothing fucks up in your function. Boom you're done.
03/22/2015 22:17 denominator#3
It has if item != null; though ;)

Code:
if (attacker.Owner.Equipment.Free(10))
                        {                            
                            var item = attacker.Owner.Equipment.TryGetItem(10);
                            //item = null;
                            if (Kernel.Rate(30, 100))
                            {
                                if (item != null)

                                if (item.Durability != 0)
                                {
                                    item.Durability -= 10;
                                    Database.ConquerItemTable.UpdateDurabilityItem(item);
                                    item.Mode = Enums.ItemMode.Update;
                                    item.Send(attacker.Owner);
                                    item.Mode = Enums.ItemMode.Default;
                                }
                            }
                        }
                    }
            #endregion
03/22/2015 23:02 pro4never#4
if (attacker.Owner.Equipment.Free(10))
{
var item = attacker.Owner.Equipment.TryGetItem(10);

if (Kernel.Rate(30, 100))
{

if (item.Durability != 0) <<<null reference here
{
item.Durability -= 10;
Database.ConquerItemTable.UpdateDurabilityItem(ite m);
item.Mode = Enums.ItemMode.Update;
item.Send(attacker.Owner);
item.Mode = Enums.ItemMode.Default;
}
}
}


You said that's where the exception is... and yet you never check if item is null right before using the item.

It could be the item is null or some component of the item is null (plus information, base item information, etc) which is why you HAVE to inspect the element rather than just breakpointing and not actually looking at the info it gives you.
03/22/2015 23:23 denominator#5
I used your example and just changed skill to item, seems to have sorted it out, I did try that but didn't have a return so it's probably why the error kept happening but yes it's sorted now. Thank you :)
03/23/2015 15:39 turk55#6
Quote:
Originally Posted by pro4never View Post
if (attacker.Owner.Equipment.Free(10))
{
var item = attacker.Owner.Equipment.TryGetItem(10);

if (Kernel.Rate(30, 100))
{

if (item.Durability != 0) <<<null reference here
{
item.Durability -= 10;
Database.ConquerItemTable.UpdateDurabilityItem(ite m);
item.Mode = Enums.ItemMode.Update;
item.Send(attacker.Owner);
item.Mode = Enums.ItemMode.Default;
}
}
}


You said that's where the exception is... and yet you never check if item is null right before using the item.

It could be the item is null or some component of the item is null (plus information, base item information, etc) which is why you HAVE to inspect the element rather than just breakpointing and not actually looking at the info it gives you.
He has the NULL check this time, you probably didn't see it because of the ugly way he wrote it.

Edit: Nvm.. He didn't have it in the OP.
03/23/2015 18:53 denominator#7
Sorry Turk it's NOT my code lmfao.
03/25/2015 14:10 -impulse-#8
if (!attacker.Owner.Equipment.Free(i))

Because if a spot is free in the equipment then you get a null item. Add the extra '!' for those checks and you are good to go.