Author Topic: Null Reference Exceptions: Why & How to Prevent  (Read 2750 times)

Archon

  • UO Central - Owner
  • Administrator
  • Full Member
  • *****
  • Posts: 122
    • View Profile
    • UO Central
Null Reference Exceptions: Why & How to Prevent
« on: June 23, 2014, 09:58:45 PM »
Tutorial by Vorspire

Null Reference Exceptions: Why & How to Prevent

Difficulty: 2/10 (Requires basic understanding of key-words, data-types and variables)
This tutorial will teach you how to distinguish and fix potential Null Reference Exceptions.

A NullReferenceException is thrown when the code attempts to access a non-static member (method, property) of an object that has no value set.  This only pertains to Nullable Types, variables whose' Type supports being 'null' (empty, undefined, not set)
 
Structs and simple data-types are exempt;
 
UO Central - Owner

uoevolution

  • Newbie
  • *
  • Posts: 7
  • UO Evolution - www.uoevolution.com
    • View Profile
    • The Game has Evolved...UO Evolution
Re: Null Reference Exceptions: Why & How to Prevent
« Reply #1 on: June 24, 2014, 09:28:15 PM »
I was wondering if you could explain how to track down this stackoverflow exception? It doesnt explain the error, it just locks up the server. The shard has been running fine, then today it started hitting this error on the save

I can now get the shard back up but it hits the error on the first save.
 
I need help with the debug, the visual studio debug led me to this folder, mscorlib.dll...no idea what to do with this info?
 
I also looked up the world save error log, not sure what to do with this situation? Here are the warnings in the log:
 6/23/2014 5:53:43 AM Warning: Attempted to delete 0x4080FB4C "TownHouse" during world save.
 This action could cause inconsistent state.
 It is strongly advised that the offending scripts be corrected.
 at Knives.TownHouses.TownHouseSign.PackUpHouse()
 at Knives.TownHouses.RentalContract.Delete()
 at Knives.TownHouses.General.OnSave(WorldSaveEventArgs e)
 at Server.WorldSaveEventHandler.Invoke(WorldSaveEventArgs e)
 at Server.World.Save(Boolean message, Boolean permitBackgroundWrite)
 at Server.Timer.Slice()
 at Server.Core.Main(String[] args)
 6/23/2014 6:58:02 AM Warning: Attempted to add 0x4026AE88 "Bag" during world save.
 This action could cause inconsistent state.
 It is strongly advised that the offending scripts be corrected.
 at Server.Item..ctor()
 at Server.Items.Container..ctor(Int32 itemID)
 at Server.Items.Bag..ctor()
 at Knives.TownHouses.TownHouseSign.PackUpItems()
 at Knives.TownHouses.TownHouseSign.PackUpHouse()
 at Knives.TownHouses.RentalContract.Delete()
 at Knives.TownHouses.General.OnSave(WorldSaveEventArgs e)
 at Server.WorldSaveEventHandler.Invoke(WorldSaveEventArgs e)
 
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
 
I keep running into this error on the save, System.StackOverflowException. I have tried to run in debug- mode but the shard does not crash, so no report.
 
 I ran the core in debug mode with visual studio and this is the info it provided.
 Problem signature:
 Problem Event Name: CLR20r3
 Problem Signature 01: core.exe
 Problem Signature 02: 2.1.4448.41334
 Problem Signature 03: 4f56eacb
 Problem Signature 04: mscorlib
 Problem Signature 05: 4.0.0.0
 Problem Signature 06: 4e1823db
 Problem Signature 07: 137b
 Problem Signature 08: 0
 Problem Signature 09: System.StackOverflowException
 OS Version: 6.1.7600.2.0.0.1296.17
 Locale ID: 1033
 Additional Information 1: bf96
 Additional Information 2: bf9635f5e5db9d5696946bebfe545c05
 Additional Information 3: 336a
 Additional Information 4: 336a8691fb855fcde36517c4bc846d9b
 
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
 Value cannot be null.
 Parameter name: key
 Arguementnullexception was unhandled by user code
 
Is this something you understand and can help with? I am currently able to force a save, but we lock up with the stack overflow exception. I dont have an uncorrupted save. Is there a way to recover or solve the overflow issue and then save normally again?
 
Any help or insight would be appreciated
 
Thanks
« Last Edit: June 24, 2014, 09:32:46 PM by uoevolution »
The Game has Evolved...UO Evolution - www.uoevolution.com

Archon

  • UO Central - Owner
  • Administrator
  • Full Member
  • *****
  • Posts: 122
    • View Profile
    • UO Central
Re: Null Reference Exceptions: Why & How to Prevent
« Reply #2 on: June 25, 2014, 11:47:32 PM »
The error seems to originate in the core so your best bet is probably
using WinDBG with the SOS extension and debug the runuo process. More details:

http://blogs.msdn.com/b/spike/archive/2011/11/04/how-to-create-a-stackoverflowexception-and-how-to-figure-out-where-it-is-happening.aspx
UO Central - Owner

Archon

  • UO Central - Owner
  • Administrator
  • Full Member
  • *****
  • Posts: 122
    • View Profile
    • UO Central
Re: Null Reference Exceptions: Why & How to Prevent
« Reply #3 on: June 25, 2014, 11:58:12 PM »
StackOverflowException

This usually means that you have a recursive call in your code.
 
A recursion is simply a method that calls itself, causing the stack to

overflow and throw the StackoverFlow exception.
 
A simple example:
 
namespace SimpleDemo
 
{
 
    class Program
 
    {
 
        static void Main(string[] args)
 
        {
 
            int i = RecursiveMethod();
 
        }
 
 
 
        private static int RecursiveMethod()
 
        {
 
            return RecursiveMethod();
 
        }
 
    }
 
}
 
 
 
Here it is fairly easy to see what is calling itself and thus being

simple to fix.
 
However, sometimes it is not as clear. It can be a event that triggers

itself or an exception that is triggering a new exception of the same

kind.
 
Another simple example that demonstrates an exception that causes a new

exception which leads to recursion and failure:
 
 
 
namespace StackOverflowDemo
 
{
 
    class Program
 
    {
 
        static void Main(string[] args)
 
        {
 
            Console.WriteLine("Any key to crash");
 
            Console.ReadLine();
 
 
 
            List<Person> persons = new List<Person>();
 
            persons.Add(new Person { Age = 30, Name = "Paul Paulson" });
 
            persons.Add(new Person { Age = 10, Name = "John Johnson" });
 
            persons.Add(new Person { Age = 20, Name = "Eric Ericson" });
 
 
 
            foreach (Person p in persons)
 
            {
 
                try
 
                {
 
                    p.GetAge();
 
                    Console.WriteLine("{0} is {1} years old", p.Name,

p.Age);
 
                }
 
                catch (ToYoungException tye)
 
                {  /* Write to log or something similar */ }
 
            }
 
        }
 
    }
 
 
 
    class Person
 
    {
 
        public int Age { get; set; }
 
        public string Name { get; set; }
 
 
 
        public int GetAge()
 
        {
 
            if (this.Age < 18)
 
            {
 
                throw new ToYoungException(this);
 
            }
 
            return this.Age;
 
        }
 
    }
 
 
 
    class ToYoungException : Exception
 
    {
 
        public ToYoungException(Person p)
 
        {
 
            Console.WriteLine("{0} is too young. Minumum age is 18, {0}

is only {1}.", p.Name, p.GetAge());
 
        }
 
    }
 
}
 
 
 
Again, not supercomplicated. When checking a persons age to see if they
are under aged an exception is thrown. Possibly to send information to a
log or something similar.
 
However, when writing the output the coder has made a mistake and calls the GetAge() method again on an instance of a Person that is under age.  Which again throws the exception and then again triggers a new
exception. And you will have the stack overflow.
 
The code is not intended to be best practice or anything. It is just to demonstrate a simple scenario where it is not obvious where it goes wrong.
 
It could for example be that the classes are in difference assemblies or namespaces etc.
 
One of the problems with StackoverflowExceptions can be found in the
documentation.
 
UO Central - Owner

uoevolution

  • Newbie
  • *
  • Posts: 7
  • UO Evolution - www.uoevolution.com
    • View Profile
    • The Game has Evolved...UO Evolution
Re: Null Reference Exceptions: Why & How to Prevent
« Reply #4 on: June 26, 2014, 12:15:17 AM »
I was able to resolve the issue by using the [go 0x4080FB4C command and it jumped right to the offending location. There was a townhome sign attached to a player acct that had been deleted. I tried to delete the
sign and it caused the exception. I then transferred the sign to my gm and then he was able to delete the townhome. I was able to make an uncorrupted save and went back to normal. Tyvm for the support and
advice! 
The Game has Evolved...UO Evolution - www.uoevolution.com