List Exception: Object Null reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Samishii23
    New Member
    • Sep 2009
    • 246

    List Exception: Object Null reference

    I'm having a issue with my List<string> variables... When I run the following code... Errors out on the ***** line of code... I commented out the Try block to have the editor show me the line of code that was giving me the runtime error, as the try/catch wasn't telling me enough information.

    Code:
    public void Load_ClassData(string c) {
    	// Global Data Class init
    	CGlobalData gd = new CGlobalData();
    	gd.Talent = new List<string>();
    	gd.Discription = new List<string>();
    	// To Load new Data. Must reset old data
    	// URI = InstallFolder / Version Folder / Using Version / Class Name / .xml
    	// Later will be replaced with GetClassVersion()
    	string uri = _settings[18] + "/wowv/" + "[3.2.2]" + c + ".xml";
    	//try  {
    		XDocument doc = XDocument.Load(uri);
    		//gd.TreeBG = doc.Root.Descendants().Where(x => x.Name == "Class").First().Attribute("bg").Value;
    		//gd.TreeIco = doc.Root.Descendants().Where(x => x.Name == "Class").First().Attribute("treeico").Value;
    		var qTalent = from x in doc.Root.Descendants()
    			where x.Name == "Talent"
    			select x;
    		var qDisc = from z in doc.Root.Descendants()
    			where z.Name == "disc"
    			select z;
    
    		foreach (XElement x in qTalent) {
    			// <Talent> Parsing
    			if (Int32.Parse(x.Attribute("pts").Value) > 0) {
    				// If <Talent> Has another talent requirement attribute
    				// Data = ID / Points / Title / Icon / Requirement (if has one)
    				if (x.LastAttribute.Name == "req") { gd.Talent.Add(x.Attribute("id").Value + "," + x.Attribute("pts").Value + "," + x.Attribute("title").Value + "," + x.Attribute("icon").Value + "," + x.Attribute("req").Value); }
    				else { gd.Talent.Add(x.Attribute("id").Value + "," + x.Attribute("pts").Value + "," + x.Attribute("title").Value + "," + x.Attribute("icon").Value); }
    				// Write descripition
    				// If Talent Points is over 1
    				// Data = Replace String / Description paragraph
    				***** if (Int32.Parse(x.Attribute("pts").Value) > 1) { gd.Discription.Add(x.Descendants().First().Attribute("replace").Value + "]|[" + x.Descendants().First().Value); }
    				else { gd.Discription.Add(x.Descendants().First().Value); }
    			}
    			else
    			{
    				// If Talent is "Empty", has 0 points, no description
    				gd.Talent.Add(x.Attribute("id").Value + "," + x.Attribute("pts").Value);
    				gd.Discription.Add("none");
    			}
    		}
    	//} catch (Exception EX) { MessageBox.Show("Error: " + EX.Message); }
    }
    
    public class CGlobalData {
    	// Current Class
    	public int SelectedClass { get; set; }
    	// This holds the Tree BG data
    	public string TreeBG { get; set; }
    	// This holds Tree Icon data
    	public string TreeIco { get; set; }
    	// This holds the actual data for each Talent: (id,title,icon,req*)
    	public List<string> Talent;
    	// This holds the Talent discription: [Replace Strings]
    	public List<string> Discription;
    }
    Object reference not set to an instance of an object.

    Any ideas?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by OriginalPoster
    "How do I fix a 'object reference not set to an instance of an object' error?
    The line your code stopped on while debugging holds all your answers.
    One of the variables/objects was created but not initialized. For example:
    Code:
    string TempString;// Created but not initialized so this is still null
    //versus
    string TempString = string.empty;
    Debug your project again. This time, when it breaks on a line look at the Locals pallet to see which variable/object is null. You can also hover your mouse over each variable and the hothelp will shows its value. One of them will be null.

    Comment

    • Samishii23
      New Member
      • Sep 2009
      • 246

      #3
      I looked at the Locals list while debugging.

      I added
      Code:
      gd.Discription = new List<string>();
      gd.Talent = new List<string>();
      gd.Discription.Clear();
      gd.Talent.Clear();
      just below the gd Init. The .Clear()'s don't error out, though they did before I added the Inits for the lists.

      In the de-bug with the error on screen, these are the values:
      Code:
      Discription	Count = 0x00000039	System.Collections.Generic.List<string>
      Talent	Count = 0x0000003a	System.Collections.Generic.List<string>
      When I open of the + for them, each list has 0x39h items in the list... And its still talking about Null reference... Aw crap... That means the Linq variables has a null... Ugh


      Edit: Thanks for telling me about the Local variables list thing. It helped me solve the problem.

      Comment

      Working...