Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java code evaluation (IntelliJ IDE), use toString() in some point?

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?

like image 722
Johnny Avatar asked Jan 06 '14 12:01

Johnny


People also ask

How do I disable toString in Intellij?

The solution was simply to uncheck "Enable 'toString()' object view" -> Apply -> re-enable the same option -> apply and they're back!

How do I automatically set getters and setters in Intellij?

You can use ⌘N (macOS), or Alt+Insert (Windows/Linux) for the Generate menu and then select Constructor , Getter , Setter or Getter and Setter .


Video Answer


1 Answers

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

like image 52
Johnny Avatar answered Sep 22 '22 19:09

Johnny