Chapter Index
premise
I have always felt that how to debug a program should be the first lesson in learning programming. Because I have seen many beginners, if it is written according to the tutorial, it is fine, once a bug occurs, they will be at a loss. Why can't it run? How can I make it work? I don't know anything.
1. The bash commands to be used in this article
- pwd: Get the absolute path of the current directory
- cd: enter/exit folder
- ls: Display all files and folder names in the current folder
- vim: text editing and browsing tool
2. Query log files
The running log of the tomcat server is stored in the %TOMCAT_HOME%/logs folder. The running log of the program is generally in the localhost.YYYY-MM-DD.log file. To find the log of which day, just follow the log to view it.
For example, on 2020/11/06, our program did not normally return a successful or failed login response, so we have to go to localhost.2020-11-06.log to check the operation log.
vim localhost.2020-11-06.log
Enter browse mode to browse log files.
The important information in the log has four parts circled in the red box above
- Timepoint: The timepoint corresponds to the time when you performed the test.
- Path: indicates the path in the request, if multiple web applications are deployed at the same time, it can be distinguished by this.
- Exception Type: Indicates what kind of exception occurred (null pointer exception in the example).
- The call stack where the exception occurred: which line of the method of the specific class throws the exception, the higher the position is, the lower the call occurs, that is, the starting position where the exception is thrown layer by layer.
After reading the log file, we exit the vim editor:
enter :q
, press Enter
the key.
3. Anti-check code
From the log file, we know that the first line of the method UserDAO
of the class throws a null pointer exception. At this point, we know through inquiry that the null pointer exception is generally caused by our reference to its members for null in the code. It may be the null created in this method, or it may be that the method entry has not been checked, and the null passed in by the calling method of the previous layer is accepted.queryUser
33
NullPointerException
From this, we can go to the corresponding location in the code to see what caused the program to throw a null pointer exception.
postscript
Just like a driver, if he knows some troubleshooting knowledge before driving, he will not panic when the car breaks down halfway, but can quickly determine the problem and prescribe the right medicine. Seeing this, some people may be confused: I haven't started writing programs yet, so you teach me how to find bugs? In fact, I just want to give you an impression in advance. Here is a blog post teaching how to analyze exceptions. I hope you won't use it, but it's impossible. How can you learn something without stepping on the pit? If you encounter an exception, you can analyze and solve it according to the method here.