0

So I am in the middle of a project and supposedly my code was fine for this part but I keep getting errors anyway. I'm not sure what my original problem was but I'm sure it's changed at some point during my changes. Despite it pointing to the the line with the problem, I just can't figure out the problem(s) is/are. What is causing the error?

Exception in thread "main" java.lang.NullPointerException
     at Cafe.setName(Cafe.java:14)
     at CubanCafe.main(CubanCafe.java:16)



import java.util.Scanner;

public class CubanCafe
{

public static void main(String[] args)
{
    String stateName;

    Scanner scan =  new Scanner(System.in);

    System.out.print(" Enter state: ");
    stateName = scan.nextLine();

    Cafe cafeState = new Cafe(stateName);
    cafeState.setName();
    cafeState.setTaxRate();
    System.out.println(cafeState);;
    System.out.println(cafeState.getTaxRate());
  }
}


public class Cafe {
    private String state, name;
    private double taxRate;

    public Cafe(String state){
        state = state.toUpperCase( );   
        name = null;
        taxRate = 0;

    }

    public void setName(){
        if(state.equals("MD"))
             name = "Parkville Cuban Cafe";
        else if(state.equals("VA"))
             name = "Alexandria Cuban Cafe";
        else 
             name = null;
    }   

    public void setTaxRate(){
        if (state.equals("MD"))
            taxRate = .06;
        else if (state.equals("VA"))
            taxRate = .04;
        else
            taxRate = 0;
    }

    public double getTaxRate(){
        return taxRate;

    }

    public String toString(){
         return (name);
    }

}
M.Burke
  • 25
  • 4
  • The heuristic for debugging a NullPointerException is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Apr 14 '17 at 14:25
  • Change `state = state.toUpperCase( );` to `this.state = state.toUpperCase( );`. Otherwise your state field remains null since the setState method changes the parameter and not the field. Note that that method is not a valid setter method and so should not be named such. – Hovercraft Full Of Eels Apr 14 '17 at 14:27
  • @HovercraftFullOfEels you beat me for 9 seconds ahah – Aurasphere Apr 14 '17 at 14:28
  • @Aurasphere: this sort of question should not be answered but rather closed and commented. We already have too many NPE questions on this site. – Hovercraft Full Of Eels Apr 14 '17 at 14:28
  • @HovercraftFullOfEels I'll keep that in mind for future questions. Thank you – Aurasphere Apr 14 '17 at 14:29

1 Answers1

0

The problem is here:

state = state.toUpperCase( );   

The variable state is shadowing your field state. You can solve it by adding "this":

this.state = state.toUpperCase( );   
Aurasphere
  • 3,841
  • 12
  • 44
  • 71