0

I'm a newbie and just started Computer Science, we we're tasked to do this simple program and I'm having a problem I don't really know how to make a way around. Says that "decision" was not initialized, but using the quick fix (initializing the variable) makes the String decision = null;. Running this code then gives me this error Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "<local5>" is null at com.company.Main.main(Main.java:20). How do I fix this?

 import java.util.Scanner;
    
    public class Main
    {
        public static void main(String[] args)
        {
            // input scanner
            Scanner scn = new Scanner (System.in);
            String empName;
            System.out.print("Please enter the Employee name: ");
            empName = scn.next();
    
            String decision;
            boolean FP = true;
            while(FP)
            {
                System.out.print("Enter 'F' for Full Time or 'P' for Part Time: ");
    
                switch (decision) {
                    case "F" -> FP = true;
                    case "P" -> FP = false;
                }
            }
    
            if (FP = true)
            {
                System.out.println("---- Full Time Employee ----");
                double monSal;
                System.out.print("Enter monthly salary: ");
                monSal = scn.nextDouble();
                System.out.print(empName + monSal);
    
            }
    
        }
    }
4ver
  • 1
  • 1
  • 3
    You want your program to determine in a switch case what to do depending on whether `String decision` is `"F"` or `"P"`. But take a look at your code and try to point out to me what line would lead to this String variable getting any value at all. If you want your program to make decisions based on the value of a variable, then that variable needs to be assigned a value at some point in your code. – OH GOD SPIDERS Dec 02 '20 at 11:57
  • 1
    Adding to what @OHGODSPIDERS says: what if decision **had** a value, for example "F"? The loop would run forever, since then FP would be set to true. – Ingo Dec 02 '20 at 12:02

0 Answers0