1

I'm having an object named data,which belongs to the class,

[DataContract]
public class names
{
    [DataMember(Name = "code")]
    public int Code { get; set; }

    [DataMember(Name = "message")]
    public string Message { get; set; }

    [DataMember(Name = "values")]
    public values values{ get; set; }
}

where values is another class with variables of its own, initially while executing the following code,

names data= new names();
data.values.Name = "";

I get a null exception when executing the second line.

What is the issue?Why data.values.Name is still null even after executing

names data= new names();

what should i do to give it some value?

Tilak
  • 30,108
  • 19
  • 83
  • 131
Aju
  • 796
  • 3
  • 10
  • 29
  • 22
    Good heavens, don't name your class `object`! – Kirk Woll Oct 24 '13 at 14:28
  • 1
    calling a class object is a bad idea. It makes it very easy to get confused with the type [object](http://msdn.microsoft.com/en-us/library/vstudio/9kkx3h3c.aspx) – Liam Oct 24 '13 at 14:28
  • `public values values` is that what you want? what is values? – Jonesopolis Oct 24 '13 at 14:29
  • 3
    I'm surprised that C# allows the naming of a class as object. – Cameron Tinker Oct 24 '13 at 14:29
  • 2
    Your not instantiating values, so it's null – Liam Oct 24 '13 at 14:29
  • 3
    You should probably need to read [Naming Guidelines](http://msdn.microsoft.com/en-us/library/xzf533w0%28v=vs.71%29.aspx)!! – huMpty duMpty Oct 24 '13 at 14:29
  • use the naming guidlines on you `public values values` as well... – Mr.GT Oct 24 '13 at 14:30
  • 1
    (the class is named `names` now, so this isn't really an issue, but just to clear something up for all the people commenting on this) Since `object` is a keyword, you can't use it as a class name unless you make it `@object`. That's still rather confusing, and things like that could be more problematic if your class might be used from other .NET languages, which might not support that sort of syntax. – Tim S. Oct 24 '13 at 14:38
  • @TimS. Thanks for the clarification. I was curious about how code would compile with a class named `object`. It seems as though the question was edited to make the class name `names` but it doesn't show any edit history. – Cameron Tinker Oct 24 '13 at 14:42
  • i just gave it a dummy name! my class is not named object, if so i could have compiled? It was not the error anyhow!! – Aju Oct 25 '13 at 06:43

4 Answers4

7

Initialize values also.

names data= new names();
data.values = new values();
Tilak
  • 30,108
  • 19
  • 83
  • 131
3

Aside from not naming your class object which is just asking for trouble. You can't access properties on values because you never initialized it. Your class needs a constructor where values is set to something. Or else you need to set values to something before you try and access it.

So either this:

[DataContract]
public class object
{
    [DataMember(Name = "code")]
    public int Code { get; set; }

    [DataMember(Name = "message")]
    public string Message { get; set; }

    [DataMember(Name = "values")]
    public values values{ get; set; }

    public object()
    {
        values = new values();
    }
 }

Or you could do:

 object data= new object();
 data.values = new values();
 data.values.Name = "";

Without context it's difficult to say which suits you better.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
1

Ok, I'll stat by guessing the name object is only a bad name choice to illustrate your problem (as other said, you don't want to choose this name (if permitted).

The problem you are facing is that the Values property is not initialized, thus giving you an exception. Here is a sample of what you can do to avoid it:

public class MyObject
{
    public MyObject()
    {
        this.Values = new Values();
    }

    public int Code { get; set; }
    public string Message { get; set; }
    public Values Values { get; set; }
}

public class Values
{
    public string Name { get; set; }
}

As you can see, I initialize the Values property in the constructor of yout object.

Shimrod
  • 3,115
  • 2
  • 36
  • 56
0

taking your class:

[DataContract]
//I've renamed it to stop it hurting my eyes.
public class myClass
{
    [DataMember(Name = "code")]
    public int Code { get; set; }

    [DataMember(Name = "message")]
    public string Message { get; set; }

    [DataMember(Name = "values")]
    public values values{ get; set; }
}

If you change

myClass data= new myClass();
data.values.Name = "";

to:

myClass data= new myClass();
data.values = new values();
data.values.Name = "";

this will now work.

presuming:

public class values
{
   //empty constructor??
   public values()
   {

   }
   public string Name { get; set; }
}
Liam
  • 27,717
  • 28
  • 128
  • 190