Why is the value returned by this Map null?

I have a program that asks the user 10 political questions and determines which party they belong to with probability calculations based off previous survey answers stored in txt files.
Whenever I run this right after I enter an answer for the first question, I get an exception that says the return value of “java.util.Map.get(Object)” is null.

public static void main(String[] args) {
        Map<PoliticalParty, ProbabilityManager> probabilityManagerMap = new HashMap<>();
    
        probabilityManagerMap.put(PoliticalParty.REPUBLICAN, new ProbabilityManager(PoliticalParty.REPUBLICAN, 0.25));   

        probabilityManagerMap.put(PoliticalParty.DEMOCRAT, new ProbabilityManager(PoliticalParty.DEMOCRAT, 0.25));   

        probabilityManagerMap.put(PoliticalParty.SOCIALIST, new ProbabilityManager(PoliticalParty.SOCIALIST, 0.25));     

        probabilityManagerMap.put(PoliticalParty.LIBERTARIAN, new ProbabilityManager(PoliticalParty.LIBERTARIAN, 0.25));
    Map<String, Integer> answerNumbers = new HashMap<>();
        answerNumbers.put("a", 1);
        answerNumbers.put("b", 2);
        answerNumbers.put("c", 3);
        Survey survey = new Survey();
        SurveyUserInput userInputCheck = new SurveyUserInput(answerNumbers);
        String userInput = "";
        Map<Integer, Integer> answerIndices = new HashMap<>();
        ProbabilityCalculator cal = new ProbabilityCalculator(probabilityManagerMap);
        int answerNumber = 0;
        SurveyManager.printProb(probabilityManagerMap);
        
        for(int i = 0; i<survey.surveyComponentArray.length; i++) {
            
            SurveyComponent c = survey.getQuestion(i);
            answerNumber = userInputCheck.performSurvey(i, c);
            answerIndices.put(i, answerNumber);
            cal.recalculate(i, answerNumber); //this is the problem. When I comment this out, its fine.
            
    
            
        }

public class SurveyUserInput {
    Map<String, Integer> mapKey;
    public SurveyUserInput(Map<String, Integer> mapKey) {
        this.mapKey = mapKey;
    }

    Scanner scanner = new Scanner(System.in);

    Survey survey = new Survey();
    
    
    public int performSurvey(int questionNumber, SurveyComponent com) {
        String user = "";
        //for(int i = 0; i<survey.surveyComponentArray.length; i++) {
            int a;
            
            System.out.println(com.question);
            for(String answer : com.getAnswers()) {
                System.out.println(answer);
                
            
            }
            do {
                user = scanner.next().toLowerCase();
            } while(!user.equals("a") && !user.equals("b") && !user.equals("c"));
            a = mapKey.get(user);
            return a;
         }

public class SurveyComponent {
    
    String question;
    String[] answers;
    
    SurveyComponent(String question, String...answers) {
        this.question = question;
        this.answers = answers;
        
    }
    

    public String[] getAnswers() {
        return answers;
    }

}

public class Survey {

    SurveyComponent[] surveyComponentArray; 

     

    public Survey() { 

        surveyComponentArray = new SurveyComponent[10]; 

        surveyComponentArray[0] = new SurveyComponent("How do you feel about the issue of guns?", 

                "A) I believe in the 2nd Amendment completely.", "B) I believe in gun control", 

                "C) I don't care"); 

        surveyComponentArray[1] = new SurveyComponent("How do you feel about the USA's involvement in with other nations?", 

                "A) I believe we should focus on America.", "B) I believe we need to be involved.", 

                "C) I don't even believe in foregin aid."); 

        surveyComponentArray[2] = new SurveyComponent("How do you feel about Climate Change?", 

                "A) I don't think its a big deal its not like its the end of the world.", "B) I believe its literally the end of the world.", 

                "C) I think its a concern."); 

        surveyComponentArray[3] = new SurveyComponent("How do you feel about Marxism?", 

                "A) I greatly admire it.", "B) I don't admire it", 

                "C) I don't care"); 

        surveyComponentArray[4] = new SurveyComponent("Do you believe in capitalism?", 

                "A) Yes.", "B) No, I think it should be relplaced with a more socailly equitable system", 

                "C) I don't care"); 

        surveyComponentArray[5] = new SurveyComponent("How do you feel about taxes?", 

                "A) I believe taxes should be lowered.", "B) I think they should be raised to fund social programs.", 

                "C) I don't think we should have to pay taxes."); 

        surveyComponentArray[6] = new SurveyComponent("How do you feel about the government's part in our lives?", 

                "A) I believe the government should play an active role.", "B) I wish the government would just leave me alone.", 

                "C) I believe in small government."); 

        surveyComponentArray[7] = new SurveyComponent("How do you feel about incarceration and law enforcement?", 

                "A) I believe prisons and law enforcement should be dimantled and abolished.", "B) I believe in criminal justice reform", 

                "C) I believe in having prisions and law enforcement."); 

        surveyComponentArray[8] = new SurveyComponent("How do you feel about laws banning things such as drugs?", 

                "A) I believe there need to be some laws. Such as banning drugs.", "B) I believe everyone is capable of making their own choices", 

                "C) I believe no one should be in prison for just using drugs."); 

        surveyComponentArray[9] = new SurveyComponent("How do you feel about funding welfare?", 

                "A) I believe welfare should be funded a lot.", "B) I believe welfare should not be funded", 

                "C) I don't care"); 

    } 

     

    public SurveyComponent getQuestion(int index) { 

        return surveyComponentArray[index]; 

    } 

 


}

public class ProbabilityCalculator {
    private final Map<PoliticalParty, ProbabilityManager> partyMap;

    public ProbabilityCalculator(Map<PoliticalParty, ProbabilityManager> partyMap) {
        this.partyMap = partyMap;
    }
    
    
    
    public void recalculate(int questionNumber, int answerNumber) {
        double totalProb = 0;
        
        Map<PoliticalParty, Double> probOfAnswerGivenParty = new HashMap<>();
        
        
        for(PoliticalParty party : PoliticalParty.values()) {
            ProbabilityManager probManager = partyMap.get(party); //The error says the error apperently lies somewhere in here. 
            
            if(probManager != null) {
                double probOfAnsGivenP = probManager.calculateProbOfAnswerGivenParty(questionNumber, answerNumber);
                totalProb += probManager.getProbability() * probOfAnsGivenP;
                
                probOfAnswerGivenParty.put(party, probOfAnsGivenP);
            } else {
                System.out.println("no");
            }
            
            
        }
        
        for(PoliticalParty party : PoliticalParty.values()) {
            
        ProbabilityManager probManager = partyMap.get(party);
             
            double probOfPartyGivenANS = probManager.getProbability() * probOfAnswerGivenParty.get(party) / totalProb;
            probManager.setProbability(probOfPartyGivenANS);
        }
    }
    

}

public class ProbabilityManager {
    
    public PoliticalParty party;
    private double partyProbability;
    private String fileName;
    private Map<Integer, Integer> previousSurveyData;
    
    
    public ProbabilityManager(PoliticalParty party, double partyProbability) {
        this.party = party;
        this.partyProbability = partyProbability;
        this.fileName = party.name().toLowerCase();
        previousSurveyData = SurveyFileIO.readFromFile(fileName);
    }
    public void setProbability(double partyProbability) {
        this.partyProbability = partyProbability;
    }
    public double getProbability() {
        return partyProbability;
    }
    
    public double calculateProbOfAnswerGivenParty(int questionNumber, int answerNumber) {
        int counter = 0;
    
            if(previousSurveyData.get(questionNumber) == answerNumber) {
                counter++;
            
        } else {
            return 1.0;
        }
        return (double)counter / previousSurveyData.size();
        
    }
    
    public void saveAnswerIndices(Map<Integer, Integer> answerIndices) {
        for(Map.Entry<Integer, Integer> entry : answerIndices.entrySet()) {
            int key = entry.getKey();
            int value = entry.getValue();
            
            this.previousSurveyData.put(key, value);
            
        }
        SurveyFileIO.writeToFile(fileName, previousSurveyData);
    }

    
}
This is the enum
public enum PoliticalParty {
    
    REPUBLICAN,
    DEMOCRAT,
    LIBERTARIAN,
    SOCIALIST

}

This is the error: Exception in thread “main” java.lang.NullPointerException: Cannot invoke “java.lang.Integer.intValue()” because the return value of “java.util.Map.get(Object)” is null
at ProbabilityManager.calculateProbOfAnswerGivenParty(ProbabilityManager.java:28)
at ProbabilityCalculator.recalculate(ProbabilityCalculator.java:22)
at Main.main(Main.java:39)

I cannot figure this out. I know this is a lot of code, but I’m at a loss.

I’ve been trying different error handling methods but nothing works. I would REALLY APPRICIATE some help.
Thanks!

New contributor

Rhonda Williams is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật