Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Object reference not set to an instance of an object

Object reference not set to an instance of an object

Scheduled Pinned Locked Moved C#
graphicsdebuggingxmlhelp
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrithula8
    wrote on last edited by
    #1

    Hi I have changed the code.When i debug this code im getting "Object reference not set to an instance of an object"..But then the output form window opens with the controls.The required output is not obtained..Im trying to list all the files in a folder in the ListView control..Please help me with this

    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Xml;
    namespace Windows21
    {
    public class ListView1 : Form
    {
    XmlTextReader reader;
    XmlDocument doc = new XmlDocument();
    ListView listView = new ListView();
    Button button1 = new Button();
    ListBox listBox1 = new ListBox();

        public ListView1()
        {
            //listView.Dock = DockStyle.Fill;
            listView.Location = new Point(250, 250);
            listView.Width = 250;
            listView.Height = 100;
            button1.Visible = true;
            listBox1.Visible = true;
            button1.Text = "button1";
            button1.Location = new Point(100, 100);
            listBox1.Location = new Point(100, 150);
            PopulateListView();
            this.Controls.Add(listView);
            this.Controls.Add(button1);
            this.Controls.Add(listBox1);
            this.ClientSize = new Size(400, 200);
            button1.Click += new EventHandler(button1\_Click);
        }
    
        private void PopulateListView()
        {
            try
            {
                // Set the view to show details.
                listView.View = View.Details;
                // Add columns
    

    listView.Columns.Add("FileName", -2, HorizontalAlignment.Center);
    listView.Columns.Add("Status", -2, HorizontalAlignment.Left);
    // Add items
    reader = new XmlTextReader("path.xml");
    while (reader.Read())
    {
    switch (reader.NodeType)
    {
    case XmlNodeType.Text: //Display the text in each element.
    // Console.WriteLine(reader.Value);
    String s1 = reader.Value;
    String ss = listBox1.SelectedItem.ToString();
    String sss = ss + s1;
    object o3 = (object)sss;

                            DirectoryInfo dire = new DirectoryInfo(sss);
                            if (dire.Exists)
                            {
               Stri
    
    P N R F 4 Replies Last reply
    0
    • M mrithula8

      Hi I have changed the code.When i debug this code im getting "Object reference not set to an instance of an object"..But then the output form window opens with the controls.The required output is not obtained..Im trying to list all the files in a folder in the ListView control..Please help me with this

      using System;
      using System.Windows.Forms;
      using System.Drawing;
      using System.IO;
      using System.Text;
      using System.Xml;
      namespace Windows21
      {
      public class ListView1 : Form
      {
      XmlTextReader reader;
      XmlDocument doc = new XmlDocument();
      ListView listView = new ListView();
      Button button1 = new Button();
      ListBox listBox1 = new ListBox();

          public ListView1()
          {
              //listView.Dock = DockStyle.Fill;
              listView.Location = new Point(250, 250);
              listView.Width = 250;
              listView.Height = 100;
              button1.Visible = true;
              listBox1.Visible = true;
              button1.Text = "button1";
              button1.Location = new Point(100, 100);
              listBox1.Location = new Point(100, 150);
              PopulateListView();
              this.Controls.Add(listView);
              this.Controls.Add(button1);
              this.Controls.Add(listBox1);
              this.ClientSize = new Size(400, 200);
              button1.Click += new EventHandler(button1\_Click);
          }
      
          private void PopulateListView()
          {
              try
              {
                  // Set the view to show details.
                  listView.View = View.Details;
                  // Add columns
      

      listView.Columns.Add("FileName", -2, HorizontalAlignment.Center);
      listView.Columns.Add("Status", -2, HorizontalAlignment.Left);
      // Add items
      reader = new XmlTextReader("path.xml");
      while (reader.Read())
      {
      switch (reader.NodeType)
      {
      case XmlNodeType.Text: //Display the text in each element.
      // Console.WriteLine(reader.Value);
      String s1 = reader.Value;
      String ss = listBox1.SelectedItem.ToString();
      String sss = ss + s1;
      object o3 = (object)sss;

                              DirectoryInfo dire = new DirectoryInfo(sss);
                              if (dire.Exists)
                              {
                 Stri
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Thanks for the complete code dump - I really need to know what your internal logic is. Anyway - this problem occurs because you have an object somewhere that you haven't instantiated or which has returned a null on an evaluation. The way to fix this is to first of all read the exception text which will tell you which line this error occurs at. Then, put a breakpoint just before this line and debug your code - finally, hover over variables to see what value they have - check for null in the tooltip.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys

      1 Reply Last reply
      0
      • M mrithula8

        Hi I have changed the code.When i debug this code im getting "Object reference not set to an instance of an object"..But then the output form window opens with the controls.The required output is not obtained..Im trying to list all the files in a folder in the ListView control..Please help me with this

        using System;
        using System.Windows.Forms;
        using System.Drawing;
        using System.IO;
        using System.Text;
        using System.Xml;
        namespace Windows21
        {
        public class ListView1 : Form
        {
        XmlTextReader reader;
        XmlDocument doc = new XmlDocument();
        ListView listView = new ListView();
        Button button1 = new Button();
        ListBox listBox1 = new ListBox();

            public ListView1()
            {
                //listView.Dock = DockStyle.Fill;
                listView.Location = new Point(250, 250);
                listView.Width = 250;
                listView.Height = 100;
                button1.Visible = true;
                listBox1.Visible = true;
                button1.Text = "button1";
                button1.Location = new Point(100, 100);
                listBox1.Location = new Point(100, 150);
                PopulateListView();
                this.Controls.Add(listView);
                this.Controls.Add(button1);
                this.Controls.Add(listBox1);
                this.ClientSize = new Size(400, 200);
                button1.Click += new EventHandler(button1\_Click);
            }
        
            private void PopulateListView()
            {
                try
                {
                    // Set the view to show details.
                    listView.View = View.Details;
                    // Add columns
        

        listView.Columns.Add("FileName", -2, HorizontalAlignment.Center);
        listView.Columns.Add("Status", -2, HorizontalAlignment.Left);
        // Add items
        reader = new XmlTextReader("path.xml");
        while (reader.Read())
        {
        switch (reader.NodeType)
        {
        case XmlNodeType.Text: //Display the text in each element.
        // Console.WriteLine(reader.Value);
        String s1 = reader.Value;
        String ss = listBox1.SelectedItem.ToString();
        String sss = ss + s1;
        object o3 = (object)sss;

                                DirectoryInfo dire = new DirectoryInfo(sss);
                                if (dire.Exists)
                                {
                   Stri
        
        N Offline
        N Offline
        NeCroFire
        wrote on last edited by
        #3

        Won't it be much better to use Recursion? As well as A LOT simpler? Recursion using C#[^] Or maybe I'm missing something?

        1 Reply Last reply
        0
        • M mrithula8

          Hi I have changed the code.When i debug this code im getting "Object reference not set to an instance of an object"..But then the output form window opens with the controls.The required output is not obtained..Im trying to list all the files in a folder in the ListView control..Please help me with this

          using System;
          using System.Windows.Forms;
          using System.Drawing;
          using System.IO;
          using System.Text;
          using System.Xml;
          namespace Windows21
          {
          public class ListView1 : Form
          {
          XmlTextReader reader;
          XmlDocument doc = new XmlDocument();
          ListView listView = new ListView();
          Button button1 = new Button();
          ListBox listBox1 = new ListBox();

              public ListView1()
              {
                  //listView.Dock = DockStyle.Fill;
                  listView.Location = new Point(250, 250);
                  listView.Width = 250;
                  listView.Height = 100;
                  button1.Visible = true;
                  listBox1.Visible = true;
                  button1.Text = "button1";
                  button1.Location = new Point(100, 100);
                  listBox1.Location = new Point(100, 150);
                  PopulateListView();
                  this.Controls.Add(listView);
                  this.Controls.Add(button1);
                  this.Controls.Add(listBox1);
                  this.ClientSize = new Size(400, 200);
                  button1.Click += new EventHandler(button1\_Click);
              }
          
              private void PopulateListView()
              {
                  try
                  {
                      // Set the view to show details.
                      listView.View = View.Details;
                      // Add columns
          

          listView.Columns.Add("FileName", -2, HorizontalAlignment.Center);
          listView.Columns.Add("Status", -2, HorizontalAlignment.Left);
          // Add items
          reader = new XmlTextReader("path.xml");
          while (reader.Read())
          {
          switch (reader.NodeType)
          {
          case XmlNodeType.Text: //Display the text in each element.
          // Console.WriteLine(reader.Value);
          String s1 = reader.Value;
          String ss = listBox1.SelectedItem.ToString();
          String sss = ss + s1;
          object o3 = (object)sss;

                                  DirectoryInfo dire = new DirectoryInfo(sss);
                                  if (dire.Exists)
                                  {
                     Stri
          
          R Offline
          R Offline
          Rutvik Dave
          wrote on last edited by
          #4

          As you said it gives you error message and then it shows you an output (which is different). means error is in try-catch block, faster way to find out exact location is just remove try-catch block for a while, and run your code it will jumps to debug mode when exception occurs, and you can see the line of code, which is causing problem. I think something is going wrong with the string assignment.

          M 1 Reply Last reply
          0
          • R Rutvik Dave

            As you said it gives you error message and then it shows you an output (which is different). means error is in try-catch block, faster way to find out exact location is just remove try-catch block for a while, and run your code it will jumps to debug mode when exception occurs, and you can see the line of code, which is causing problem. I think something is going wrong with the string assignment.

            M Offline
            M Offline
            mrithula8
            wrote on last edited by
            #5

            Hi, I tried debugging with Step Into option it shows error in the Program.cs file

            static void Main()
            {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new ListView1());
            }

            //code for Program.cs //Here ListView is the namespace of ListView1() so i have used using ListView.

            using System;
            using System.Collections.Generic;
            using System.Windows.Forms;
            using System.IO;
            using ListView;

            namespace WindowsApplication19
            {
            static class Program
            {
            /// /// The main entry point for the application.
            ///
            [STAThread]
            static void Main()
            {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new ListView1());
            }
            }
            }

            R 1 Reply Last reply
            0
            • M mrithula8

              Hi, I tried debugging with Step Into option it shows error in the Program.cs file

              static void Main()
              {
              //Application.EnableVisualStyles();
              //Application.SetCompatibleTextRenderingDefault(false);
              Application.Run(new ListView1());
              }

              //code for Program.cs //Here ListView is the namespace of ListView1() so i have used using ListView.

              using System;
              using System.Collections.Generic;
              using System.Windows.Forms;
              using System.IO;
              using ListView;

              namespace WindowsApplication19
              {
              static class Program
              {
              /// /// The main entry point for the application.
              ///
              [STAThread]
              static void Main()
              {
              //Application.EnableVisualStyles();
              //Application.SetCompatibleTextRenderingDefault(false);
              Application.Run(new ListView1());
              }
              }
              }

              R Offline
              R Offline
              Rutvik Dave
              wrote on last edited by
              #6

              Whats in the path.xml ? just dump it here (if it is not very large). I think the following code is causing error. somthing wrong with the string assignments.

              String ss = listBox1.SelectedItem.ToString();

              I think, this is causing problem. what if listbox1 is not selected ? and at the first time when form is loading its not selected. so that is causing problem. try this one instead,

                                      String ss;
                                      if(listBox1.SelectedIndex >= 0)
                                          ss = listBox1.SelectedItem.ToString();
                                      else
                                          ss = listBox1.Items\[0\].ToString(); //this will select the first item in the listbox
              
              1 Reply Last reply
              0
              • M mrithula8

                Hi I have changed the code.When i debug this code im getting "Object reference not set to an instance of an object"..But then the output form window opens with the controls.The required output is not obtained..Im trying to list all the files in a folder in the ListView control..Please help me with this

                using System;
                using System.Windows.Forms;
                using System.Drawing;
                using System.IO;
                using System.Text;
                using System.Xml;
                namespace Windows21
                {
                public class ListView1 : Form
                {
                XmlTextReader reader;
                XmlDocument doc = new XmlDocument();
                ListView listView = new ListView();
                Button button1 = new Button();
                ListBox listBox1 = new ListBox();

                    public ListView1()
                    {
                        //listView.Dock = DockStyle.Fill;
                        listView.Location = new Point(250, 250);
                        listView.Width = 250;
                        listView.Height = 100;
                        button1.Visible = true;
                        listBox1.Visible = true;
                        button1.Text = "button1";
                        button1.Location = new Point(100, 100);
                        listBox1.Location = new Point(100, 150);
                        PopulateListView();
                        this.Controls.Add(listView);
                        this.Controls.Add(button1);
                        this.Controls.Add(listBox1);
                        this.ClientSize = new Size(400, 200);
                        button1.Click += new EventHandler(button1\_Click);
                    }
                
                    private void PopulateListView()
                    {
                        try
                        {
                            // Set the view to show details.
                            listView.View = View.Details;
                            // Add columns
                

                listView.Columns.Add("FileName", -2, HorizontalAlignment.Center);
                listView.Columns.Add("Status", -2, HorizontalAlignment.Left);
                // Add items
                reader = new XmlTextReader("path.xml");
                while (reader.Read())
                {
                switch (reader.NodeType)
                {
                case XmlNodeType.Text: //Display the text in each element.
                // Console.WriteLine(reader.Value);
                String s1 = reader.Value;
                String ss = listBox1.SelectedItem.ToString();
                String sss = ss + s1;
                object o3 = (object)sss;

                                        DirectoryInfo dire = new DirectoryInfo(sss);
                                        if (dire.Exists)
                                        {
                           Stri
                
                F Offline
                F Offline
                fandikurnia
                wrote on last edited by
                #7

                Hello mrithula8, in a few time ago .. i have a same problem with u... and I have resolved the problem... may be you have problem with an declared variable in listbox or another object with another user (your) variable. Just make i same because it will make your listbox or object will be get a null value.. that's it

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups