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]

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?
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);
> 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?
> > 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?
Maybe it's a matter of style, but to me, one straightforward line beats 8 lines of code.
/**
* 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.
[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
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.
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.
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
Backing up a bit, is yearDifference() supposed to be true if the years are different or if the years are the same?
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.
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
> 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?
> 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.