Home > Net >  How to fix null value issue
How to fix null value issue

Time:08-10

I am currently learning Java. I wrote the code below. I am getting an error Cannot invoke "java.lang.Double.doubleValue()" because the return value of "java.util.Map.get(Object)" is null on line 90 [double totalPopulationForZipCode = this.populationPerZipCode.get(dailyReport.getZipCode());].

I modified the code several ways and kept getting the same result. How can I fix this?

public class Processor {
    protected CovidReader covidReader;
    protected PopulationReader populationReader;
    protected PropertyReader propertyReader;
    protected List<Covid> covidData;
    protected List<Population> populationData;
    protected List<Property> propertyData;

    private Map<String, Double> populationPerZipCode = new HashMap<String, Double>();
    public Processor(CovidReader covidReader, PropertyReader propertyReader, PopulationReader populationReader) throws IOException {
        this.covidReader = covidReader;
        this.populationReader = populationReader;
        this.propertyReader = propertyReader;
        covidData = this.covidReader.getAllCovidData();
        populationData = this.populationReader.getAllPopulationData();
        propertyData = this.propertyReader.getAllPropertyData();
        populationPerZipCode = this.storePopulationDataToMap();
    }

    /**
     * Calculate the total population
     * for All ZIP Codes
     * @return totalPopulation
     */
    public double calculateTotalPopulation() {
        double totalPopulation = 0;
        for(Population residentPerZipCode: populationData) {
            totalPopulation  = residentPerZipCode.getResident();
        }
        return totalPopulation;
    }

    /**
     * Calculate total partial vaccinations
     * per capita
     * @param date
     * @return a map of zip code and ratio
     */
    public Map<Integer, String> calculateTotalPartialVaccination(String date) throws ParseException {
        Map<Integer, String> result = new TreeMap<>();

        for(Covid dailyReport: covidData) {
            String reportDate = dailyReport.getTimestamp().split(" ")[0];
            this.display(this.populationPerZipCode);
            if(this.compareDates(reportDate, date)) {
                int zipCode = Integer.parseInt(dailyReport.getZipCode());
                double partiallyVaccinated = dailyReport.getPartiallyVaccinated();
                double totalPopulationForZipCode = this.populationPerZipCode.get(String.valueOf(zipCode));
                double perCapita = (partiallyVaccinated / totalPopulationForZipCode);

                String ratio = this.formatRatio(perCapita);

                // Store data in map
                result.put(zipCode, ratio);
            }
        }
        return result;
    }

    /**
     * Calculate total partial vaccinations
     * per capita
     * @param date
     * @return a map of zip code and ratio
     */
    public Map<Integer, String> calculateTotalFullVaccination(String date) throws ParseException {
        Map<Integer, String> result = new TreeMap<>();
        for(Covid dailyReport: covidData) {
            String reportDate = dailyReport.getTimestamp().split(" ")[0];
            if(this.compareDates(reportDate, date)) {
                int zipCode = Integer.parseInt(dailyReport.getZipCode());
                double fullyVaccinated = dailyReport.getFullyVaccinated();
                System.out.println(fullyVaccinated);
                if(this.populationPerZipCode != null) {
                    double totalPopulationForZipCode = this.populationPerZipCode.get(dailyReport.getZipCode());
                    double perCapita = (fullyVaccinated/totalPopulationForZipCode);

                    String ratio = this.formatRatio(perCapita);

                    // Store data in map
                    result.put(zipCode, ratio);
                }


            }
        }
        return result;
    }

    /**
     * Compare two dates
     * @param date1
     * @param date2
     * @return a boolean value
     * @throws ParseException
     */
    public static boolean compareDates(String date1, String date2) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-mm-dd");
        Date spDate1 = dateFormat.parse(date1);
        Date spDate2 = dateFormat.parse(date2);
        if(spDate2.equals(spDate1)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Helper method to format result
     * @param value
     * @return 4 decimal numbers
     */
    private String formatRatio(double value) {
        DecimalFormat format = new DecimalFormat("0.0000");
        return format.format(value);
    }

    public void displayData(Map<Integer, String> map) {
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            System.out.println(entry.getKey()   ": "  entry.getValue());
        }
    }

    public void display(Map<String, Double> map) {
        for (Map.Entry<String, Double> entry : map.entrySet()) {
            System.out.println(entry.getKey()   ": "  entry.getValue());
        }
    }

    private Map<String, Double> storePopulationDataToMap() {
        Map<String, Double> result = new HashMap<>();
        for(Population residentPerZipCode: this.populationData) {
            String zipCode = residentPerZipCode.getZipCode();
            double resident = residentPerZipCode.getResident();
            if (!this.populationPerZipCode.containsKey(zipCode)) {
                this.populationPerZipCode.put(zipCode, resident);
            }
        }
        return result;
    }
}

CodePudding user response:

If key doesn't mapped to value in map, or mapped to null, it will return null in this case you need to check if you're getting null. In my opinion the best way to handle this is to use getOrDefault

 yourMap.getOrDefault(key, defaultValue);

Documentation on method
It will return defaultValue (some double value in your case), when it's yourkey: null in map.
There are also putIfAbsent method (docs), that might help.

Also check what is Optional in java, described good here, if you want to handle nulls in different way.

  •  Tags:  
  • java
  • Related