Hallo,
let's say that I have a class Person that has some attributes like name, partner (another Person), mother (another Person), etc. The class refers to itself and I would write something like this:
now I would like to create a method that makes two persons refer to each other, like marriedTo(Person):
I wrote this code:
If I instanciate two persons and make them married I get an a NullPointerException. I don't understand why. Does someone has an hint? thanks
let's say that I have a class Person that has some attributes like name, partner (another Person), mother (another Person), etc. The class refers to itself and I would write something like this:
Code:
abstract class Person
{
public String name = "<unknown>";
private Person father;
private Person mother;
private Person partner;
public Person(String s1) {name = s1;}
}
I wrote this code:
Code:
public void marriedTo (Person p1)
{
try
{
if (this.partner != null || p1.partner != null)
{
throw new BigamyException();
}
else
{
this.partner.name = p1.name;
p1.partner.name = this.name;
}
}
catch (BigamyException e)
{
System.out.println("Illegal marriage");
}
}
If I instanciate two persons and make them married I get an a NullPointerException. I don't understand why. Does someone has an hint? thanks