|
||
|
|||||||
| Script Support Get support for modifying RunUO Scripts, or writing your own! |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Account Terminated
Join Date: Jan 2007
Age: 94
Posts: 5
|
If you don't check for null it usually results in a server crash if you refer the the object that is null. So would a check like this be safe to do?
if ( talisman.Killer != null && talisman.Killer.Type != null ) Or should I nest this instead? This way is just cleaner, but I'm wondering if it will still check .killer.type even if the first one is null. |
|
|
|
|
|
#2 (permalink) |
|
Forum Expert
Join Date: Nov 2003
Location: Illinois, USA
Age: 22
Posts: 2,911
|
Yeah, checking if something is null like that wont cause any problems.
Edit: Correction: It only checks the second one if the first is false. See post below Last edited by mordero; 01-02-2007 at 05:46 AM. |
|
|
|
|
|
#5 (permalink) |
|
Forum Master
|
you do need to check them seperate - i found this out the hard way
because of the 1st is null, then the type can not be found and will cause the crash read my thread on the prospectors tool - that happened there - because the def was null, even checking for def.what ever being nul would crash
__________________
http://www.AoAUO.com
:) ..... Come for the Customs, Play for the Fun. Return to see your new Friends ..... :) |
|
|
|
|
|
#6 (permalink) | |
|
Forum Expert
|
Quote:
Code:
if ( m != null && m.Mana >= 50) while Code:
if ( from.Hits >= 50 && from.Mana >= 50)
__________________
Procrastinators unite!
Tomorrow. Saying that Java is nice because it works on all OS's is like saying that anal sex is nice because it works on all genders. ![]() |
|
|
|
|
|
|
#8 (permalink) |
|
Forum Expert
|
So then how do you figure the crash as stated by greywolf, if it doesn't reach it?
__________________
Procrastinators unite!
Tomorrow. Saying that Java is nice because it works on all OS's is like saying that anal sex is nice because it works on all genders. ![]() |
|
|
|
|
|
#9 (permalink) |
|
Forum Expert
Join Date: Nov 2003
Location: Illinois, USA
Age: 22
Posts: 2,911
|
It only checks the second operand if the first one is false...
http://msdn2.microsoft.com/en-us/library/2a723cdk.aspx Edit: Whoops forget to say what I wanted to. If you get an error because it is being null but still checking the second, you need to do a var != null on the first one (like movezig does), because then if it is null, it will return false, and thus wont check the second one. Last edited by mordero; 01-02-2007 at 05:52 AM. |
|
|
|
|
|
#10 (permalink) |
|
Forum Expert
|
It works exactly as it should. The crash greywolf reffered to was caused by his wrong logic.
__________________
Angels are falling the very last time, down they're burning in hate and decline, unfaithful and violent we're breaking the spell, we're god, we're scissor, in heaven and hell! |
|
|
|
|
|
#11 (permalink) |
|
Forum Master
|
i would seperate them if they are a sub of the other one
if they are not related at all - then would be no problem (i,e, m.hits and m.mana is no problem - not a sub but if m != nul and m.mana i would) because i had this crash from doing just that: if ( root is PlayerMobile && ((PlayerMobile)root).ClassLevel <= 4) gave a crash if it was not a player mobile - so even though 1st half was false - still checked the 2nd - giving the crash
__________________
http://www.AoAUO.com
:) ..... Come for the Customs, Play for the Fun. Return to see your new Friends ..... :) |
|
|
|
|
|
#12 (permalink) |
|
Forum Expert
|
Code:
if ( root is PlayerMobile && ((PlayerMobile)root).ClassLevel <= 4)
__________________
Angels are falling the very last time, down they're burning in hate and decline, unfaithful and violent we're breaking the spell, we're god, we're scissor, in heaven and hell! |
|
|
|
|
|
#13 (permalink) | |
|
RunUO Forum Moderator
|
Quote:
As for the rest of the argument: Code:
if ( m != null && m.Mana >= 50)
__________________
I always try to help
![]() Sometimes, I don't know how.... ![]() My Web Page Forum Rules ------------------------------------------------------------- Extensive OWLTR System | Token System | World Teleporters ------------------------------------------------------------- |
|
|
|
|
|
|
#14 (permalink) |
|
Forum Expert
|
Nope, the 'is' operator at first checks if the operand is null. If so, the expression is evaluated as false.
__________________
Angels are falling the very last time, down they're burning in hate and decline, unfaithful and violent we're breaking the spell, we're god, we're scissor, in heaven and hell! |
|
|
|
|
|
#15 (permalink) |
|
Forum Master
|
mine crashed because the root was a packhorse and not a player mobile - and packhorses do not have class levels
i checked this out with statements being used before and after it to find the exact line causing the crash the error was a null exception -- i do not have it saved to show exact wordage i changed it to 2 lines (a sub if of the 1st) and then no more crash
__________________
http://www.AoAUO.com
:) ..... Come for the Customs, Play for the Fun. Return to see your new Friends ..... :) |
|
|
|
|
|
#16 (permalink) | |
|
RunUO Forum Moderator
|
Quote:
Regardless my earlier statements is still valid (and if someone disagree than show proof!!! you won't find it...): Code:
if ( m != null && m.Mana >= 50)
__________________
I always try to help
![]() Sometimes, I don't know how.... ![]() My Web Page Forum Rules ------------------------------------------------------------- Extensive OWLTR System | Token System | World Teleporters ------------------------------------------------------------- Last edited by daat99; 01-03-2007 at 03:34 AM. |
|
|
|
|
|
|
#17 (permalink) |
|
RunUO Forum Moderator
|
Here's some testing code I made up to test your problem.
Just create a new console application project in visual studio 2003/5 (maybe in sharp develop also) and paste this code as it is on your main file: Code:
using System;
//conclusion -> no such null
namespace testingNull
{
class A { } //object that doesn't have "mana" property
class B //object that have mana property
{
public B(int classLevel) { this.classLevel = classLevel; }
private int classLevel;
public int ClassLevel { get { return classLevel; } set { classLevel = value; } }
}
class Program
{
static void Main(string[] args)
{
A a = new A(); //doesn't have ClassLevel
B b1 = new B(3); //have lower ClassLevel
B b2 = new B(5); //have higher ClassLevel
check(a); //object that doesn't have "class" property
check(b1); //object that have lower class
check(b2); //object that have higher class
check(null); //"null object"
check2(b1); //b that have lower class
check2(b2); //b that have higher class
check2(null); //"null b"
Console.ReadLine();
}
private static void check( Object o )
{
if (o is B && ((B)o).ClassLevel >= 4) //object that have higher class
Console.WriteLine("b and class higher than 4");
else if (o is B) //object that have lower class
Console.WriteLine("b and class lower than 4");
else if (o is A) //object that doesn't have "class" property
Console.WriteLine("not b - doesn't have class");
else //"null object"
Console.WriteLine("null");
}
private static void check2(B b)
{
if ( b != null && b.ClassLevel >= 4 ) //b have higher class
Console.WriteLine("not null and higher class");
else if ( b != null ) //b have lower class
Console.WriteLine("not null and lower class");
else //"null b"
Console.WriteLine("null");
}
}
}
Dictionary: root -> object PlayerMobile -> B Horse -> A ClassLevel -> ClassLevel Please note that nothing crashes regardless if the object have the ClassLevel property or it's null... If someone disagree than show code that prove your point.
__________________
I always try to help
![]() Sometimes, I don't know how.... ![]() My Web Page Forum Rules ------------------------------------------------------------- Extensive OWLTR System | Token System | World Teleporters ------------------------------------------------------------- |
|
|
|
|
|
#18 (permalink) |
|
Forum Master
|
try a check2 on a - you did it with b's - which will be b's no matter what
and a defined null - defined as being nulls to start with, the system might treat deferently it may also be something that runuo added in someplace along the line also because i know that line caused the crash, replaced and no more crash
__________________
http://www.AoAUO.com
:) ..... Come for the Customs, Play for the Fun. Return to see your new Friends ..... :) |
|
|
|
|
|
#19 (permalink) | |
|
RunUO Forum Moderator
|
Quote:
Before you try to drive a car you need to make sure you're in a car, not a ship... What you did was getting into a ship and trying to drive it on the highway... makes no sense... In any case this will not cause a null reference exception but some kind of "invalid cast exception" or "argument exception" (don't recall the name of that particular exception atm). RunUO doesn't have its own scripting language, it uses c# itself. **edit** By all means, open visual studio, create a new "console application" project, copy the code I posted over the single file in there and test it.
__________________
I always try to help
![]() Sometimes, I don't know how.... ![]() My Web Page Forum Rules ------------------------------------------------------------- Extensive OWLTR System | Token System | World Teleporters ------------------------------------------------------------- Last edited by daat99; 01-03-2007 at 05:39 PM. |
|
|
|
|
|
|
#20 (permalink) |
|
Forum Master
|
it will not throw a null exception then, but can throw a crash still - because of diffefernt types - i dug the report out of my recycle bin
so even if the 1st part is invalde - it will still check the 2nd half and throw the error/crash Code:
System.InvalidCastException: Unable to cast object of type 'Server.Mobiles.PackLlama' to type 'Server.Mobiles.PlayerMobile'. origional line that threw it: Code:
if ( root is PlayerMobile && ((PlayerMobile)root).ClassLevel <= 4)
__________________
http://www.AoAUO.com
:) ..... Come for the Customs, Play for the Fun. Return to see your new Friends ..... :) |
|
|
|
|
|
#21 (permalink) |
|
Forum Expert
|
Then you probably used a wrong operator. Replacing '&&' with only '&' is a common mistake.
__________________
Angels are falling the very last time, down they're burning in hate and decline, unfaithful and violent we're breaking the spell, we're god, we're scissor, in heaven and hell! |
|
|
|
|
|
#22 (permalink) | |
|
Moderate
Join Date: Nov 2002
Location: USA
Posts: 6,598
|
Quote:
![]()
__________________
David Forum Moderator The RunUO.com Forum Moderator Team Forum Rules and Guidelines RunUO Forum Search Engine Download RunUO 2.0 RC2 |
|
|
|
|
|
|
#23 (permalink) | |
|
Forum Master
|
Quote:
did you read the line that threw the exception - it is an && there and not just an & - it would not eeven compile if i used just an & there it may not check the 2nd half on a null check, but it does when it is not a null check
__________________
http://www.AoAUO.com
:) ..... Come for the Customs, Play for the Fun. Return to see your new Friends ..... :) |
|