0

我无法弄清楚为什么我会收到 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]这里是空的。

4

2 回答 2

6

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

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

于 2012-12-13T19:51:59.213 回答
2
allJourneyDetails = new CustomJourneyUserInformation[subscribedToJourneys.length];

应该

allJourneyDetails = new CustomJourneyUserInformation[providerDetails.length];

也; 但@LouisWasserman 是对的

于 2012-12-13T19:53:05.833 回答