Null pointer exception problem!!

Hi,

Getting a null pointer exception when i call this method...

/**

* This will determine the difference between the year built and year sold

*/

privateboolean yearDifference()

{

Calendar cal1 =new GregorianCalendar();

cal1.setTime(dateBuilt);

Calendar cal =new GregorianCalendar();

cal.setTime(soldDate);

int sold = cal.get(Calendar.YEAR);

int build = cal1.get(Calendar.YEAR);

if(sold == build)

{

returntrue;

}else{

returnfalse;

}

}

The error is on line cal.setTime(soldDate)

my constructor is..

public House(String id, String address,double sellingPrice,int year,int month,int day)

{

this.id = id;

this.address = address;

this.sellingPrice = sellingPrice;

isSold =false;

Calendar cal =new GregorianCalendar(year, month - 1, day);

Calendar cal2 =new GregorianCalendar();

dateBuilt = cal.getTime();

df = DateFormat.getDateInstance(DateFormat.LONG);

nf = NumberFormat.getCurrencyInstance();

Could someone help please. Thank you

[2021 byte] By [Capsuda] at [2007-11-27 3:42:51]
# 1

You have dateBuilt initiated, but soldDate is not, at least not in the code you provided. Wrap yearDifference in a try catch block and print the stack trace. It will make it easier to debug and point to the line the code breaks.

Also, If you have, dateBuilt already assigned, why create another object holding the same information?

kdajania at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 2

int sold = cal.get(Calendar.YEAR);

int build = cal1.get(Calendar.YEAR);

if(sold == build)

{

return true;

} else {

return false;

}

Prefer

return calSold.get(Calendar.YEAR) == calBuilt.get(Calendar.YEAR);

DrLaszloJamfa at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 3

> You have dateBuilt initiated, but soldDate is not, at

> least not in the code you provided. Wrap

> yearDifference in a try catch block and print the

> stack trace. It will make it easier to debug and

> point to the line the code breaks.

>

> Also, If you have, dateBuilt already assigned, why

> create another object holding the same information?

How do i go about putting yearDifference in a try catch block?

Capsuda at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 4

> > int sold = cal.get(Calendar.YEAR);

> int build = cal1.get(Calendar.YEAR);

> if(sold == build)

> {

>return true;

> se {

>return false;

>

> de]

> Prefer

> [code]

> return calSold.get(Calendar.YEAR) ==

> calBuilt.get(Calendar.YEAR);

>

Are you suggesting i take out my code and replace it with your piece? Is that better code?

Capsuda at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 5
Maybe it's a matter of style, but to me, one straightforward line beats 8 lines of code.
DrLaszloJamfa at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 6

/**

* This will determine the difference between the year built and year sold

*/

private boolean yearDifference()

{

try {

// .... your code

}

catch (Exception e) {

e.printStackTrace();

}

// .... code

}

Keep in mind though, the return statement has to be outside of the try-catch block.

kdajania at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 7

[code]

/**

* This will determine the difference between the year built and year sold

*/

private boolean yearDifference()

{

Calendar cal = new GregorianCalendar();

cal.setTime(dateBuilt);

Calendar cal2 = new GregorianCalendar();

cal2.setTime(soldDate);

int sold = cal2.get(Calendar.YEAR);

int build = cal.get(Calendar.YEAR);

if(sold == build)

{

return cal2.get(Calendar.YEAR) == cal.get(Calendar.YEAR);

}

}

[code\]

I'm getting 'missing return statement' now

Message was edited by:

Capsud

Capsuda at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 8

Yikes. Don't blindly half-copy code. Think it through.

You wrote:

int sold = cal2.get(Calendar.YEAR);

int build = cal.get(Calendar.YEAR);

if(sold == build)

{

return cal2.get(Calendar.YEAR) == cal.get(Calendar.YEAR);

}

Replace all that with

return cal2.get(Calendar.YEAR) == cal.get(Calendar.YEAR);

Suggestion: even give local variables better names than cal and cal2.

DrLaszloJamfa at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 9

Will this method still work if do those changes?

/**

* Calculate price of house

*/

public void calculatePrice()

{

if(buyer.getFirstTime() == false)

{

stampDuty = .10;

sellingPrice = sellingPrice * stampDuty;

} else {

if(yearDifference() == true)

{

sellingPrice = sellingPrice * stampDuty;

} else {

// nothing happens here

}

}

}

Basically stamp duty is payable on a house at 10%, unless the house was built in the current year or the buyer is a first time buyer.

That is what i am trying to achieve with the two methods.

Capsuda at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 10

would this be simpler?

public void calculatePrice()

{

if(!buyer.getFirstTime() && yearDifference())

{

stampDuty = .10;

sellingPrice = sellingPrice * stampDuty;

}

}

also, I'm not sure your logic above is correct since it will assess the stamp duty if both it is the buyer's first time and the house was built more than a year ago. Is this what you want to have happen?

Message was edited by:

petes1234

petes1234a at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 11
Backing up a bit, is yearDifference() supposed to be true if the years are different or if the years are the same?
DrLaszloJamfa at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 12
yearDifference is supposed to be 'true' if the house was sold in a different year than it was built. Then false if it was built in the same year.
Capsuda at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 13

I'm having trouble with my 'calculatePrice()' method. It works properly if the buyer is a first time buyer then the stamp duty is brought in correctly but if the house was built in a different year then it dosen't work.

This is my code for the two methods.

/**

* This will determine the difference between the year built and year sold

*/

private boolean yearDifference()

{

Calendar cal = new GregorianCalendar();

cal.setTime(dateBuilt);

Calendar cal2 = new GregorianCalendar();

cal2.setTime(soldDate);

return cal2.get(Calendar.YEAR) == cal.get(Calendar.YEAR);

}

/**

* Calculate price of house

*/

public void calculatePrice()

{

if(buyer.getFirstTime() == false)

{

stampDuty = 1.10;

sellingPrice = sellingPrice * stampDuty;

} else {

if(yearDifference() == true)

{

sellingPrice = sellingPrice * stampDuty;

} else {

// nothing happens here

}

}

}

Can someone help? Thanks

Capsuda at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 14
> yearDifference is supposed to be 'true' if the house was sold in a different year than it was built. Then false if it was built in the same year.And is that the way it is defined?
DrLaszloJamfa at 2007-7-12 8:46:28 > top of Java-index,Java Essentials,New To Java...
# 15

> I'm having trouble with my 'calculatePrice()' method.

> It works properly if the buyer is a first time buyer

> then the stamp duty is brought in correctly but if

> the house was built in a different year then it

> dosen't work.

Have you tried my rendition? Does it work? if not, please specify as clearly as possible your specifications.

petes1234a at 2007-7-21 20:52:54 > top of Java-index,Java Essentials,New To Java...