您的位置:首页 > 其它

IntelliJ IDEA Debug调试案例二

2017-09-14 15:41 811 查看
第一个debug
代码块

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 调试