1

I am getting this error, but I can not seem to find want might be causing it. It says the error is on the line where I initialize the variable bet. If I remove that line and Integer.parseInt(betTextFieldAmount), then it says the error is on that initialization.

public class RollTheDice extends JFrame implements ActionListener {

    JButton roll = new JButton("Roll the Dice");
    JButton bet1 = new JButton("5$");
    JButton bet2 = new JButton("10$");
    JButton bet3 = new JButton("25$");
    JButton bet4 = new JButton("50$");
    JButton bet5 = new JButton("100$");

    JTextField betTextField = new JTextField(5);
    String betTextFieldAmount = betTextField.getText();
    int bet = Integer.parseInt(betTextFieldAmount);

    int money = 100;

    JLabel currentBet = new JLabel();
    JLabel currentMoney = new JLabel("Your money: " + money + "$");

    int myDice = new Random().nextInt(12) + 1;
    int AIDice = new Random().nextInt(12) + 1;

    public static void RollTheDice() {
        int myDice = new Random().nextInt(12) + 1;
        int AIDice = new Random().nextInt(12) + 1;
    }

    public RollTheDice() {
        setLayout(new FlowLayout());

        add(currentMoney);

        add(bet1);
        bet1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (money < 5) {
                    JOptionPane.showMessageDialog(null, "Not enough money!");
                } else {
                    money -= 5;
                    bet +=5;
                }
                currentMoney.setText(money + "$");
            }           
        });

        add(bet2);
        bet2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (money < 10) {
                    JOptionPane.showMessageDialog(null, "Not enough money!");
                } else {
                    money -= 10;
                    bet +=10;
                }
                currentMoney.setText(money + "$");
            }           
        });

        add(bet3);
        bet3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (money < 25) {
                    JOptionPane.showMessageDialog(null, "Not enough money!");
                } else {
                    money -= 25;
                    bet +=25;
                }
                currentMoney.setText(money + "$");
            }           
        });

        add(bet4);
        bet4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (money < 50) {
                    JOptionPane.showMessageDialog(null, "Not enough money!");
                } else {
                    money -= 50;
                    bet +=50;
                }
                currentMoney.setText(money + "$");
            }           
        });

        add(bet5);
        bet5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (money < 100) {
                    JOptionPane.showMessageDialog(null, "Not enough money!");
                } else {
                    money -= 100;
                    bet +=100;
                }
                currentMoney.setText(money + "$");
            }           
        });

        add(betTextField);
        betTextField.setEditable(false);
        add(roll);
        roll.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (myDice > AIDice) {
                    money += bet;
                    JOptionPane.showMessageDialog(null, "You won " + bet + "$");
                } else {
                    JOptionPane.showMessageDialog(null, "You lost " + bet + "$");
                }
                currentMoney.setText(money + "$");
                RollTheDice();
            }           
        });
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}
JNYRanger
  • 6,829
  • 12
  • 53
  • 81
Nivalen
  • 29
  • 5
  • Just to clarify: Is it the same error when you remove the parsing method? – JNYRanger Aug 12 '15 at 13:03
  • Yes it is exactly the same. – Nivalen Aug 12 '15 at 13:08
  • As other's posted below NumberFormatException always means that the string passed to parseInt is not a valid number, so you should wrap with a `try...catch` to handle bad data. Additionally, you should separate the initialization and the parsing so at least you can default to a value when data data is passed. – JNYRanger Aug 12 '15 at 13:10
  • So surround "bet" in each ActionListener? – Nivalen Aug 12 '15 at 13:24
  • No, only whenever you perform the parsing you should wrap the parse method in a `try..catch` block to catch invalid number data being passed and do something like alerting the user. – JNYRanger Aug 12 '15 at 13:37
  • Yeah I did that but Eclipse marks everything in red and and I cannot compile the program. – Nivalen Aug 12 '15 at 14:29

3 Answers3

3

Integer.parseInt(betTextFieldAmount)is causing this error.
Check betTextFieldAmount to see whether it has a valid integer value or not.
Ensure that betTextFieldAmount should not be null, or contain any special characters or white spaces at the begining or end.

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
3

This is caused by the value of betTextFieldAmount not being a valid integer, so when you try to convert it using Integer.parseInt(betTextFieldAmount); an exception is thrown.

You should surround the parseInt call with a try...catch statement and display an appropriate error message to the user if this happens.

Even better, try to prevent the user entering non-numeric values in the first place by restricting which characters can be entered in the field, and add an emptiness check to catch the case where no value has been entered in the field.

codebox
  • 19,927
  • 9
  • 63
  • 81
  • How do I restrict certain characters in the textfield? Can't I make the textfield not editable? – Nivalen Aug 12 '15 at 13:11
  • Also do I have to surround the String and Integer at the start only when I create them or every time I add a value to bet? – Nivalen Aug 12 '15 at 13:19
  • Some ideas about restricting characters here: https://stackoverflow.com/questions/15703644/how-to-filter-certain-characters-in-jtextfield – codebox Aug 12 '15 at 13:24
  • You should surround any calls to `Integer.parseInt` with a `try..catch` block – codebox Aug 12 '15 at 13:24
  • I think I surrounded all of them, all except the first (the bet initialization, because I get a whole bunch of errors once I do that) but I still get the same error. – Nivalen Aug 12 '15 at 13:32
-1

Try to verify if you betTextField.getText() is not null. I'm presuming that your field has no value, so the getText will return null and the parse will fail