Object Reference Not set to an instance of an object - Help!!!!

A

andyblum

Hi all,

I know this message usually relates to trying to use a NULL instance of
an object. However, this is not the case in my code.
DestinationDirectory is a seperate class defined as follows:


Namespace WebLogFileMover
{
class DestinationDirectory
{
string path;
string deleteCriteria;
string type;

public DestinationDirectory()
{
path = "";
deleteCriteria = "";
type = "";
}

public string Path
{
get { return path; }
set { path = value; }
}

public string Type
{
get { return type; }
set { type = value; }
}

public string DeleteCriteria
{
get { return deleteCriteria; }
set { deleteCriteria = value; }
}
}
}




As you can see from the code below I am defining an array of this
class:





namespace WebLogFileMover
{
class ConfigurationInfo
{
ArrayList sourcePaths;
DestinationDirectory[] destinationDirectories;
DateTime lastAccessDate;
XmlDOMReader reader;
EventLogHandler el;

public ConfigurationInfo(string theFile)
{
try
{
reader = new XmlDOMReader(theFile);
el = new EventLogHandler();
PopulateSourcePaths();
PopulateLastAccessDate();
PopulateDestinationDirectories();
}
catch (Exception e)
{
el.WriteEvent("WLFM", "Unable to construct
ConfigurationClass. " + e.Message);
}
}

private void PopulateSourcePaths()
{
sourcePaths = reader.readXMLDOM("FileDir", "path");
}

private void PopulateDestinationDirectories()
{
ArrayList path;
ArrayList type;
ArrayList deleteCriteria;
path = reader.readXMLDOM("DirLocation", "path");
type = reader.readXMLDOM("DirLocation", "type");
deleteCriteria = reader.readXMLDOM("DirLocation",
"DeleteCriteria");

for (int i = 0; i <= (path.Count - 1);i++ )
{
destinationDirectories = new DestinationDirectory();
destinationDirectories.Path = path.ToString();
destinationDirectories.Type = type.ToString();
destinationDirectories.DeleteCriteria =
deleteCriteria.ToString();
i++;
}

}
private void PopulateLastAccessDate()
{
ArrayList a;
a = reader.readXMLDOM("AppData", "LastAccess");
lastAccessDate =
System.Convert.ToDateTime(a[0].ToString());
}

}
}
When I call the method PopulateDestinationDirectories() I am looping
through and instantiating a new instance:

for (int i = 0; i <= (path.Count - 1);i++ )
{
destinationDirectories = new DestinationDirectory();
destinationDirectories.Path = path.ToString();
destinationDirectories.Type = type.ToString();
destinationDirectories.DeleteCriteria =
deleteCriteria.ToString();
i++;
}

Yet, even so I am still getting the Object Reference Not set... error
message. When I debug it actually goes through the constructor and then
returns the message. Any suggestons would be appreaciated????
 
C

Claes Bergefall

You need to specify a size for your destinationDirectories array. When you
do destinationDirectories = new DestinationDirectory() it will
successfully create the DestinationDirectory object but when it then tries
to put it in the array you get the error since the array doesn't have room
for any elements (i.e. its size is 0).

Either specify a size or use the generic List<T> class instead


/claes

Hi all,

I know this message usually relates to trying to use a NULL instance of
an object. However, this is not the case in my code.
DestinationDirectory is a seperate class defined as follows:


Namespace WebLogFileMover
{
class DestinationDirectory
{
string path;
string deleteCriteria;
string type;

public DestinationDirectory()
{
path = "";
deleteCriteria = "";
type = "";
}

public string Path
{
get { return path; }
set { path = value; }
}

public string Type
{
get { return type; }
set { type = value; }
}

public string DeleteCriteria
{
get { return deleteCriteria; }
set { deleteCriteria = value; }
}
}
}




As you can see from the code below I am defining an array of this
class:





namespace WebLogFileMover
{
class ConfigurationInfo
{
ArrayList sourcePaths;
DestinationDirectory[] destinationDirectories;
DateTime lastAccessDate;
XmlDOMReader reader;
EventLogHandler el;

public ConfigurationInfo(string theFile)
{
try
{
reader = new XmlDOMReader(theFile);
el = new EventLogHandler();
PopulateSourcePaths();
PopulateLastAccessDate();
PopulateDestinationDirectories();
}
catch (Exception e)
{
el.WriteEvent("WLFM", "Unable to construct
ConfigurationClass. " + e.Message);
}
}

private void PopulateSourcePaths()
{
sourcePaths = reader.readXMLDOM("FileDir", "path");
}

private void PopulateDestinationDirectories()
{
ArrayList path;
ArrayList type;
ArrayList deleteCriteria;
path = reader.readXMLDOM("DirLocation", "path");
type = reader.readXMLDOM("DirLocation", "type");
deleteCriteria = reader.readXMLDOM("DirLocation",
"DeleteCriteria");

for (int i = 0; i <= (path.Count - 1);i++ )
{
destinationDirectories = new DestinationDirectory();
destinationDirectories.Path = path.ToString();
destinationDirectories.Type = type.ToString();
destinationDirectories.DeleteCriteria =
deleteCriteria.ToString();
i++;
}

}
private void PopulateLastAccessDate()
{
ArrayList a;
a = reader.readXMLDOM("AppData", "LastAccess");
lastAccessDate =
System.Convert.ToDateTime(a[0].ToString());
}

}
}
When I call the method PopulateDestinationDirectories() I am looping
through and instantiating a new instance:

for (int i = 0; i <= (path.Count - 1);i++ )
{
destinationDirectories = new DestinationDirectory();
destinationDirectories.Path = path.ToString();
destinationDirectories.Type = type.ToString();
destinationDirectories.DeleteCriteria =
deleteCriteria.ToString();
i++;
}

Yet, even so I am still getting the Object Reference Not set... error
message. When I debug it actually goes through the constructor and then
returns the message. Any suggestons would be appreaciated????
 
A

andyblum

Thank You very much. I have moved on to using the generic Lists,
however if I was to provide a size for the array such as
destinationDirectories = new DestinationDirectory(15), how would I
set up the constuctor to accept that?
Claes said:
You need to specify a size for your destinationDirectories array. When you
do destinationDirectories = new DestinationDirectory() it will
successfully create the DestinationDirectory object but when it then tries
to put it in the array you get the error since the array doesn't have room
for any elements (i.e. its size is 0).

Either specify a size or use the generic List<T> class instead


/claes

Hi all,

I know this message usually relates to trying to use a NULL instance of
an object. However, this is not the case in my code.
DestinationDirectory is a seperate class defined as follows:


Namespace WebLogFileMover
{
class DestinationDirectory
{
string path;
string deleteCriteria;
string type;

public DestinationDirectory()
{
path = "";
deleteCriteria = "";
type = "";
}

public string Path
{
get { return path; }
set { path = value; }
}

public string Type
{
get { return type; }
set { type = value; }
}

public string DeleteCriteria
{
get { return deleteCriteria; }
set { deleteCriteria = value; }
}
}
}




As you can see from the code below I am defining an array of this
class:





namespace WebLogFileMover
{
class ConfigurationInfo
{
ArrayList sourcePaths;
DestinationDirectory[] destinationDirectories;
DateTime lastAccessDate;
XmlDOMReader reader;
EventLogHandler el;

public ConfigurationInfo(string theFile)
{
try
{
reader = new XmlDOMReader(theFile);
el = new EventLogHandler();
PopulateSourcePaths();
PopulateLastAccessDate();
PopulateDestinationDirectories();
}
catch (Exception e)
{
el.WriteEvent("WLFM", "Unable to construct
ConfigurationClass. " + e.Message);
}
}

private void PopulateSourcePaths()
{
sourcePaths = reader.readXMLDOM("FileDir", "path");
}

private void PopulateDestinationDirectories()
{
ArrayList path;
ArrayList type;
ArrayList deleteCriteria;
path = reader.readXMLDOM("DirLocation", "path");
type = reader.readXMLDOM("DirLocation", "type");
deleteCriteria = reader.readXMLDOM("DirLocation",
"DeleteCriteria");

for (int i = 0; i <= (path.Count - 1);i++ )
{
destinationDirectories = new DestinationDirectory();
destinationDirectories.Path = path.ToString();
destinationDirectories.Type = type.ToString();
destinationDirectories.DeleteCriteria =
deleteCriteria.ToString();
i++;
}

}
private void PopulateLastAccessDate()
{
ArrayList a;
a = reader.readXMLDOM("AppData", "LastAccess");
lastAccessDate =
System.Convert.ToDateTime(a[0].ToString());
}

}
}
When I call the method PopulateDestinationDirectories() I am looping
through and instantiating a new instance:

for (int i = 0; i <= (path.Count - 1);i++ )
{
destinationDirectories = new DestinationDirectory();
destinationDirectories.Path = path.ToString();
destinationDirectories.Type = type.ToString();
destinationDirectories.DeleteCriteria =
deleteCriteria.ToString();
i++;
}

Yet, even so I am still getting the Object Reference Not set... error
message. When I debug it actually goes through the constructor and then
returns the message. Any suggestons would be appreaciated????
 
M

Mel

Here is one way to declare it.


namespace WebLogFileMover
{
class ConfigurationInfo
{
ArrayList sourcePaths;
DestinationDirectory[] destinationDirectories = new
DestinationDirectory[10];
DateTime lastAccessDate;
XmlDOMReader reader;
.......
}
..........
}



Thank You very much. I have moved on to using the generic Lists,
however if I was to provide a size for the array such as
destinationDirectories = new DestinationDirectory(15), how would I
set up the constuctor to accept that?
Claes said:
You need to specify a size for your destinationDirectories array. When
you
do destinationDirectories = new DestinationDirectory() it will
successfully create the DestinationDirectory object but when it then
tries
to put it in the array you get the error since the array doesn't have
room
for any elements (i.e. its size is 0).

Either specify a size or use the generic List<T> class instead


/claes

Hi all,

I know this message usually relates to trying to use a NULL instance of
an object. However, this is not the case in my code.
DestinationDirectory is a seperate class defined as follows:


Namespace WebLogFileMover
{
class DestinationDirectory
{
string path;
string deleteCriteria;
string type;

public DestinationDirectory()
{
path = "";
deleteCriteria = "";
type = "";
}

public string Path
{
get { return path; }
set { path = value; }
}

public string Type
{
get { return type; }
set { type = value; }
}

public string DeleteCriteria
{
get { return deleteCriteria; }
set { deleteCriteria = value; }
}
}
}




As you can see from the code below I am defining an array of this
class:





namespace WebLogFileMover
{
class ConfigurationInfo
{
ArrayList sourcePaths;
DestinationDirectory[] destinationDirectories;
DateTime lastAccessDate;
XmlDOMReader reader;
EventLogHandler el;

public ConfigurationInfo(string theFile)
{
try
{
reader = new XmlDOMReader(theFile);
el = new EventLogHandler();
PopulateSourcePaths();
PopulateLastAccessDate();
PopulateDestinationDirectories();
}
catch (Exception e)
{
el.WriteEvent("WLFM", "Unable to construct
ConfigurationClass. " + e.Message);
}
}

private void PopulateSourcePaths()
{
sourcePaths = reader.readXMLDOM("FileDir", "path");
}

private void PopulateDestinationDirectories()
{
ArrayList path;
ArrayList type;
ArrayList deleteCriteria;
path = reader.readXMLDOM("DirLocation", "path");
type = reader.readXMLDOM("DirLocation", "type");
deleteCriteria = reader.readXMLDOM("DirLocation",
"DeleteCriteria");

for (int i = 0; i <= (path.Count - 1);i++ )
{
destinationDirectories = new DestinationDirectory();
destinationDirectories.Path = path.ToString();
destinationDirectories.Type = type.ToString();
destinationDirectories.DeleteCriteria =
deleteCriteria.ToString();
i++;
}

}
private void PopulateLastAccessDate()
{
ArrayList a;
a = reader.readXMLDOM("AppData", "LastAccess");
lastAccessDate =
System.Convert.ToDateTime(a[0].ToString());
}

}
}
When I call the method PopulateDestinationDirectories() I am looping
through and instantiating a new instance:

for (int i = 0; i <= (path.Count - 1);i++ )
{
destinationDirectories = new DestinationDirectory();
destinationDirectories.Path = path.ToString();
destinationDirectories.Type = type.ToString();
destinationDirectories.DeleteCriteria =
deleteCriteria.ToString();
i++;
}

Yet, even so I am still getting the Object Reference Not set... error
message. When I debug it actually goes through the constructor and then
returns the message. Any suggestons would be appreaciated????

 
J

Jon Skeet [C# MVP]

Claes Bergefall said:
You need to specify a size for your destinationDirectories array. When you
do destinationDirectories = new DestinationDirectory() it will
successfully create the DestinationDirectory object but when it then tries
to put it in the array you get the error since the array doesn't have room
for any elements (i.e. its size is 0).


No, that's not quite the issue - it's not that destinationDirectories
is a reference to an array with a size of 0, it's a null reference -
the variable itself is never assigned to.
 
J

Jon Skeet [C# MVP]

Thank You very much. I have moved on to using the generic Lists,
however if I was to provide a size for the array such as
destinationDirectories = new DestinationDirectory(15), how would I
set up the constuctor to accept that?


You'd do that before actually populating the array:

destinationDirectories = new DestinationDirectory[15];

then put your loop after that. Note the different type of brackets - no
constructors are being called here; after the array has been created,
each element of it will be null.
 
G

Gnanam

Hi all,

I know this message usually relates to trying to use a NULL instance of
an object. However, this is not the case in my code.
DestinationDirectory is a seperate class defined as follows:


Namespace WebLogFileMover
{
class DestinationDirectory
{
string path;
string deleteCriteria;
string type;

public DestinationDirectory()
{
path = "";
deleteCriteria = "";
type = "";
}

public string Path
{
get { return path; }
set { path = value; }
}

public string Type
{
get { return type; }
set { type = value; }
}

public string DeleteCriteria
{
get { return deleteCriteria; }
set { deleteCriteria = value; }
}
}
}




As you can see from the code below I am defining an array of this
class:





namespace WebLogFileMover
{
class ConfigurationInfo
{
ArrayList sourcePaths;
DestinationDirectory[] destinationDirectories;
DateTime lastAccessDate;
XmlDOMReader reader;
EventLogHandler el;

public ConfigurationInfo(string theFile)
{
try
{
reader = new XmlDOMReader(theFile);
el = new EventLogHandler();
PopulateSourcePaths();
PopulateLastAccessDate();
PopulateDestinationDirectories();
}
catch (Exception e)
{
el.WriteEvent("WLFM", "Unable to construct
ConfigurationClass. " + e.Message);
}
}

private void PopulateSourcePaths()
{
sourcePaths = reader.readXMLDOM("FileDir", "path");
}

private void PopulateDestinationDirectories()
{
ArrayList path;
ArrayList type;
ArrayList deleteCriteria;
path = reader.readXMLDOM("DirLocation", "path");
type = reader.readXMLDOM("DirLocation", "type");
deleteCriteria = reader.readXMLDOM("DirLocation",
"DeleteCriteria");

for (int i = 0; i <= (path.Count - 1);i++ )
{
destinationDirectories = new DestinationDirectory();
destinationDirectories.Path = path.ToString();
destinationDirectories.Type = type.ToString();
destinationDirectories.DeleteCriteria =
deleteCriteria.ToString();
i++;
}

}
private void PopulateLastAccessDate()
{
ArrayList a;
a = reader.readXMLDOM("AppData", "LastAccess");
lastAccessDate =
System.Convert.ToDateTime(a[0].ToString());
}

}
}
When I call the method PopulateDestinationDirectories() I am looping
through and instantiating a new instance:

for (int i = 0; i <= (path.Count - 1);i++ )
{
destinationDirectories = new DestinationDirectory();
destinationDirectories.Path = path.ToString();
destinationDirectories.Type = type.ToString();
destinationDirectories.DeleteCriteria =
deleteCriteria.ToString();
i++;
}

Yet, even so I am still getting the Object Reference Not set... error
message. When I debug it actually goes through the constructor and then
returns the message. Any suggestons would be appreaciated????






hai

you need to write scope of the variable(private,public....)

Namespace WebLogFileMover
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top