IntelliJ IDEA Debug调试案例二
2017-09-14 15:41
811 查看
第一个debug
代码块
Run
分析错误
Debug
分析错误
正确代码
第二个debug
代码块
Run
分析错误
Debug
分析错误
正确代码
日期解析异常
图1:
图2:
当时不理解为什么会出错,于是选择debug运行
new SimpleDateFormat( ” yyyy-MM-dd HH:mm:ss ” );
空指针异常
图2:
代码如下:
再次debug运行后:
此时logs不再是为null
代码块
Run
分析错误
Debug
分析错误
正确代码
第二个debug
代码块
Run
分析错误
Debug
分析错误
正确代码
第一个debug
java.text.ParseException: Unparseable date日期解析异常
代码块
@RunWith(SpringRunner.class) @SpringBootTest public class LogParserTest { SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " ); @Autowired LogParser logParser; List<LogModel> logs = null; @Before public void before() { logs = new ArrayList<>(); try { LogModel logModel1 = new LogModel(sdf.parse("2017-09-07 11:32:43"), "Notepad_AddLogmessage execution started-4"); LogModel logModel2 = new LogModel(sdf.parse("2017-09-01 10:32:43"), "Notepad_AddLogmessage execution-3"); LogModel logModel3 = new LogModel(sdf.parse("2017-01-07 13:32:43"), "Notepad_AddLogmessage execution-1"); LogModel logModel4 = new LogModel(sdf.parse("2017-01-07 16:32:43"), "Notepad_AddLogmessage execution ended-2"); logs.add(logModel1); logs.add(logModel2); logs.add(logModel3); logs.add(logModel4); } catch (Exception e) { e.printStackTrace(); } } @Test public void parse() throws Exception { //logParser.parse(logs); System.out.println(logs.get(0).getMessage()); } }
Run:
java.text.ParseException: Unparseable date: "2017-09-07 11:32:43" at java.text.DateFormat.parse(DateFormat.java:366) at com.dxc.leap.raas.uipath.orchestrator.service.LogParserTest.before(LogParserTest.java:37)
图1:
图2:
分析错误
检查日期格式:2017-09-07 11:32:43 对应 yyyy-MM-dd HH:mm:ss当时不理解为什么会出错,于是选择debug运行
Debug
分析错误
当时debug运行完还是没找到错误源头,之后无意中发现SimpleDateFormat构造的时间格式前后有空格,所以无法解析。new SimpleDateFormat( ” yyyy-MM-dd HH:mm:ss ” );
正确代码
去掉日期格式中的前后空格SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
第二个debug
java.lang.NullPointerException空指针异常
代码块
@RunWith(SpringRunner.class) @SpringBootTest public class LogParserTest { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); @Autowired LogParser logParser; List<LogModel> logs = null; @Before public void before() { try { LogModel logModel1 = new LogModel(sdf.parse("2017-09-07 11:32:43"), "Notepad_AddLogmessage execution started-4"); LogModel logModel2 = new LogModel(sdf.parse("2017-09-01 10:32:43"), "Notepad_AddLogmessage execution-3"); LogModel logModel3 = new LogModel(sdf.parse("2017-01-07 13:32:43"), "Notepad_AddLogmessage execution-1"); LogModel logModel4 = new LogModel(sdf.parse("2017-01-07 16:32:43"), "Notepad_AddLogmessage execution ended-2"); logs.add(logModel1); logs.add(logModel2); logs.add(logModel3); logs.add(logModel4); } catch (Exception e) { e.printStackTrace(); } } @Test public void parse() throws Exception { //logParser.parse(logs); System.out.println(logs.get(0).getMessage()); } }
Run
java.lang.NullPointerException at com.dxc.leap.raas.uipath.orchestrator.service.LogParserTest.before(LogParserTest.java:37)
分析错误
从错误提示中提示第37行空指针异常,那么意味着logs为null,再结合IntelliJ IDEA的运行提示中可以看出,第28行显示logs = null,就是说logs没有创建,需要new ArrayList()。Debug
图1:图2:
分析错误
从debug错误提示中可以看出,logs为null。正确代码
创建logs而不是声明logs,添加代码行logs = new ArrayList<>();代码如下:
@RunWith(SpringRunner.class) @SpringBootTest public class LogParserTest { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); @Autowired LogParser logParser; List<LogModel> logs = null; @Before public void before() { try { logs = new ArrayList<>(); LogModel logModel1 = new LogModel(sdf.parse("2017-09-07 11:32:43"), "Notepad_AddLogmessage execution started-4"); LogModel logModel2 = new LogModel(sdf.parse("2017-09-01 10:32:43"), "Notepad_AddLogmessage execution-3"); LogModel logModel3 = new LogModel(sdf.parse("2017-01-07 13:32:43"), "Notepad_AddLogmessage execution-1"); LogModel logModel4 = new LogModel(sdf.parse("2017-01-07 16:32:43"), "Notepad_AddLogmessage execution ended-2"); logs.add(logModel1); logs.add(logModel2); logs.add(logModel3); logs.add(logModel4); } catch (Exception e) { e.printStackTrace(); } } @Test public void parse() throws Exception { //logParser.parse(logs); System.out.println(logs.get(0).getMessage()); }
再次debug运行后:
此时logs不再是为null
相关文章推荐
- IntelliJ IDEA Debug调试案例一
- Intellij IDEA Debug调试技巧
- IntelliJ IDEA Debug调试用法一
- Intellij IDEA Debug调试技巧
- Intellij IDEA Debug调试技巧
- IntelliJ IDEA Debug调试用法一
- IntelliJ IDEA debug 调试打开 Evaluate Expression计算表达式窗口
- IntelliJ IDEA debug调试小结
- IntelliJ IDEA使用教程十 Debug调试程序
- Intellij IDEA Debug调试技巧
- Intellij IDEA Debug调试技巧
- Intellij IDEA Debug调试技巧
- Intellij IDEA Debug调试技巧
- IntelliJ IDEA使用教程十 Debug调试程序
- Intellij idea远程debug连接tomcat,实现单步调试
- Intellij idea远程debug连接tomcat,实现单步调试
- Intellij IDEA Debug调试方法
- 【转】Intellij IDEA Debug调试技巧
- Intellij IDEA Debug调试技巧
- Intellij IDEA Debug调试技巧