5

I am migrating my application from JDK 7 to JDK 8. While doing, I am facing an exception com.sun.jdi.InvocationException occurred invoking method when an instance of TestField as shown below is created. I am getting the exception while debugging and could not find the reason for it. I suspect NullPointerException occurs and InvocationException masks it. I have the below Object methods overridden in TestField.

Below utility classes are a part of commons-lang jar.

HashCodeBuilder
EqualsBuilder
ToStringBuilder

public class TestField {

private String name;

private Rules rules;

public TestField(String name, Rules rules)

{   this.name = name;
    this.rules = rules;
}

public String toString() {
    return new ToStringBuilder(this)
    .append("\n name", this.getName())
    .append("\n Rules", this.getRules())
    .append("\n ")
    .toString();
}

public boolean equals(Object other) {
    if ( !(other instanceof TestField) ) return false;
    TestField castOther = (TestField) other;
    return new EqualsBuilder()
        .append(this.getName(), castOther.getName())
                .append(this.getRules(), castOther.getRules())
        .isEquals();
}

public int hashCode() {
    return new HashCodeBuilder()
        .append(this.getName())            
        .append(this.getRules())            
        .toHashCode();
}   
}

Have anyone faced such an issue. Could anyone please help me to resolve the same. Thanks.

sridhar
  • 1,117
  • 5
  • 31
  • 59

2 Answers2

2

Although I have not run into this particular issue I have had my fair share of migration issues that were caused by little changes that got masked by later processes.

I was going to suggest backtracking and looking at your toString method but it seems several people have had this same exact problem already; toString or your toHashCode is the most likely culprits. In a nut shell a null pointer exception is most likely being thrown but masked by the com.sun.jdi.InvocationException error. So if you are getting a Null Pointer Exception chances are there still was something that happened before that but was masked. Just take parts of your code out and work them back in step by step.

Here is the other question and answers that I think will solve this (I do not have the reputation to mark as a duplicate):

com-sun-jdi-invocationexception occurred invoking method

Also take a look at this question, particularly the comment by Robin Green, have you tried debugging this code that way?

Example

Community
  • 1
  • 1
Blizzardengle
  • 992
  • 1
  • 17
  • 30
  • 1
    Many thanks for the response.. Blizzardegle. It was NullPointerException that was masked by InvocationException in toString() implementation of any class. It is working fine with JDK 7 but not with JDK8. In toString(), looping on an empty string array without null check worked fine in JDK7 but not in JDK8. So, it seems null check is required for all string operartions in JDK8. – sridhar Jul 22 '15 at 16:05
0

You might want to check the hashcode() of your class. If by any way it can get stuck in a nullPointer Exception, Mockito will not be able to invoke the method. For example, if the hashCode() has been implemeted using arguments passed int he constructor of your class, and any of them is null.

PipoTells
  • 511
  • 6
  • 11