|
You last visited: Today at 23:31
Advertisement
One null reference down one still to go :/
Discussion on One null reference down one still to go :/ within the CO2 Private Server forum part of the Conquer Online 2 category.
03/22/2015, 20:59
|
#1
|
elite*gold: 0
Join Date: Aug 2010
Posts: 940
Received Thanks: 76
|
One null reference down one still to go :/
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
|
#2
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,376
|
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 ***... 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 ****. 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 ***** up in your function. Boom you're done.
|
|
|
03/22/2015, 22:17
|
#3
|
elite*gold: 0
Join Date: Aug 2010
Posts: 940
Received Thanks: 76
|
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
|
#4
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,376
|
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
|
#5
|
elite*gold: 0
Join Date: Aug 2010
Posts: 940
Received Thanks: 76
|
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
|
#6
|
elite*gold: 130
Join Date: Oct 2007
Posts: 1,652
Received Thanks: 701
|
Quote:
Originally Posted by pro4never
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
|
#7
|
elite*gold: 0
Join Date: Aug 2010
Posts: 940
Received Thanks: 76
|
Sorry Turk it's NOT my code *****.
|
|
|
03/25/2015, 14:10
|
#8
|
elite*gold: 0
Join Date: Oct 2009
Posts: 768
Received Thanks: 550
|
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.
|
|
|
|
Similar Threads
|
Will das Spiel starten und dann kommt NULL NULL
12/08/2011 - Kal Online - 2 Replies
Also wie im titel schon gesagt ist habe ich wenn ich das spiel starten will ein kleines fenster mit zwei "NULL NULL" buttons kann mir jemand helfen wie ich jetzt spielen und das problem lösen kann. wäre cool wenn schnell eine antwort kommt
danke, gruß:D:D
|
Looking for a reference
11/08/2011 - Dekaron Private Server - 8 Replies
We are sorry but we just got this letter and we comply with the msg
"
To Elitepvpers
The account Cataracts - Daemon Strother has been reported for multiple crimes by the US state department of illegal Child sexual abuse!
law § 2243 - Sexual abuse of a minor or ward:
(a) Of a Minor.— Whoever, in the special maritime and territorial jurisdiction of the United States or in a Federal prison, or in any prison, institution, or facility in which persons are held in custody by direction of or...
|
Weird null reference exception.
07/18/2011 - CO2 Private Server - 5 Replies
#Edit I know why it happens and it's fixed.
|
How to kill Dark Colluseast: Null and Steelation: Null (TT 3-1)?
06/10/2011 - PW Hacks, Bots, Cheats, Exploits - 1 Replies
Can anyone tell me How to kill Dark Colluseast: Null and Steelation: Null (TT 3-1)?
:handsdown:
After patch 121 or mermaid update, the boss, Dark Colluseast: Null and Steelation: Null, has a random aggro, area stun, and area banish.
:confused:
|
All times are GMT +2. The time now is 23:31.
|
|