Object reference not set to an instance of an object

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 16254 Views - Last Post: 19 December 2011 - 03:22 AM Rate Topic: -----

#1 Poornima Mani   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 25-January 09

Object reference not set to an instance of an object

Post icon  Posted 10 March 2009 - 03:42 AM

Hi
When i debug this code im getting "Object reference not set to an instance of an object"..But then the form with the controls are displayed..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)
							{
			   String[] folder = Directory.GetDirectories(sss);
								foreach (string foldername in folder)
								{
			  DirectoryInfo di = new DirectoryInfo(foldername);
									if (di.Exists)
									{
				String[] files = Directory.GetFiles(foldername);
										foreach (String filename in files)
										{
											FileInfo fil2 = new FileInfo(filename);
						   ListViewItem item1 = new ListViewItem();
											item1.Text = fil2.Name;
											item1.ImageIndex = 1;
											item1.Tag = fil2.FullName;
						 item1.SubItems.Add("Programming .NET");
											listView.Items.Add(item1);
									
										}}}}
						   
							break;
					}}}
		   
			catch (Exception e)
			{
				MessageBox.Show(e.Message);
			}
		}
		private void InitializeComponent()
		{
			this.SuspendLayout();
			this.ClientSize = new System.Drawing.Size(756, 485);
			this.Name = "ListView1";
			this.ResumeLayout(false);	
	}

		private void button1_Click(object sender, EventArgs e)
		{
			//listBox1 lb = (listBox1)sender;
			string[] drives = Environment.GetLogicalDrives();
			// Loop into the string array
			foreach (string strDrive in drives)
			{
				// Add items (drives) to the list
				listBox1.Items.Add(strDrive.ToString());
			}}}}
	
 


Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Object reference not set to an instance of an object

Posted 10 March 2009 - 03:54 AM

Which object is the debugger complaining about?
Was This Post Helpful? 0
  • +
  • -

#3 Poornima Mani   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 25-January 09

Re: Object reference not set to an instance of an object

Posted 10 March 2009 - 04:11 AM

View Postn8wxs, on 10 Mar, 2009 - 02:54 AM, said:

Which object is the debugger complaining about?

hi
It simply gives that error.It does not display the line number
Was This Post Helpful? 0
  • +
  • -

#4 ragingben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Object reference not set to an instance of an object

Posted 10 March 2009 - 06:31 AM

I think that you are not calling the InitializeComponent() method, which should be the first thing in your constructor unless I'm missing something?
Was This Post Helpful? 0
  • +
  • -

#5 Poornima Mani   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 25-January 09

Re: Object reference not set to an instance of an object

Posted 10 March 2009 - 06:52 AM

View Postragingben, on 10 Mar, 2009 - 05:31 AM, said:

I think that you are not calling the InitializeComponent() method, which should be the first thing in your constructor unless I'm missing something?

hi,
I tried by calling initializeComponent() but still it does not work..
Again it gives me an error" Object reference not set to an instance of the object"
Was This Post Helpful? 0
  • +
  • -

#6 ragingben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Object reference not set to an instance of an object

Posted 10 March 2009 - 07:44 AM

Ok, I've fidled with the code on my computer to get it to riun, and it loads, but can't find the XML document, as expected. But the main thing is it ran to that stage, and then continued fine after, so without having your whole project and the xml file (which would be handy if you could post) I assume that the error is somewhere within this code...
 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)
							{
			   String[] folder = Directory.GetDirectories(sss);
								foreach (string foldername in folder)
								{
			  DirectoryInfo di = new DirectoryInfo(foldername);
									if (di.Exists)
									{
				String[] files = Directory.GetFiles(foldername);
										foreach (String filename in files)
										{
											FileInfo fil2 = new FileInfo(filename);
						   ListViewItem item1 = new ListViewItem();
											item1.Text = fil2.Name;
											item1.ImageIndex = 1;
											item1.Tag = fil2.FullName;
						 item1.SubItems.Add("Programming .NET");
											listView.Items.Add(item1);
									
										}}}}
						   
							break;
					}}}
		   
			catch (Exception e)
			{
				MessageBox.Show(e.Message);
			}
		}



And there seems to be nothing wrong with your constructor, your not licking the button and thus calling taht code before the error and you are not calling InitialiseComponent()

Try debugging with a breakpoint and stepping through with F11 untill you find the line that spazzes it out
Was This Post Helpful? 0
  • +
  • -

#7 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Object reference not set to an instance of an object

Posted 10 March 2009 - 05:19 PM

I get the same behaviour as ragingben - it compiles and runs. It can't find the XML file of course.
Was This Post Helpful? 0
  • +
  • -

#8 Poornima Mani   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 25-January 09

Re: Object reference not set to an instance of an object

Posted 10 March 2009 - 11:24 PM

View Postn8wxs, on 10 Mar, 2009 - 04:19 PM, said:

I get the same behaviour as ragingben - it compiles and runs. It can't find the XML file of course.

Hi,
I have saved the path.xml file in the bin folder of the WindowsApplication...I tried by calling the InitializeComponent() but it again it gives me an error
Was This Post Helpful? 0
  • +
  • -

#9 Poornima Mani   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 25-January 09

Re: Object reference not set to an instance of an object

Posted 10 March 2009 - 11:46 PM

View Postragingben, on 10 Mar, 2009 - 06:44 AM, said:

Ok, I've fidled with the code on my computer to get it to riun, and it loads, but can't find the XML document, as expected. But the main thing is it ran to that stage, and then continued fine after, so without having your whole project and the xml file (which would be handy if you could post) I assume that the error is somewhere within this code...
 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)
							{
			   String[] folder = Directory.GetDirectories(sss);
								foreach (string foldername in folder)
								{
			  DirectoryInfo di = new DirectoryInfo(foldername);
									if (di.Exists)
									{
				String[] files = Directory.GetFiles(foldername);
										foreach (String filename in files)
										{
											FileInfo fil2 = new FileInfo(filename);
						   ListViewItem item1 = new ListViewItem();
											item1.Text = fil2.Name;
											item1.ImageIndex = 1;
											item1.Tag = fil2.FullName;
						 item1.SubItems.Add("Programming .NET");
											listView.Items.Add(item1);
									
										}}}}
						   
							break;
					}}}
		   
			catch (Exception e)
			{
				MessageBox.Show(e.Message);
			}
		}



And there seems to be nothing wrong with your constructor, your not licking the button and thus calling taht code before the error and you are not calling InitialiseComponent()

Try debugging with a breakpoint and stepping through with F11 untill you find the line that spazzes it out

Hi,
I tried debugging with the Step Into option ,it points to line
 static void Main()
		{
			//Application.EnableVisualStyles();
			//Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new ListView1());
		}
in Program.cs

//code for Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using Windows19;

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


Does it mean that there is an error in the ListView() constructor?
Any suggestions please...
Was This Post Helpful? 0
  • +
  • -

#10 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Object reference not set to an instance of an object

Posted 10 March 2009 - 11:54 PM

You have not told the Program Class what namespace to use to activate ListView1();
Was This Post Helpful? 0
  • +
  • -

#11 Poornima Mani   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 25-January 09

Re: Object reference not set to an instance of an object

Posted 11 March 2009 - 12:19 AM

View Postn8wxs, on 10 Mar, 2009 - 10:54 PM, said:

You have not told the Program Class what namespace to use to activate ListView1();

Hi,
I have used
using Windows19;
which is the namespace of ListView1.cs
It says,
WindowsApplication19.exe!WindowsApplication19.Program.Main()

This post has been edited by Poornima Mani: 11 March 2009 - 01:08 AM

Was This Post Helpful? 0
  • +
  • -

#12 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Object reference not set to an instance of an object

Posted 11 March 2009 - 02:21 AM

 namespace Windows21
{
	public class ListView1 : Form
	{


Was This Post Helpful? 0
  • +
  • -

#13 Poornima Mani   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 25-January 09

Re: Object reference not set to an instance of an object

Posted 11 March 2009 - 03:42 AM

View Postn8wxs, on 11 Mar, 2009 - 01:21 AM, said:

 namespace Windows21
{
	public class ListView1 : Form
	{


Hi
I have changed the name of the namespace for ListView1.cs as Listsample so i have included using Listsample in Program.cs.
Was This Post Helpful? 0
  • +
  • -

#14 mfaheemriaz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-September 08

Re: Object reference not set to an instance of an object

Posted 18 December 2011 - 12:35 PM

i am having the same error, when my program compiles the last line it generates the error

Object reference not set to an instance of an object.
also screen shot is attached
plz help ASAP
if (Education_Id > 0)
            {
                Education objEducation = new Education((Education_Id));
                txtDegreeName.Text = objEducation.Education_Degree_Name;
                txtInstituteName.Text = objEducation.Education_Institute;
                txtBoardUniv.Text = objEducation.Education_Board_Uni;
                txtPassingYear.Text = objEducation.Education_Passing_Year;
                txtMarksObtained.Text = objEducation.Education_Marks;
                txtMajorSubjects.Text = objEducation.Education_Majors;
                cmbCategory.Text = objEducation.Biodata_Type;
                cmbName.SelectedValue = objEducation.Biodata_Id;
                
                if (cmbCategory.Text == "Student")
                    cmbRollNo.SelectedValue = objEducation.Biodata_Id;

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#15 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: Object reference not set to an instance of an object

Posted 18 December 2011 - 01:30 PM

It means that you haven't initialized a object that you're referencing.

A error tutorial:
What does this error mean? Understanding the common errors (DIC Tutorial)

This post has been edited by RexGrammer: 18 December 2011 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2