Object reference set to a null reference?

A

Andy B

I am having a problem with a few classes of mine. I have the 3 classes below
(sorry if they are quite huge). When I do the following, I get a Object
reference set to null reference error. Any idea what might be going on? Just
a note that the whole Contract design isn't included since it would be very
huge in size. I also created this class setup with xsd (from an xsd file I
created).

What I did:
//create a new contract
Contract Contract = new Contract();

//create some definitions (word/definition pairs)
Definition TermHost = new Definition();
TermHost.Word="Host";
TermHost.Definition1="The person or persons doing the hiring of the
artist/musician";
//add the definition to the definition cllection class
Contract.Definitions.Add(TermHost);


The classes used:

using System;
using System.Collections;
using System.Xml.Serialization;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Contracts.ContractModel {
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace
= "http://tempuri.org/XMLSchema1.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace =
"http://tempuri.org/XMLSchema1.xsd", IsNullable = false)]
public partial class Contract {

private ContractDefinitions definitionsField;
private ContractSections sectionsField;
private Venue venueField;
private Event eventField;
private Host hostField;
private LongDistanceTransport longDistanceTransportField;
private LocalTransport localTransportField;
private Lodging lodgingField;
private Meals mealsField;
private SignatureBlock signaturesField;

public ContractDefinitions Definitions {
get {
return this.definitionsField;
}
set {
this.definitionsField = value;
}
}

public ContractSections Sections {
get {
return this.sectionsField;
}
set {
this.sectionsField = value;
}
}

public Venue Venue {
get {
return this.venueField;
}
set {
this.venueField = value;
}
}


public Event Event {
get {
return this.eventField;
}
set {
this.eventField = value;
}
}


public Host Host {
get {
return this.hostField;
}
set {
this.hostField = value;
}
}


public LongDistanceTransport LongDistanceTransport {
get {
return this.longDistanceTransportField;
}
set {
this.longDistanceTransportField = value;
}
}


public LocalTransport LocalTransport {
get {
return this.localTransportField;
}
set {
this.localTransportField = value;
}
}


public Lodging Lodging {
get {
return this.lodgingField;
}
set {
this.lodgingField = value;
}
}


public Meals Meals {
get {
return this.mealsField;
}
set {
this.mealsField = value;
}
}


public SignatureBlock Signatures {
get {
return this.signaturesField;
}
set {
this.signaturesField = value;
}
}
}
}

using System;
using System.Collections;
using System.Xml.Serialization;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Contracts.ContractModel {
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace
= "http://tempuri.org/XMLSchema1.xsd")]
public partial class ContractDefinitions : CollectionBase {
public void Add(Definition Definition) {
List.Add(Definition);
}

public void Remove(int index) {
// Check to see if there is a Definition at the supplied index.
if(index > Count - 1 || index < 0)
// If no Definition exists, a messagebox is shown and the operation
// is cancelled.
{
throw new ArgumentException("Invalid index");
} else {
List.RemoveAt(index);
}
}

public Definition Item(int index) {
return (Definition)List[index];
}

private Definition termsField;


public Definition Terms {
get {
return this.termsField;
}
set {
this.termsField = value;
}
}
}
}

using System;
using System.Collections;
using System.Xml.Serialization;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Contracts.ContractModel {
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace =
"http://tempuri.org/XMLSchema1.xsd")]
public partial class Definition {

private string definition1Field;
private string wordField;

[System.Xml.Serialization.XmlElementAttribute("Definition")]
public string Definition1 {
get {
return this.definition1Field;
}
set {
this.definition1Field = value;
}
}

[System.Xml.Serialization.XmlAttributeAttribute()]
public string Word {
get {
return this.wordField;
}
set {
this.wordField = value;
}
}

}
}
 
P

Peter Bromberg [C# MVP]

The problem here is that you've posted a bunch of sample code, but you
haven't explained to us *where* (at what line) of your code that you're
getting a null reference exception.
Suggestion:
for testing purposes, surround the entire code block with a try / catch
block and output the exception message and stacktrace to the console or to
Debug.WriteLine.
Then you will be able to pinpoint the problem by yourself. Don't forget to
turn on line numbering in the Editor.

A null reference exception means just what it says: somewhere in your code
you are attempting to do something with an object reference that is null --
in other words, there is no instance of an object in it.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net


Andy B said:
I am having a problem with a few classes of mine. I have the 3 classes below
(sorry if they are quite huge). When I do the following, I get a Object
reference set to null reference error. Any idea what might be going on? Just
a note that the whole Contract design isn't included since it would be very
huge in size. I also created this class setup with xsd (from an xsd file I
created).

What I did:
//create a new contract
Contract Contract = new Contract();

//create some definitions (word/definition pairs)
Definition TermHost = new Definition();
TermHost.Word="Host";
TermHost.Definition1="The person or persons doing the hiring of the
artist/musician";
//add the definition to the definition cllection class
Contract.Definitions.Add(TermHost);


The classes used:

using System;
using System.Collections;
using System.Xml.Serialization;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Contracts.ContractModel {
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace
= "http://tempuri.org/XMLSchema1.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace =
"http://tempuri.org/XMLSchema1.xsd", IsNullable = false)]
public partial class Contract {

private ContractDefinitions definitionsField;
private ContractSections sectionsField;
private Venue venueField;
private Event eventField;
private Host hostField;
private LongDistanceTransport longDistanceTransportField;
private LocalTransport localTransportField;
private Lodging lodgingField;
private Meals mealsField;
private SignatureBlock signaturesField;

public ContractDefinitions Definitions {
get {
return this.definitionsField;
}
set {
this.definitionsField = value;
}
}

public ContractSections Sections {
get {
return this.sectionsField;
}
set {
this.sectionsField = value;
}
}

public Venue Venue {
get {
return this.venueField;
}
set {
this.venueField = value;
}
}


public Event Event {
get {
return this.eventField;
}
set {
this.eventField = value;
}
}


public Host Host {
get {
return this.hostField;
}
set {
this.hostField = value;
}
}


public LongDistanceTransport LongDistanceTransport {
get {
return this.longDistanceTransportField;
}
set {
this.longDistanceTransportField = value;
}
}


public LocalTransport LocalTransport {
get {
return this.localTransportField;
}
set {
this.localTransportField = value;
}
}


public Lodging Lodging {
get {
return this.lodgingField;
}
set {
this.lodgingField = value;
}
}


public Meals Meals {
get {
return this.mealsField;
}
set {
this.mealsField = value;
}
}


public SignatureBlock Signatures {
get {
return this.signaturesField;
}
set {
this.signaturesField = value;
}
}
}
}

using System;
using System.Collections;
using System.Xml.Serialization;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Contracts.ContractModel {
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace
= "http://tempuri.org/XMLSchema1.xsd")]
public partial class ContractDefinitions : CollectionBase {
public void Add(Definition Definition) {
List.Add(Definition);
}

public void Remove(int index) {
// Check to see if there is a Definition at the supplied index.
if(index > Count - 1 || index < 0)
// If no Definition exists, a messagebox is shown and the operation
// is cancelled.
{
throw new ArgumentException("Invalid index");
} else {
List.RemoveAt(index);
}
}

public Definition Item(int index) {
return (Definition)List[index];
}

private Definition termsField;


public Definition Terms {
get {
return this.termsField;
}
set {
this.termsField = value;
}
}
}
}

using System;
using System.Collections;
using System.Xml.Serialization;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Contracts.ContractModel {
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace =
"http://tempuri.org/XMLSchema1.xsd")]
public partial class Definition {

private string definition1Field;
private string wordField;

[System.Xml.Serialization.XmlElementAttribute("Definition")]
public string Definition1 {
get {
return this.definition1Field;
}
set {
this.definition1Field = value;
}
}

[System.Xml.Serialization.XmlAttributeAttribute()]
public string Word {
get {
return this.wordField;
}
set {
this.wordField = value;
}
}

}
}
 
K

KWienhold

The problem here is that you've posted a bunch of sample code, but you
haven't explained to us *where* (at what line) of your code that you're
getting a null reference exception.
Suggestion:
for testing purposes, surround the entire code block with a try / catch
block and output the exception message and stacktrace to the console or to
Debug.WriteLine.
Then you will be able to pinpoint the problem by yourself. Don't forget to
turn on line numbering in the Editor.

A null reference exception means just what it says: somewhere in your code
you are attempting to do something with an object reference that is null --
in other words, there is no instance of an object in it.
-- Peter
Site:http://www.eggheadcafe.com
UnBlog:http://petesbloggerama.blogspot.com
Short Urls & more:http://ittyurl.net



Andy B said:
I am having a problem with a few classes of mine. I have the 3 classes below
(sorry if they are quite huge). When I do the following, I get a Object
reference set to null reference error. Any idea what might be going on? Just
a note that the whole Contract design isn't included since it would be very
huge in size. I also created this class setup with xsd (from an xsd fileI
created).
What I did:
//create a new contract
Contract Contract = new Contract();
//create some definitions (word/definition pairs)
Definition TermHost = new Definition();
TermHost.Word="Host";
TermHost.Definition1="The person or persons doing the hiring of the
artist/musician";
//add the definition to the definition cllection class
Contract.Definitions.Add(TermHost);
The classes used:
using System;
using System.Collections;
using System.Xml.Serialization;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Contracts.ContractModel {
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
  [System.SerializableAttribute()]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace
= "http://tempuri.org/XMLSchema1.xsd")]
  [System.Xml.Serialization.XmlRootAttribute(Namespace =
"http://tempuri.org/XMLSchema1.xsd", IsNullable = false)]
  public partial class Contract {
 private ContractDefinitions definitionsField;
   private ContractSections sectionsField;
   private Venue venueField;
   private Event eventField;
 private Host hostField;
 private LongDistanceTransport longDistanceTransportField;
 private LocalTransport localTransportField;
 private Lodging lodgingField;
 private Meals mealsField;
 private SignatureBlock signaturesField;
  public ContractDefinitions Definitions {
   get {
  return this.definitionsField;
  }
   set {
  this.definitionsField = value;
  }
   }
 public ContractSections Sections {
   get {
  return this.sectionsField;
  }
   set {
  this.sectionsField = value;
  }
   }
   public Venue Venue {
   get {
  return this.venueField;
  }
   set {
  this.venueField = value;
  }
   }
 public Event Event {
   get {
  return this.eventField;
  }
   set {
  this.eventField = value;
  }
   }
 public Host Host {
   get {
  return this.hostField;
  }
   set {
  this.hostField = value;
  }
   }
 public LongDistanceTransport LongDistanceTransport {
   get {
  return this.longDistanceTransportField;
  }
   set {
  this.longDistanceTransportField = value;
  }
   }
 public LocalTransport LocalTransport {
   get {
  return this.localTransportField;
  }
   set {
  this.localTransportField = value;
  }
   }
 public Lodging Lodging {
   get {
  return this.lodgingField;
  }
   set {
  this.lodgingField = value;
  }
   }
 public Meals Meals {
   get {
  return this.mealsField;
  }
   set {
  this.mealsField = value;
  }
   }
 public SignatureBlock Signatures {
   get {
  return this.signaturesField;
  }
   set {
  this.signaturesField = value;
  }
   }
   }
  }
using System;
using System.Collections;
using System.Xml.Serialization;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Contracts.ContractModel {
  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
  [System.SerializableAttribute()]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace
= "http://tempuri.org/XMLSchema1.xsd")]
  public partial class ContractDefinitions : CollectionBase {
 public void Add(Definition Definition) {
   List.Add(Definition);
   }
 public void Remove(int index) {
   // Check to see if there is a Definition at the supplied index.
   if(index > Count - 1 || index < 0)
   // If no Definition exists, a messagebox is shown and the operation
   // is cancelled.
      {
  throw new ArgumentException("Invalid index");
  } else {
  List.RemoveAt(index);
  }
   }
 public Definition Item(int index) {
   return (Definition)List[index];
   }
 private Definition termsField;
 public Definition Terms {
   get {
  return this.termsField;
  }
   set {
  this.termsField = value;
  }
   }
 }
  }
using System;
using System.Collections;
using System.Xml.Serialization;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Contracts.ContractModel {
   [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727..42")]
  [System.SerializableAttribute()]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(Namespace =
"http://tempuri.org/XMLSchema1.xsd")]
  public partial class Definition {
 private string definition1Field;
 private string wordField;
 [System.Xml.Serialization.XmlElementAttribute("Definition")]
 public string Definition1 {
   get {
  return this.definition1Field;
  }
   set {
  this.definition1Field = value;
  }
   }
 [System.Xml.Serialization.XmlAttributeAttribute()]
 public string Word {
   get {
  return this.wordField;
  }
   set {
  this.wordField = value;
  }
   }
       }
   }- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

As Peter pointed out, the way you present your problem isn't entirely
ideal. However, looking through the code, I think your problem may be
that you don't initialize the definitionsField in your Contract class
when you create an instance of it, so
"Contract.Definitions.Add(TermHost);"
would cause a null reference exception since Contract.Definitions is
null.

hth,
Kevin Wienhold
 
I

Ignacio Machin ( .NET/ C# MVP )

I am having a problem with a few classes of mine. I have the 3 classes below
(sorry if they are quite huge). When I do the following, I get a Object
reference set to null reference error. Any idea what might be going on? Just
a note that the whole Contract design isn't included since it would be very
huge in size. I also created this class setup with xsd (from an xsd file I
created).

First of all, nobody will read all that code, not only that but you
failed to say where and when you get the exception, if you provide the
line relevant we can try to guess which is the isntance that is null.

But in anycase this exception is self explanatory, you are failing to
initialize one instance of a class. See what isntance is null and make
sure that it's created before using its methods.
 

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