IntelliJ IDEA Debug调试案例二

来源:互联网 发布:网络侦探黄金大 编辑:程序博客网 时间:2024/06/03 17:02

    • 第一个debug
      • 代码块
      • Run
        • 分析错误
      • Debug
        • 分析错误
      • 正确代码
    • 第二个debug
      • 代码块
      • Run
        • 分析错误
      • Debug
        • 分析错误
      • 正确代码

第一个debug

java.text.ParseException: Unparseable date
日期解析异常

代码块

@RunWith(SpringRunner.class)@SpringBootTestpublic 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)@SpringBootTestpublic 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)@SpringBootTestpublic 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