1

I have a Mainclass in my code with various a list and various subset. When I try

main k = new main();
k.main.addressinfo.addressline1 = "XXX";

I am getting the error "Object reference not set to an instance of an object// NULLEXCEPTION."

public class Mainclass
{
    public List<main> mainset { get; set; }
 // do sth to load and save model info
}
 public class main
{
    public personalinfo info { get; set; }
    public addressinfo currentaddr { get; set; }
    public addressinfo[] otheraddr { get; set; }
    public telephone currenttel { get; set; }
    public telephone[] othertel { get; set; }
 }
public class addressinfo
{
    public string Addressline1 { get; set; } 
    public string Addressline2 { get; set; }
    public string City { get; set; }
    public string postcode { get; set; 
}
public class telephone
{
    public int tel { get; set; }
}

Since the Class contains lists and arrays I am a little confused as to how to set the default values of string to EMPTY but not NULL. Also How do I ensure that the Childrens by default have One EMPTY but not NULL Object?

0

3 Answers 3

3

You need to initialize your objects

public personalinfo info = new personalinfo();
public addressinfo currentaddr = new addressinfo();
public telephone currenttel = new telephone();
Sign up to request clarification or add additional context in comments.

4 Comments

I know but what about the Arrays? Do I need to initialize them as well?
Yes, it would help. Like this: public addressinfo[] otheraddr = new addressinfo[2] {new addressinfo(), new addressinfo()}; All classes need to be initialized, structs do not. (EG, int and string are both structs)
@mcmonkey4eva System.String is a class, not a struct.
@Lukazoid - ... oh yeah, it is. Woops. Wasn't thinking. Yeah strings need to be initializing too, but it'll let you get away with just public string mystring = ""; instead of new string(); or anything like that.
0

You have to define constructors for your classes and inside them set the properties to their default values.

Comments

0

@Using mcmonkey4eva Answer

public personalinfo info = new personalinfo();
public addressinfo currentaddr = new addressinfo();
public telephone currenttel = new telephone();

public addressinfo[] otheraddr = new addressinfo[1];
public List<main> mainset = new List<main>();

4 Comments

If mcmonkey4eva's answer was the one that helped you the most, you should accept the answer.
What Tim said - also, public addressinfo[] otheraddr = new addressinfo[1]; should probably be public addressinfo[] otheraddr = new addressinfo[1] { new addressinfo(); };, otherwise it will be an array containing only a null.
Didnt not realise that the Correct symbol was to mark as answer :
Changed arrays to list to overcome problems that Tim pointed out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.