为什么会出现错误和异常?

时间:2014-08-06 12:09:28

标签: java

好的我正在做一个小猜谜游戏,我有一行代码的一点点问题。 当我猜数字时,它会显示下面的错误。代码低于错误。

错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at GuessGame.Start(GuessGame.java:63)
    at Menu$1.actionPerformed(Menu.java:39)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

GuessGame.java

import javax.swing.*;
import java.util.Random;

public class GuessGame {

    private Random randomnumber;
    private Bank bank;
    private PowerUps powerups;

    private void init(){

        randomnumber = new Random();
    }

    public void Start(){
        init();
        String userinput = "";
        int usernumber = 0;
        int random = 0;
        int guess = 12;
        int bet = 0;
        int powerupmodifier = 0;
        boolean activated = true;
        random = 1+(randomnumber.nextInt(100));
        JOptionPane.showMessageDialog(null,  "Guess the number from 1 -100");
        while(activated){
        userinput = JOptionPane.showInputDialog("Please enter a bet");
        bet = Integer.parseInt(userinput);
        if(bet > bank.getMoney()){
            JOptionPane.showMessageDialog(null, "You can't enter a bet when you don't have the money");

        }else if(bet <= 0){

            JOptionPane.showMessageDialog(null, "You can't enter a bet which is equal 0 or below 0");

        }else{

            activated = false;

        }
        }
        boolean active = true;
        while(active){
            if(usernumber != random){
            userinput = JOptionPane.showInputDialog("Enter your guess number");
            usernumber = Integer.parseInt(userinput);

            if(random > usernumber){
                guess--;
                JOptionPane.showMessageDialog(null,"To low. "+guess+" guesses left.");

            }else if(random < usernumber){
                guess--;
                JOptionPane.showMessageDialog(null,"To high. "+guess+" guesses left.");
            }
            if(guess <= 0){
                JOptionPane.showMessageDialog(null, "You lost "+bet+ " money.");
                bank.setMoney(-bet);
                active = false;

            }
            }else{
LINE 63:                if(powerups.getActive()){
                    JOptionPane.showMessageDialog(null, "You guessed the number you get 100 money");
                    bank.setMoney(bet);
                    active = false;
                }


            }
        }

    }

    public void GetObject(Bank bank, PowerUps powerups){

        this.bank = bank;
        this.powerups = powerups;

    }

}

PowerUps.java

public class PowerUps {

    private boolean powerupactive = false;

    public void setActive(boolean active){

        powerupactive = active;

    }

    public boolean getActive(){
        return powerupactive;

    }

}

1 个答案:

答案 0 :(得分:0)

如果您从未初始化某个字段或明确指定NullPointerException字段,则会引发null

您可能需要在init - 方法中初始化它:

private void init(){
    this.powerups = new PowerUps();
    randomnumber = new Random();
}