4 Replies - 2936 Views - Last Post: 10 February 2013 - 08:56 PM Rate Topic: -----

#1 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Object reference not set to an instance of an object, Issue using LINQ

Posted 09 February 2013 - 07:40 AM

I am unsure what the problem is, I get this error after checking an item in the checkedlistbox:

A first chance exception of type 'System.NullReferenceException' occurred in SpaceshipList.exe
An unhandled exception of type 'System.NullReferenceException' occurred in SpaceshipList.exe
Additional information: Object reference not set to an instance of an object.
The program '[1044] SpaceshipList.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).



The full output:

'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\Bonnie\documents\visual studio 2012\Projects\SpaceshipList\SpaceshipList\bin\Debug\SpaceshipList.vshost.exe', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xaml\v4.0_4.0.0.0__b77a5c561934e089\System.Xaml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Deployment\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Deployment.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Serialization\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Serialization.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\XamlBuildTask\v4.0_4.0.0.0__31bf3856ad364e35\XamlBuildTask.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 'vshost.NotifyLoad' (0x1a80) has exited with code 0 (0x0).
The thread '<No Name>' (0xc64) has exited with code 0 (0x0).
The thread 'vshost.LoadReference' (0x14b0) has exited with code 0 (0x0).
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'c:\users\bonnie\documents\visual studio 2012\Projects\SpaceshipList\SpaceshipList\bin\Debug\SpaceshipList.exe', Symbols loaded.
'SpaceshipList.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
A first chance exception of type 'System.NullReferenceException' occurred in SpaceshipList.exe
An unhandled exception of type 'System.NullReferenceException' occurred in SpaceshipList.exe
Additional information: Object reference not set to an instance of an object.
The program '[1044] SpaceshipList.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).



I don't really understand what the problem is, the code is supposed to get data from a website which is working, partly. I am reading the information fine as you can tell in the MessageBox popups. Perhaps someone could show me what I'm doing wrong?

The code:

main.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace SpaceshipList
{
    public partial class main : Form
    {
        string[] charNameLB = new string[3];
        public main()
        {
            InitializeComponent();
        }
        
        private void CharIDTextBox_TextChanged(object sender, EventArgs e)
        {
        }

        private void APIKeyTextBox_TextChanged(object sender, EventArgs e)
        {
        }

        private void Submit_Click(object sender, EventArgs e)
        {
            CharacterNameList.Items.Clear();
            string ID = CharIDTextBox.Text;
            string API = APIKeyTextBox.Text;

            string url = "https://api.eveonline.com/account/characters.xml.aspx?keyID=" + ID + "&vCode=" + API;

            XDocument xmlDoc = Xdocument.Load(url);
            var characters = from row in xmlDoc.Descendants("row")
                             select new
                             {
                                 charName = row.Attribute("name").Value,
                                 charID = row.Attribute("characterID").Value
                             };
            int i = 0;
            foreach(var row in characters)
            {
                CharacterNameList.Items.Add(row.charName, CheckState.Unchecked);
                //characterID = row.charID;
                charNameLB[i] = row.charName;
                ++i;
            }
            i = 0;
        }

        private void CharacterNameList_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            string ID = CharIDTextBox.Text;
            string API = APIKeyTextBox.Text;
            string characterID = "";
            string url = "https://api.eveonline.com/account/characters.xml.aspx?keyID=" + ID + "&vCode=" + API;
            var checkedListBox = (CheckedListBox)sender;
            string checkedItemText = checkedListBox.Items[e.Index].ToString();
            if (e.NewValue == CheckState.Checked)
            {
                XDocument xmlDoc = Xdocument.Load(url);
                var characters = from row in xmlDoc.Descendants("row")
                                 select new
                                 {
                                     charID = row.Attribute("characterID").Value,
                                     charName = row.Attribute("name").Value
                                 };
                foreach (var row in characters)
                {
                    if (checkedItemText == row.charName)
                    {
                        characterID = row.charID;
                        MessageBox.Show(characterID);
                    }
                }
                url = "https://api.eveonline.com/char/CharacterSheet.xml.aspx?keyID=" + ID + "&vCode=" + API + "&characterID=" + characterID;
                MessageBox.Show(url);
                XDocument xmlDocProfile = Xdocument.Load(url);
                var characterProfile = from result in xmlDocProfile.Descendants("result")
                                        select new
                                        {
                                            charID = result.Element("characterID").Value,
                                            charName = result.Element("name").Value,
                                            DoB = result.Element("DoB").Value,
                                            race = result.Element("race").Value,
                                            bloodline = result.Element("bloodline").Value,
                                            ancestry = result.Element("ancestry").Value,
                                            gender = result.Element("gender").Value,
                                            corporationName = result.Element("corporationName").Value,
                                            allianceName = result.Element("allianceName").Value,
                                            cloneName = result.Element("cloneName").Value,
                                            cloneSkillPoints = result.Element("cloneSkillPoints").Value,
                                            balance = result.Element("balance").Value
                                        };
                /*var characterAttributes = from attributes in xmlDoc.Descendants("attributes")
                                 select new
                                 {
                                     intelligence = attributes.Element("intelligence").Value,
                                     memory = attributes.Element("memory").Value,
                                     charisma = attributes.Element("memory").Value,
                                     perception = attributes.Element("perception").Value,
                                     willpower = attributes.Element("willpower").Value
                                 };
                var characterSkills = from skills in xmlDoc.Descendants("rowset")
                                select new
                                {
                                    skillID = skills.Attribute("typeID").Value,
                                    skillLVL = skills.Attribute("level").Value
                                };*/
                foreach (var profile in characterProfile)
                {
                    DescriptionBox.Text += "Character ID: " + profile.charID;
                    DescriptionBox.Text += "\nCharacter Name: " + profile.charName;
                }
                /*foreach (var skill in characterSkills)
                {
                    switch (skill.skillID)
                    {
                        case "3300":
                        {
                            DescriptionBox.Text = "Gunnery: " + skill.skillLVL;
                            SpaceShipList.Items.Add("Gunnery: " + skill.skillLVL.ToString());
                            break;
                        }
                    }
                }*/
            }
            else
            {
                SpaceShipList.Items.Remove(CharacterNameList.SelectedItem.ToString());
            }
        }

        private void DescriptionBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}



main.Designer.cs

namespace SpaceshipList
{
    partial class main
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.CharacterID = new System.Windows.Forms.Label();
            this.CharIDTextBox = new System.Windows.Forms.TextBox();
            this.APIKey = new System.Windows.Forms.Label();
            this.APIKeyTextBox = new System.Windows.Forms.TextBox();
            this.Submit = new System.Windows.Forms.Button();
            this.CharacterNameList = new System.Windows.Forms.CheckedListBox();
            this.SpaceShipList = new System.Windows.Forms.ListBox();
            this.DescriptionBox = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // CharacterID
            // 
            this.CharacterID.AutoSize = true;
            this.CharacterID.Location = new System.Drawing.Point(12, 13);
            this.CharacterID.Margin = new System.Windows.Forms.Padding(0);
            this.CharacterID.Name = "CharacterID";
            this.CharacterID.Size = new System.Drawing.Size(63, 13);
            this.CharacterID.TabIndex = 0;
            this.CharacterID.Text = "Character ID";
            // 
            // CharIDTextBox
            // 
            this.CharIDTextBox.BackColor = System.Drawing.SystemColors.ButtonFace;
            this.CharIDTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.CharIDTextBox.ForeColor = System.Drawing.SystemColors.InactiveCaptionText;
            this.CharIDTextBox.Location = new System.Drawing.Point(84, 13);
            this.CharIDTextBox.Margin = new System.Windows.Forms.Padding(0, 4, 0, 4);
            this.CharIDTextBox.Name = "CharIDTextBox";
            this.CharIDTextBox.Size = new System.Drawing.Size(98, 21);
            this.CharIDTextBox.TabIndex = 1;
            this.CharIDTextBox.Text = "1688140";
            this.CharIDTextBox.TextChanged += new System.EventHandler(this.CharIDTextBox_TextChanged);
            // 
            // APIKey
            // 
            this.APIKey.AutoSize = true;
            this.APIKey.Location = new System.Drawing.Point(12, 43);
            this.APIKey.Margin = new System.Windows.Forms.Padding(0);
            this.APIKey.Name = "APIKey";
            this.APIKey.Size = new System.Drawing.Size(42, 13);
            this.APIKey.TabIndex = 2;
            this.APIKey.Text = "API Key";
            // 
            // APIKeyTextBox
            // 
            this.APIKeyTextBox.BackColor = System.Drawing.SystemColors.ButtonFace;
            this.APIKeyTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.APIKeyTextBox.ForeColor = System.Drawing.SystemColors.InactiveCaptionText;
            this.APIKeyTextBox.Location = new System.Drawing.Point(84, 39);
            this.APIKeyTextBox.Margin = new System.Windows.Forms.Padding(0, 4, 0, 4);
            this.APIKeyTextBox.Name = "APIKeyTextBox";
            this.APIKeyTextBox.Size = new System.Drawing.Size(98, 21);
            this.APIKeyTextBox.TabIndex = 3;
            this.APIKeyTextBox.Text = "O9f9WAG5B20BlbTg8t2GiyEpdQS3ZYA5n6uq7woyYuWcvS6XfwAGR6CLUVnLT3Wq";
            this.APIKeyTextBox.TextChanged += new System.EventHandler(this.APIKeyTextBox_TextChanged);
            // 
            // Submit
            // 
            this.Submit.BackColor = System.Drawing.SystemColors.ScrollBar;
            this.Submit.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.Submit.Cursor = System.Windows.Forms.Cursors.Default;
            this.Submit.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.Submit.ForeColor = System.Drawing.SystemColors.InactiveCaptionText;
            this.Submit.Location = new System.Drawing.Point(122, 75);
            this.Submit.Margin = new System.Windows.Forms.Padding(0, 4, 0, 4);
            this.Submit.Name = "Submit";
            this.Submit.Size = new System.Drawing.Size(60, 22);
            this.Submit.TabIndex = 5;
            this.Submit.Text = "Submit";
            this.Submit.UseVisualStyleBackColor = false;
            this.Submit.Click += new System.EventHandler(this.Submit_Click);
            // 
            // CharacterNameList
            // 
            this.CharacterNameList.BackColor = System.Drawing.SystemColors.ButtonFace;
            this.CharacterNameList.Checkonclick = true;
            this.CharacterNameList.ForeColor = System.Drawing.SystemColors.InactiveCaptionText;
            this.CharacterNameList.FormattingEnabled = true;
            this.CharacterNameList.Location = new System.Drawing.Point(192, 13);
            this.CharacterNameList.Margin = new System.Windows.Forms.Padding(0, 4, 0, 4);
            this.CharacterNameList.Name = "CharacterNameList";
            this.CharacterNameList.Size = new System.Drawing.Size(122, 84);
            this.CharacterNameList.TabIndex = 7;
            this.CharacterNameList.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.CharacterNameList_ItemCheck);
            // 
            // SpaceShipList
            // 
            this.SpaceShipList.BackColor = System.Drawing.SystemColors.ButtonFace;
            this.SpaceShipList.ForeColor = System.Drawing.SystemColors.InactiveCaptionText;
            this.SpaceShipList.FormattingEnabled = true;
            this.SpaceShipList.Location = new System.Drawing.Point(318, 13);
            this.SpaceShipList.Margin = new System.Windows.Forms.Padding(0, 4, 0, 4);
            this.SpaceShipList.Name = "SpaceShipList";
            this.SpaceShipList.Size = new System.Drawing.Size(236, 329);
            this.SpaceShipList.TabIndex = 9;
            // 
            // DescriptionBox
            // 
            this.DescriptionBox.BackColor = System.Drawing.SystemColors.ButtonFace;
            this.DescriptionBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.DescriptionBox.Location = new System.Drawing.Point(12, 102);
            this.DescriptionBox.Multiline = true;
            this.DescriptionBox.Name = "DescriptionBox";
            this.DescriptionBox.ReadOnly = true;
            this.DescriptionBox.Size = new System.Drawing.Size(302, 240);
            this.DescriptionBox.TabIndex = 10;
            this.DescriptionBox.TextChanged += new System.EventHandler(this.DescriptionBox_TextChanged);
            // 
            // main
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.ButtonFace;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.ClientSize = new System.Drawing.Size(563, 351);
            this.Controls.Add(this.DescriptionBox);
            this.Controls.Add(this.SpaceShipList);
            this.Controls.Add(this.CharacterNameList);
            this.Controls.Add(this.Submit);
            this.Controls.Add(this.APIKeyTextBox);
            this.Controls.Add(this.APIKey);
            this.Controls.Add(this.CharIDTextBox);
            this.Controls.Add(this.CharacterID);
            this.Cursor = System.Windows.Forms.Cursors.Default;
            this.Font = new System.Drawing.Font("Corbel", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ForeColor = System.Drawing.SystemColors.InactiveCaptionText;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Margin = new System.Windows.Forms.Padding(0, 4, 0, 4);
            this.MaximizeBox = false;
            this.Name = "main";
            this.ShowIcon = false;
            this.Text = "Spaceship List";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label CharacterID;
        private System.Windows.Forms.TextBox CharIDTextBox;
        private System.Windows.Forms.Label APIKey;
        private System.Windows.Forms.TextBox APIKeyTextBox;
        private System.Windows.Forms.Button Submit;
        private System.Windows.Forms.CheckedListBox CharacterNameList;
        private System.Windows.Forms.ListBox SpaceShipList;
        private System.Windows.Forms.TextBox DescriptionBox;
    }
}


And the link to the XML for the CharacterSheet: https://api.eveonlin...terID=282200859

I would appreciate any help, thanks :)/>

Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object, Issue using LINQ

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,652
  • Joined: 02-June 10

Re: Object reference not set to an instance of an object, Issue using LINQ

Posted 09 February 2013 - 07:48 AM

This is the first error I cover in the "What is this error?" article linked further below.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler



Was This Post Helpful? 1
  • +
  • -

#3 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Re: Object reference not set to an instance of an object, Issue using LINQ

Posted 09 February 2013 - 08:50 AM

I seemed to have found the problem code, but I don't understand why it's causing the issue. When I comment out this code:

foreach (var result in characterProfile)
                {
                    DescriptionBox.Text += "Character ID: " + result.charID;
                    DescriptionBox.Text += "\nCharacter Name: " + result.charName;
                }



There is no exception, but when I uncomment it, I get the exception. Why is that code causing the exception? When I replace DescriptionBox.Text with just a string variable I get the same exception, so it's not that object. The highlighted part in VS is

var characterProfile = from result in xmlDocProfile.Descendants("result")
                                        select new
                                        {
                                            charID = result.Element("characterID").Value,
                                            charName = result.Element("name").Value,
                                            DoB = result.Element("DoB").Value,
                                            race = result.Element("race").Value,
                                            bloodline = result.Element("bloodline").Value,
                                            ancestry = result.Element("ancestry").Value,
                                            gender = result.Element("gender").Value,
                                            corporationName = result.Element("corporationName").Value,
                                            allianceName = result.Element("allianceName").Value,
                                            cloneName = result.Element("cloneName").Value,
                                            cloneSkillPoints = result.Element("cloneSkillPoints").Value,
                                            balance = result.Element("balance").Value
                                        };



but the only object is xmlDocProfile which is instantiated here:

XDocument xmlDocProfile = Xdocument.Load(url);



Maybe I'm just overlooking something but I really don't see what's causing the issue.

This post has been edited by kiasta: 09 February 2013 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,652
  • Joined: 02-June 10

Re: Object reference not set to an instance of an object, Issue using LINQ

Posted 09 February 2013 - 09:21 AM

Quote

There is no exception, but when I uncomment it, I get the exception. Why is that code causing the exception?


because one of the objects on that line is null.
Look at the Locals pallet, or hover your mouse over each object to get its value. one of them will be null.

Did you read the "What does this error mean?" article I pointed you to? Because I teach how to determine this in that article.
Was This Post Helpful? 1
  • +
  • -

#5 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Re: Object reference not set to an instance of an object, Issue using LINQ

Posted 10 February 2013 - 08:56 PM

I figured it out, it was actually simple. Here is my revised code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace SpaceshipList
{
    public partial class main : Form
    {
        string[] charNameLB = new string[3];
        XDocument xmlDoc = new XDocument();
        public main()
        {
            InitializeComponent();
        }

        private void Submit_Click(object sender, EventArgs e)
        {
            CharacterNameList.Items.Clear();
            string ID = CharIDTextBox.Text;
            string API = APIKeyTextBox.Text;

            string url = "https://api.eveonline.com/account/characters.xml.aspx?keyID=" + ID + "&vCode=" + API;

            xmlDoc = Xdocument.Load(url);
            var characters = from row in xmlDoc.Descendants("row")
                                select new Character
                                {
                                    name = row.Attribute("name").Value
                                };
            int i = 0;
            foreach(var row in characters)
            {
                CharacterNameList.Items.Add(row.name, CheckState.Unchecked);
                charNameLB[i] = row.name;
                ++i;
            }
            i = 0;
        }

        private void CharacterNameList_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            string ID = CharIDTextBox.Text;
            string API = APIKeyTextBox.Text;
            string characterID = "";
            string url = "https://api.eveonline.com/account/characters.xml.aspx?keyID=" + ID + "&vCode=" + API;
            var checkedListBox = (CheckedListBox)sender;
            string checkedItemText = checkedListBox.Items[e.Index].ToString();
            if (e.NewValue == CheckState.Checked)
            {
                xmlDoc = Xdocument.Load(url);
                var playerID = from row in xmlDoc.Descendants("row")
                                 select new Character
                                 {
                                     ID = row.Attribute("characterID").Value
                                 };
                foreach (var row in playerID)
                {
                    characterID = row.ID;
                }
                url = "https://api.eveonline.com/char/CharacterSheet.xml.aspx?keyID=" + ID + "&vCode=" + API + "&characterID=" + characterID;
                xmlDoc = Xdocument.Load(url);
                var characters = from result in xmlDoc.Descendants("result")
                                    select new Character
                                    {
                                        name = result.Element("name").Value,
                                        ID = result.Element("characterID").Value,
                                        DoB = result.Element("DoB").Value,
                                        race = result.Element("race").Value,
                                        bloodLine = result.Element("bloodLine").Value,
                                        ancestry = result.Element("ancestry").Value,
                                        gender = result.Element("gender").Value,
                                        corporation = result.Element("corporationName").Value,
                                        alliance = result.Element("allianceName").Value,
                                        clone = result.Element("cloneName").Value,
                                        cloneSkillPoints = result.Element("cloneSkillPoints").Value,
                                        balance = result.Element("balance").Value
                                    };
                foreach (var result in characters)
                {
                    if (checkedItemText == result.name)
                    {
                        DescriptionBox.Text += "Character Info\r\n\r\n";
                        DescriptionBox.Text += "Name: " + result.name;
                        DescriptionBox.Text += "\r\nID: " + result.ID;
                        DescriptionBox.Text += "\r\nDoB: " + result.DoB;
                        DescriptionBox.Text += "\r\nRace: " + result.race;
                        DescriptionBox.Text += "\r\nBloodline: " + result.bloodLine;
                        DescriptionBox.Text += "\r\nAncestry: " + result.ancestry;
                        DescriptionBox.Text += "\r\nGender: " + result.gender;
                        DescriptionBox.Text += "\r\nCorporation: " + result.corporation;
                        DescriptionBox.Text += "\r\nAlliance: " + result.alliance;
                        DescriptionBox.Text += "\r\nClone: " + result.clone;
                        DescriptionBox.Text += "\r\nClone Skill Points: " + result.cloneSkillPoints;
                        DescriptionBox.Text += "\r\nBalance: " + result.balance;
                    }
                }
            }
            else
            {
                DescriptionBox.Text = "";
            }
        }
        private void DescriptionBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void CharacterNameList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
    public class Character
    {
        public string name { get; set; }
        public string ID { get; set; }
        public string DoB { get; set; }
        public string race { get; set; }
        public string bloodLine { get; set; }
        public string ancestry { get; set; }
        public string gender { get; set; }
        public string corporation { get; set; }
        public string alliance { get; set; }
        public string clone { get; set; }
        public string cloneSkillPoints { get; set; }
        public string balance { get; set; }
    }
    public class Attributes
    {
        public string intelligence { get; set; }
        public string memory { get; set; }
        public string charisma { get; set; }
        public string perception { get; set; }
        public string willpower { get; set; }
    }
    public class Skill
    {
        public string name { get; set; }
        public string type { get; set; }
    }
}



Thanks a lot for your advice :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1