Search code examples
javastack-overflowjodatimejoda-convert

java.lang.StackOverflowError at org.joda.time.chrono.BasicChronology.getYear(BasicChronology.java:426)


Could someone tell me what I am doing wrong for getting this StackOverFlowError with Joda library?

Here the code implyed:

  public Integer getAge() {
    if ( getBirthDate() != "//" ) {
        try {
            LocalDate birth = LocalDate.parse( getBirthDate(), DateTimeFormat.forPattern( "dd/MM/yyyy" ) );//Error raised here
            DateTime today = new DateTime();
            if ( today.getMonthOfYear() >= birth.getMonthOfYear() ) {
                age = today.getYear() - birth.getYear();
            } else {
                age = today.getYear() - birth.getYear() - 1;
            }
        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }
    return age;
}

Here where I call this method :

@Override
public boolean equals( Object obj ) {

    if ( this == obj ) {
        return true;
    }
    if ( obj == null ) {
        return false;
    }
    if ( !( obj instanceof Identite ) ) {
        return false;
    }

    Identity other = (Identity) obj;
    EqualsBuilder equalsBuilder = new EqualsBuilder();

    equalsBuilder.append( getAge(), other.getAge() );//here the call
    return equalsBuilder.isEquals();
}

I am using the getter method for Hibernate.

How to avoid this error?

The stack trace :

java.lang.StackOverflowError
at org.joda.time.chrono.BasicChronology.getYear(BasicChronology.java:426)
at org.joda.time.chrono.BasicGJChronology.setYear(BasicGJChronology.java:180)
at org.joda.time.chrono.BasicYearDateTimeField.setExtended(BasicYearDateTimeField.java:92)
at org.joda.time.format.DateTimeParserBucket$SavedField.set(DateTimeParserBucket.java:568)
at org.joda.time.format.DateTimeParserBucket.computeMillis(DateTimeParserBucket.java:447)
at org.joda.time.format.DateTimeParserBucket.computeMillis(DateTimeParserBucket.java:411)
at org.joda.time.format.DateTimeFormatter.parseLocalDateTime(DateTimeFormatter.java:887)
at org.joda.time.format.DateTimeFormatter.parseLocalDate(DateTimeFormatter.java:844)
at org.joda.time.LocalDate.parse(LocalDate.java:179)
at com.home.entities.Identity.getAge(Identite.java:127)
at com.home.entities.Identity.equals(Identite.java:185)

EDIT Joda dependencies and getBirthDate():

   <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
       <version>2.9.7</version> 
    </dependency>
    <dependency>
        <groupId>org.joda</groupId>
        <artifactId>joda-convert</artifactId>
        <version>1.8.1</version>
    </dependency>

The method is:

public String getBirthDate() {
    if ( getBirthDay() != null && getBirthMonth() != null && getBirthYear() != null ) {
        birthDate= getBirthDay()+ "/" +  getBirthMonth() + "/" + getBirthYear();
    }
    return birthDate;
}

Solution

  • I found the reason of this exception, I just forgot to load the required data from database.