Object reference not set to an instance of an object
-
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
-
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
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.
-
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
-
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
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.
-
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.
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());
}
}
} -
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());
}
}
}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
-
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
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