简体   繁体   中英

Why am I getting a NullPointerException with my array?

I cant figure out why I am getting a NullPointerException. What am I missing here ?

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

IJourneyDetails, IUserDetails, ISubscribeJourney are all interfaces

In a different activity, I am calling

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];

I am getting a null pointer exception in line marked by >>>>

I am sure allJourneyDetails[i] is null here.

It looks like allJourneyDetails[i] is null, since the array is still filled with null after initialization.

Did you expect creating the array to fill the array with non-null values? That's not how it works in Java.

allJourneyDetails = new CustomJourneyUserInformation[subscribedToJourneys.length];

should be

allJourneyDetails = new CustomJourneyUserInformation[providerDetails.length];

too; but @LouisWasserman is right

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM