com.sun.jdi.InvocationException occurred invoking method
Asked Answered
C

16

81

I just want to create an object of class, but got this error when debugging. Can anybody tell me what the problem is? The location of this code is in some Spring(2.5) Service class.

There is a similar problem: OJB Reference Descriptor 1:0 relationship? Should I set auto-retrieve to false?

Thanks a lot~

Conceal answered 8/11, 2010 at 12:2 Comment(3)
It's kinda hard to guess what your problem is without any source code or stack trace.Tessellated
I figured it out, the toString() method of the class may throw NullPointerException if invoke,when I correct it, the error disappeared,but I don't konwn how this happend?Conceal
Nice work xyz. Can you please post an answer to the question yourself and then accept that answer so that we can close this question? Also, you need to accept answers to previous questions if they fix your problem.Jackstraws
R
99

The root cause is that when debugging the java debug interface will call the toString() of your class to show the class information in the pop up box, so if the toString method is not defined correctly, this may happen.

Rouse answered 28/6, 2012 at 8:25 Comment(3)
What if the toString method isn't defined at all? I'm getting it in the RestTemplate class of the Spring jar, that's one of the core classes, and it doesn't have toString() method. Still I'm getting this exception from another class where I'm calling RestTemplate.getForObject(). I'm clueless what's happening.Sauropod
@SaurabhPatil toString is always defined as all objects extend java.lang.Object. One possibility with Spring is that scoped beans are proxied if they are injected into singletons. If toString is called on the wrong thread(e.g. one not associated with request context) then the proxy would throw an exception when calling toString on it.Head
Minor correction: AFAICT the JDI will not ever call toString in the debugee on its own. What people are seeing here is a (specific) debugger using the JDI to do invokeMethod on toString on some debugee objects, which is in fact a fairly brittle approach to inspect stuff. Eclipse moved from their old debugger to something they call JDT since this answer was written. IDK if the latter still uses invokeMethod on toString.Ger
Q
31

I also had a similar exception when debugging in Eclipse. When I moused-over an object, the pop up box displayed an com.sun.jdi.InvocationException message. The root cause for me was not the toString() method of my class, but rather the hashCode() method. It was causing a NullPointerException, which caused the com.sun.jdi.InvocationException to appear during debugging. Once I took care of the null pointer, everything worked as expected.

Quadrivium answered 4/7, 2013 at 13:41 Comment(0)
F
18

Well, it might be because of several things as mentioned by others before and after. In my case the problem was same but reason was something else.

In a class (A), I had several objects and one of object was another class (B) with some other objects. During the process, one of the object (String) from class B was null, and then I tried to access that object via parent class (A).

Thus, console will throw null point exception but eclipse debugger will show above mentioned error.

I hope you can do the remaining.

Fishplate answered 12/9, 2014 at 11:50 Comment(1)
@Fishplate This is the same issue i am facing.How to fix this?Corina
C
6

For me the same exception was thrown when the toString was defined as such:

@Override
public String toString() {
    return "ListElem [next=" + next + ", data=" + data + "]";
}

Where ListElem is a linked list element and I created a ListElem as such:

private ListElem<Integer> cyclicLinkedList = new ListElem<>(3);
ListElem<Integer> cyclicObj = new ListElem<>(4);
...

cyclicLinkedList.setNext(new ListElem<Integer>(2)).setNext(cyclicObj)
    .setNext(new ListElem<Integer>(6)).setNext(new ListElem<Integer>(2)).setNext(cyclicObj);

This effectively caused a cyclic linked list that cannot be printed. Thanks for the pointer.

Chose answered 10/4, 2014 at 0:39 Comment(0)
J
6

I was facing the same issue because I was using Lombok @Data annotation that was creating toString and hashcode methods in class files, so I removed @Data annotation and used specific @Gettter @Setter annotation that fixed my issue.

we should use @Data only when we need all @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor.

Junkie answered 26/6, 2018 at 6:9 Comment(1)
it resolved my issue. ThanksMisti
D
3

so I had same problem here. Found out that my domain instance was getting detached from the hibernate session. I used isAttached() to check and attached the domain using d.attach()

Doublepark answered 27/9, 2013 at 15:47 Comment(0)
S
3

Disabling 'Show Logical Structure' button/icon of the upper right corner of the variables window in the eclipse debugger resolved it, in my case.

Subfloor answered 4/12, 2013 at 19:0 Comment(0)
G
3

I had the same issue once. In my case toString() method was badly created. TO be precise a static final variable was included in the toString method when a developer form my team was assigned code cleaning task and to add toString(), hashCode() code and equals() methods to domain objects where ever possible. but in of the classes because of over looking at it, he included final static variable that caused the "com.sun.jdi.InvocationException" this exception was visible on debugging only when I hovered over the object which has the exception.

Gamble answered 25/6, 2015 at 15:1 Comment(0)
M
3

This was my case

I had a entity Student which was having many-to-one relation with another entity Classes (the classes which he studied).

I wanted to save the data into another table, which was having foreign keys of both Student and Classes. At some instance of execution, I was bringing a List of Students under some conditions, and each Student will have a reference of Classes class.

Sample code :-

Iterator<Student> itr = studentId.iterator();
while (itr.hasNext()) 
{
    Student student = (Student) itr.next();
    MarksCardSiNoGen bo = new MarksCardSiNoGen();

    bo.setStudentId(student);

    Classes classBo = student.getClasses();

    bo.setClassId(classBo);
}

Here you can see that, I'm setting both Student and Classes reference to the BO I want to save. But while debugging when I inspected student.getClasses() it was showing this exception(com.sun.jdi.InvocationException).

The problem I found was that, after fetching the Student list using HQL query, I was flushing and closing the session. When I removed that session.close(); statement the problem was solved.

The session was closed when I finally saved all the data into table(MarksCardSiNoGen).

Hope this helps.

Misericord answered 1/12, 2016 at 15:6 Comment(0)
W
3

I got similar exception in Eclipse. This was due to java.lang.StackOverflowError error. I had overriden toString() method in child class, having JoinColumn, which was returning string using object of parentclass, resulting in circular dependency. Try to remove that object from toString(), and it will work.

Wilmington answered 6/6, 2017 at 12:58 Comment(0)
G
1

Removing hashCode() and equals() solved my issue. In my case, I used Apache's commons-lang hash code and equals builders for creating non-static classes manually, so the compiler didn't throw any exception. But at runtime it caused the invocation exception.

Gamble answered 19/5, 2015 at 16:28 Comment(0)
B
1

I have received com.sun.jdi.InvocationException occurred invoking method when I lazy loaded entity field which used secondary database config (Spring Boot with 2 database configs - lazy loading with second config does not work). Temporary solution was to add FetchType.EAGER.

Banquer answered 24/1, 2018 at 14:27 Comment(0)
R
0

There could be two reasons an element doesn't exist:

  1. Bad xpath (//*[@id'forgotQuote])
  2. Correct xpath but no element (//*[contains(text(),'This text is not in the page')])

Would you get com.sun.jdi.InvocationException in either case when you are running Debug and you hover your mouse over a reference to the WeBElement (this with Selenium and Java)???

We use the following, but can't distinguish if it returns false due to bad xpath or non-existent element (valid xpath syntax):

public static boolean isElementDisplayed(WebElement element) {
    boolean isDisplayed = false;

    try {
        isDisplayed = element.isDisplayed();
    } catch (NoSuchElementException e) {
        ;// No Worries
    }
    return isDisplayed;
}
Recrudesce answered 16/10, 2014 at 18:59 Comment(0)
C
0

In my case it was due to the object reference getting stale. I was automating my application using selenium webdriver, so i type something into a text box and then it navigates to another page, so while i come back on the previous page , that object gets stale. So this was causing the exception, I handled it by again initialising the elements - PageFactory.initElements(driver, Test.class;

Crabtree answered 3/2, 2016 at 11:10 Comment(0)
E
0

I also faced the same problem. In my case I was hitting a java.util.UnknownFormatConversionException. I figured this out only after putting a printStackTrace call. I resolved it by changing my code as shown below.

from:

StringBuilder sb = new StringBuilder();
sb.append("***** Test Details *****\n");
String.format("[Test: %1]", sb.toString());

to:

String.format("[Test: %s]", sb.toString());
Ewers answered 11/2, 2019 at 7:38 Comment(0)
B
-1

I faced the same problem once. In my case it was because of overridden equals method. One values was coming null.

Broken answered 30/3, 2018 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.