background
Customer service students feedback when using a function, the page prompts an exception.
As usual, my friend responded positively, first scrolling through the log, finding abnormal information, locating to which area, and then… (Give feedback results)
A small partner looked for a while, how to see all feel no problem, is preparing to catch the bus, a small partner called slag yong also look at this section of logic, probably aimed at two eyes did not find the problem (mainly too vegetables), first withdrew…
Beyond coding specifications for the moment…
The body of the
At a loss
On the way back to that logic, as if there is no problem… Believe that diligent can make up the dregs yong, back or decided to turn over the code
Check the log information of the offline exception:
Class name + number of lines Follow the usual routine, find the corresponding code… (Make sure the code is consistent with the online version)
Exception line 563 is as follows:
This makes it look like there really isn’t anything wrong with this line of code (there really is!).
The class file is down, as shown in the red box:
That’s weird. Is the code really toxic?
2. The End of the Road
But now I have to pull out my great sword, 40 meters long — Arthas!
First,traceThe following corresponding method, the page to simulate the request:
Ok, obviously, the null pointer is out!
Then use jad to decomcompile the corresponding method:
See the red box, can not sigh, what the help (Fxxx)?
By now, the reason is obvious. NPE occurs when the object is not empty and the property is empty;
If you’re like me, you might wonder why integer.value is included in decompilation. OK, it’s the DEFAULT in Figure 2.
private static final int DEFAULT = 0;
Copy the code
The integer. value method is used because the object is Integer and involves boxing. (This shows how unreliable the class file is in the package)
3. Problems recur
To verify the above problem, I wrote a demo to duplicate it:
** * Author: bingo624 * Date: 2020/9/17 23:23 * Description: * version: 1.0 */
@RestController
public class HelloController {
private static final int DEFAULT = 0;
@RequestMapping("/hello1")
public StudentVo hello1(a){
PhxUserDto phxUserDto = new PhxUserDto();
StudentVo vo = new StudentVo(phxUserDto == null ? DEFAULT : phxUserDto.getGender());
return vo;
}
@RequestMapping("/hello2")
public StudentVo hello2(a){
PhxUserDto phxUserDto = new PhxUserDto();
StudentVo vo = new StudentVo(phxUserDto == null ? null : phxUserDto.getGender());
return vo;
}
@RequestMapping("/hello3")
public StudentVo hello3(a){
PhxUserDto phxUserDto = new PhxUserDto();
StudentVo vo = new StudentVo(phxUserDto == null ? Integer.valueOf(0) : phxUserDto.getGender());
returnvo; }}Copy the code
Combined with the above introduction, can you guess whether they all return normally?
As smart as you are, as expected, the request /hello1 will appear as NPE returns 500
/hello2 and /hello3 respond properly!
Finally, look at the class file in the jar package, and the decomcompiled class online:
Class file in jar package:
After online decompilation:
4. There’s a silver lining
Read the Ali statute:(Unpacking and packing can cause NPE)
The website guidance: docs.oracle.com/javase/spec…
conclusion
Through a case, it shows the importance of basic knowledge; Code writing specification, can avoid a lot of problems;
It is normal to make mistakes, but what should be considered more is: how to solve problems through the various tools at your command, and not to repeat mistakes!
Finally amway wave arthas, how sweet! (There are many more advanced features to discover…)