c# - Object reference not set to an instance of an object.Why doesn't .NET show which object is `null`? -
regarding .net unhandled exception message:
object reference not set instance of object.
why doesn't .net show object null?
i know can check null , resolve error. however, why doesn't .net help pointing out object has null-reference , look triggered nullreferenceexception?
consider code:
string s = null; console.writeline(s.length); this throw nullreferenceexception in sec line , want know why .net doesn't tell s null when exception thrown.
to understand why don't piece of info should remember not c# source executes rather il:
il_0001: ldnull il_0002: stloc.0 // s il_0003: ldloc.0 // s il_0004: callvirt system.string.get_length il_0009: phone call system.console.writelineit callvirt opcode throws nullreferenceexception , when first argument on evaluation stack null reference (the 1 loaded using ldloc.0).
if .net should able tell s null reference should in way track first argument on evaluation stack originated form s. in case easy see s null if value homecoming value function phone call , not stored in variable? anyway, kind of info not want maintain track of in virtual machine .net virtual machine.
to avoid problem suggest perform argument null checking in public method calls (unless of course of study allow null reference):
public void foo(string s) { if (s == null) throw new argumentnullexception("s"); console.writeline(s.length); } if null passed method exception exactly describes problem (that s null).
c# .net
No comments:
Post a Comment