-1

Ok i am making a small guessing game and i have a little porblem with one line of the code. When i guess the number it shows the error below.The code is below the error.

Error

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 Answers1

0

A NullPointerException is thrown when you never initialized a field of a class or assigned null to it explicitly.

You probably need to initialize it in the init-method:

private void init(){
    this.powerups = new PowerUps();
    randomnumber = new Random();
}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Well when i do that i make a new object of the powerups class.Because i have a menu java class i initialize it there and then pass the object to the guessing game class.Because if i initialize it in the menu class and in the guess class i get different values.I can make it static but i want to try it this way that's why. – ExTrevoman Aug 06 '14 at 12:18
  • Well in that case you don't call `GetObject` in time (before starting the game) or with a `null` value. So the bug is located somewhere else.... – Willem Van Onsem Aug 06 '14 at 12:22
  • Ok thanks very much i think i fixed it.You were right i wasn't calling it in time. – ExTrevoman Aug 06 '14 at 12:24