0

public class JohnsonGuessingGame extends Application{
    private ImageView d;
    private Image one;
    private Image two;
    private Image three;
    private Image four;
    private Image five;
    private Image six;
    private TextField input;
    public static void main(String[] args){
        launch(args);
    }
    public void start(Stage primaryStage)
    {     
        TextField input = new TextField();
        Button button = new Button("PLAY");
        Label prompt = new Label();    
        int dice = 6;

        Image one = new Image("1.png");
        Image two = new Image("2.png");
        Image three = new Image("3.png");
        Image four = new Image("4.png");
        Image five = new Image("5.png");
        Image six = new Image("6.png");
        ImageView d = new ImageView(six);

        HBox hbox = new HBox(prompt, input);
        VBox vbox = new VBox(hbox, d, button);
        
        button.setOnAction(new ButtonHandler());

        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    class ButtonHandler implements EventHandler<ActionEvent>
    {
       @Override
       public void handle(ActionEvent event)
       {
          // Get the kilometers.
        
           int inp = Integer.parseInt(input.getText());
          if(inp == 1){
            d.setImage(one);
          }
          else if(inp == 2){
            d.setImage(two);
          } 
          else if(inp == 3){
            d.setImage(three);
        }
          else if(inp == 4){
            d.setImage(four);
        } 
          else if(inp == 5){
            d.setImage(five);
        }
        else if(inp == 6){
            d.setImage(six);
        }
          
       }
    }
}

everything works fine until i hit the button. then it gives me this error:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TextField.getText()" because "this.this$0.input" is null

I don't even know what I'm doing wrong please tell me, thanks.

  • 1
    One instance of `TextField input` _shadows_ the other, and the one accessible to the handler remains `null`. – trashgod May 06 '22 at 23:04

0 Answers0