我现在正在做作业,系统要求我返回一个空列表,并在该字段为空时打印一条错误消息。
这是我到目前为止的代码,但是抛出错误或返回空列表(如果我更改顺序,则返回空列表)是无法访问的。我该怎么做呢?
// the method is not void it returns a list
if (fileContent.equals(null)) {
return Collections.emptyList();
throw new Error("ERROR: No content loaded before parsing.");
} else {
// return another list ;
}
这是我收到的错误:
org.junit.ComparisonFailure: Console output not as expected in parseFileContent.
Expected :ERROR: No content loaded before parsing.
Actual :ERROR: executing parseFileContent for console output check:java.lang.NullPointerException
谢谢!
最佳答案
我猜你不需要抛出错误,而只需打印它。在这种情况下,请执行以下操作:
if(fileContent.equals(null)) {
// If you need to print the error:
System.err.println("ERROR: No content loaded before parsing.");
return Collections.EMPTY_LIST;
} else {
// return anotherList
}
关于java - 如何抛出错误并同时返回空列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60875004/