在迭代使用Map.Entry <String,Map <Date,List <SomeObject >>>的三个for-each中,为什么我在第二个for-each循环中有异常?

时间:2019-10-25 12:23:22

标签: java list date dictionary nullpointerexception

在使用Map.Entry >>绑定的三个foreach中,当我迭代第二个Map时,在下面的代码中有一个NullPointerException。我不知道根本原因是什么。 要进行复制,您需要在项目文件夹中创建一个“ inputData”文件夹,其中包含一个“ data.txt”文件,该文件的某些值如下所示(我仅举几个例子):

Apahida,15,199,2017-09-03
Apahida,15,249,2017-09-03
Apahida,15,299,2017-09-03
Floresti,7,100,2017-09-01
Floresti,7,250,2017-09-01
Turda,29,149,2017-09-01
Turda,29,199,2017-09-02

此后,当我从MainApp类运行我的main方法时(请参见下文),我在行中有一个“线程“ main”中的异常java.lang.NullPointerException”,其中的代码带有以下注释。您能找出问题出在哪里,请给我建议解决方案吗?

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class MainApp {

    public static void main(String[] args) {

        List<Package> dataPackage = new ArrayList<>();

        File file = new File("inputData/data.txt");
        List<String> listInputData = null;
        try {
            listInputData = Files.readAllLines(Paths.get(file.getPath()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (String string : listInputData) {
            String[] split = string.split(",");

            Package aPackage = new Package(split[0], Double.parseDouble(split[1]), Double.parseDouble(split[2]), split[3]);
            dataPackage.add(aPackage);
        }

        Map<String, Map<Date, List<Package>>> packagesGroupedBasedOnTargetLocationAndDeliveryDate = new HashMap<>();
        for (Package aPackage : dataPackage) {
            String targetLocationOfPackage = aPackage.getTargetLocation();
            boolean targetLocationExistInMap = false;
            for (Map.Entry<String, Map<Date, List<Package>>> targetLocationInMap : packagesGroupedBasedOnTargetLocationAndDeliveryDate.entrySet()
            ) {

                if (targetLocationInMap.getKey().equals(targetLocationOfPackage)) {
                    targetLocationExistInMap = true;
                    Map<Date, List<Package>> packagesGroupedBasedOnDeliveryDate =
                            packagesGroupedBasedOnTargetLocationAndDeliveryDate.get(targetLocationOfPackage);
                    boolean deliveryDateExistInMap = false;
                    Date deliveryDateOfPackage = aPackage.getDeliveryDate();
                    for (Map.Entry<Date, List<Package>> deliveryDateInMap : packagesGroupedBasedOnDeliveryDate.entrySet()) {

                        if (deliveryDateInMap.getKey().equals(deliveryDateOfPackage)) {
                            deliveryDateExistInMap = true;
                            packagesGroupedBasedOnTargetLocationAndDeliveryDate
                                    .get(targetLocationOfPackage)
                                    .get(deliveryDateOfPackage)
                                    .add(aPackage);
                        }
                    }
                    if (!deliveryDateExistInMap) {
                        List<Package> newListOfPackageByDeliveryDate = new ArrayList<>();
                        newListOfPackageByDeliveryDate.add(aPackage);
                        packagesGroupedBasedOnTargetLocationAndDeliveryDate
                                .get(targetLocationOfPackage)
                                .put(deliveryDateOfPackage, newListOfPackageByDeliveryDate);
                    }
                    System.out.println();
                    System.out.println(packagesGroupedBasedOnTargetLocationAndDeliveryDate.toString());
                    System.out.println();
                }

            }

            if (!targetLocationExistInMap) {
                List<Package> newListOfPackageByDeliveryDate = new ArrayList<>();
                newListOfPackageByDeliveryDate.add(aPackage);
                Map<Date, List<Package>> newMapOfPackageByDeliveryDate = new HashMap<>();
                newMapOfPackageByDeliveryDate.put(aPackage.getDeliveryDate(), newListOfPackageByDeliveryDate);
                packagesGroupedBasedOnTargetLocationAndDeliveryDate
                        .put(targetLocationOfPackage, newMapOfPackageByDeliveryDate);
            }
        }

        for (Map.Entry<String, Map<Date, List<Package>>> targetLocationSTRING :
                packagesGroupedBasedOnTargetLocationAndDeliveryDate.entrySet()) {

            for (Map.Entry<Date, List<Package>> deliveryDateDATE :
                    packagesGroupedBasedOnTargetLocationAndDeliveryDate
                            .get(targetLocationSTRING).entrySet()) {         /// ***EXCEPTION APPEAR HERE, WHY?***

                for (Package aPackage :
                        packagesGroupedBasedOnTargetLocationAndDeliveryDate
                                .get(targetLocationSTRING).get(deliveryDateDATE)) {
                    System.out.println(aPackage.toString());

                }
            }
        }


    }

}

1 个答案:

答案 0 :(得分:1)

在标记为/// ***EXCEPTION APPEAR HERE, WHY?***的行中,您正在执行packagesGroupedBasedOnTargetLocationAndDeliveryDate.get(),并以Map.Entry<String, Map<Date, List<Package>>>作为键。但是packagesGroupedBasedOnTargetLocationAndDeliveryDate的键是String类型,因此我们可以从一开始就知道没有找到键,因此get返回null。接下来,您尝试在此entrySet()上调用null,从而得到一个NullPointerException

问题来自Map.get()接受错误类型的密钥。原因是历史性的。但是您的IDE应该警告您。