473,320 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

List Exception: Object Null reference

Samishii23
246 100+
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.

Expand|Select|Wrap|Line Numbers
  1. public void Load_ClassData(string c) {
  2.     // Global Data Class init
  3.     CGlobalData gd = new CGlobalData();
  4.     gd.Talent = new List<string>();
  5.     gd.Discription = new List<string>();
  6.     // To Load new Data. Must reset old data
  7.     // URI = InstallFolder / Version Folder / Using Version / Class Name / .xml
  8.     // Later will be replaced with GetClassVersion()
  9.     string uri = _settings[18] + "/wowv/" + "[3.2.2]" + c + ".xml";
  10.     //try  {
  11.         XDocument doc = XDocument.Load(uri);
  12.         //gd.TreeBG = doc.Root.Descendants().Where(x => x.Name == "Class").First().Attribute("bg").Value;
  13.         //gd.TreeIco = doc.Root.Descendants().Where(x => x.Name == "Class").First().Attribute("treeico").Value;
  14.         var qTalent = from x in doc.Root.Descendants()
  15.             where x.Name == "Talent"
  16.             select x;
  17.         var qDisc = from z in doc.Root.Descendants()
  18.             where z.Name == "disc"
  19.             select z;
  20.  
  21.         foreach (XElement x in qTalent) {
  22.             // <Talent> Parsing
  23.             if (Int32.Parse(x.Attribute("pts").Value) > 0) {
  24.                 // If <Talent> Has another talent requirement attribute
  25.                 // Data = ID / Points / Title / Icon / Requirement (if has one)
  26.                 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); }
  27.                 else { gd.Talent.Add(x.Attribute("id").Value + "," + x.Attribute("pts").Value + "," + x.Attribute("title").Value + "," + x.Attribute("icon").Value); }
  28.                 // Write descripition
  29.                 // If Talent Points is over 1
  30.                 // Data = Replace String / Description paragraph
  31.                 ***** if (Int32.Parse(x.Attribute("pts").Value) > 1) { gd.Discription.Add(x.Descendants().First().Attribute("replace").Value + "]|[" + x.Descendants().First().Value); }
  32.                 else { gd.Discription.Add(x.Descendants().First().Value); }
  33.             }
  34.             else
  35.             {
  36.                 // If Talent is "Empty", has 0 points, no description
  37.                 gd.Talent.Add(x.Attribute("id").Value + "," + x.Attribute("pts").Value);
  38.                 gd.Discription.Add("none");
  39.             }
  40.         }
  41.     //} catch (Exception EX) { MessageBox.Show("Error: " + EX.Message); }
  42. }
  43.  
  44. public class CGlobalData {
  45.     // Current Class
  46.     public int SelectedClass { get; set; }
  47.     // This holds the Tree BG data
  48.     public string TreeBG { get; set; }
  49.     // This holds Tree Icon data
  50.     public string TreeIco { get; set; }
  51.     // This holds the actual data for each Talent: (id,title,icon,req*)
  52.     public List<string> Talent;
  53.     // This holds the Talent discription: [Replace Strings]
  54.     public List<string> Discription;
  55. }
Object reference not set to an instance of an object.

Any ideas?
Feb 13 '10 #1

✓ answered by tlhintoq

"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:
Expand|Select|Wrap|Line Numbers
  1. string TempString;// Created but not initialized so this is still null
  2. //versus
  3. 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.

2 4874
tlhintoq
3,525 Expert 2GB
"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:
Expand|Select|Wrap|Line Numbers
  1. string TempString;// Created but not initialized so this is still null
  2. //versus
  3. 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.
Feb 13 '10 #2
Samishii23
246 100+
I looked at the Locals list while debugging.

I added
Expand|Select|Wrap|Line Numbers
  1. gd.Discription = new List<string>();
  2. gd.Talent = new List<string>();
  3. gd.Discription.Clear();
  4. 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:
Expand|Select|Wrap|Line Numbers
  1. Discription    Count = 0x00000039    System.Collections.Generic.List<string>
  2. 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.
Feb 13 '10 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Hunter | last post by:
I am writing a python extension module and have a reference counting question My function looks like static PyObject * pokereval_seven_cards(PyObject *self, PyObject *args) { int i;
15
by: Wolfram Humann | last post by:
Hi, please don't be too harsh if I made stupid errors creating this simple example from my more complex case. Suppose I have a class like this: class BOOK { const string title;
5
by: Vitling | last post by:
For no apparent reason, a NullReference exception is thrown in system.dll (System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback). Since I only get a disassembly from Visual Studio, it...
3
by: N8 | last post by:
I am trying to get an exception to occur and consequently found that when adding a target method to a delegates invocation list, a copy of that object is added instead of a reference to the object....
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
3
by: BobforeApples | last post by:
Hi All, I was trying to apply databinding to a custom collection as in the MSDN Magazine article found in the August, 2005 issue. After converting the code in the article to C# and applying it to...
7
by: Chuck Hartman | last post by:
I have a Windows service that requests web pages from a site using an HttpWebRequest object. When I try to request a page from an ASP.NET 2 site, I get a WebException with message "The remote...
3
by: Ganesh Palaniappan | last post by:
We're getting following exception for the below piece of code. We're wondering how it is possible since we're having a null check for objGraphics and strokePen... Exception:...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.