嗨,我正在编写代码,用于根据输入内容过滤特定数据。如果输入在数据库中不可用,由于我是Java新手,它会在nullpointer exception
上抛出list
,我不知道如何解决此错误
private String isCreditRatingValid(String isin) {
System.out.println(isin);
List<Credit_rating_details> result = Credit.stream()
.filter(i-> i.getIssuer_id()
.contains(isin)).collect(Collectors.toList()); \\throwing NullPointerException on this line\\
if(!result.isEmpty() ) {
validCreditRating = result.get(0).getCredit_rating();
return result.get(0).getCredit_rating();
}else {
return null;
}
答案 0 :(得分:1)
根据您的评论,您将其声明为
static ArrayList<Credit_rating_details> Credit = null;
正在对null
对象执行操作时导致NPE。将其更改为
static ArrayList<Credit_rating_details> Credit = new ArrayList<Credit_rating_details>();