38

When evaluating object initialization, toString nullPointerException appear.

Method threw 'java.lang.NullPointerException' exception. Cannot evaluate XXXX.toString()

Code example which recreates this issue:

Car car = carUtils.objectMapper.readValue(new File(pathTo("carProperties.json")), Car.class); 

Why is that happens? is the evaluation done using toSting() invocations?

Johnny
  • 14,397
  • 15
  • 77
  • 118
  • 2
    I had the same error message today. It turns out Intellij is trying to show an object with a null field, the toString of that object threw a null pointer exception. Modified the toString method for that class and the problem went away. – Guangliang May 03 '17 at 21:44

2 Answers2

56

I have discovered the root cause for this behavior.

You can choose in what way IntelliJ evaluation will display the data: the result can be the Object or toString evaluation. Therefore the error i had experienced, View as->toString was used, while not all the objects had toString implemented. Hope it will help someone in the future.

enter image description here

You can update the display type for a variable by right clicking on the variable (inside the variables window of the debugger) and clicking on 'View as'

If you don't want to update it manually each time, you can change it in IntelliJ properties following this steps

Johnny
  • 14,397
  • 15
  • 77
  • 118
1

Maybe you override the hashCode() method and hashCode() return null.

logbasex
  • 1,688
  • 1
  • 16
  • 22