为什么我的数组会出现NullPointerException?

时间:2012-12-13 19:50:11

标签: java interface nullpointerexception

我无法弄清楚为什么我会收到NullPointerException。我在这里缺少什么?

public class CustomJourneyUserInformation {
    public IJourneyDetails journeyDetails;
    public IUserDetails userDetails;
    public ISubscribeJourney subscribedToJourneys;
}

IJourneyDetails,IUserDetails,ISubscribeJourney都是接口

在另一项活动中,我正在致电

private CustomJourneyUserInformation[] allJourneyDetails;
    allJourneyDetails = new CustomJourneyUserInformation[subscribedToJourneys.length];
    if (providerDetails[i] != null)
>>> allJourneyDetails[i].journeyDetails = providerDetails[i];
    if (userDetails[i] != null)
    allJourneyDetails[i].userDetails = userDetails[i];
    if (subscribedToJourneys[i] != null)
        allJourneyDetails[i].subscribedToJourneys = subscribedToJourneys[i];

我在>>>>>

标记的行中得到一个空指针异常

我确信此处allJourneyDetails[i]为空。

2 个答案:

答案 0 :(得分:6)

看起来allJourneyDetails[i]为空,因为初始化后数组仍然填充null

您是否期望创建数组以使用非空值填充数组?这不是它在Java中的工作方式。

答案 1 :(得分:2)

allJourneyDetails = new CustomJourneyUserInformation[subscribedToJourneys.length];

应该是

allJourneyDetails = new CustomJourneyUserInformation[providerDetails.length];

太;但@LouisWasserman是对的