-1

I just cannot figure out why this keeps pointing to null. I am a fairly new programmer. But here I have made a Dice class and a commands class. I have tried changing variables and all sorts of stuff. I guess I'm just not sure why it's pointing to null.

    package com.rs.game.player;

/**
 * Created by Kevinpro0 on 2/26/2015.
 */
public class Dice {
    public int die2;
    public int die3;
    public int die4;
    public int die6;
    public int die8;
    public int die10;
    public int die12;
    public int die14;
    public int die16;
    public int die18;
    public static int die20;

    public void d2() {
        die2 = (int)(Math.random()*2) +1;
    }
    public void d3() {
        die3 = (int)(Math.random()*3) +1;
    }
    public void d4() {
        die4 = (int)(Math.random()*4) +1;
    }
    public void d6() {
        die6 = (int)(Math.random()*6) +1;
    }
    public void d8() {
        die8 = (int)(Math.random()*8) +1;
    }
    public void d10() {
        die10 = (int)(Math.random()*10) +1;
    }
    public void d12() {
        die12 = (int)(Math.random()*12) +1;
    }
    public void d14() {
        die14 = (int)(Math.random()*14) +1;
    }
    public void d16() {
        die16 = (int)(Math.random()*16) +1;
    }
    public void d18() {
        die18 = (int)(Math.random()*18) +1;
    }
    public static int d20() {
        die20 = (int)(Math.random()*20) +1;
        return die20;
    }
}

/***** i am ONLY using d20 for this right now...****/

DM.java

package com.rs.game.player;
import com.rs.game.player.*;
import com.rs.game.World;

import java.util.Arrays;

/**
 * Created by Kevinpro0 on 3/1/2015.
 */
public class Dm {
    public static Dice init = new Dice();
    public static int worldSize = World.getPlayers().size();
    public static int[] PlayerInt = new int[worldSize];
    //
    public static Player player;
    public static int initRoll  = init.die20;
    public static String playersName;
    public static int i = 1;


    public static void initiative(){

        for (i = 1; i < World.getPlayers().size() + 1; i++) {
            //Dice = new Dice();
            init.d20();
            PlayerInt[i] = initRoll;
            player.getPackets().sendPanelBoxMessage(Integer.toString(Dice.d20()));
            //player.getPackets().sendPanelBoxMessage("Player " + i + " is " + playersName + " and initiative roll: " + Integer.toString(initRoll));

        }
    }
}



Commands.java


case "init":
                case "initiative":
                    Dm dmaster = new Dm();
                    //String currentPlayer = player.getUsername();
                    //Player Dm = World.getPlayerByDisplayName("Dm");
                    if (player.getUsername().equalsIgnoreCase("Dm")) {
                        if (World.getPlayers().size() <= 1){
                            player.getPackets().sendPanelBoxMessage("Not enough players logged in.");
                        } else {
                            Dm.initiative();
                            player.getPackets().sendPanelBoxMessage(Dm.playersName + " and initiative roll: " + Integer.toString(Dm.initRoll));

                        }

                    }

                return true;

1 Answers1

0

In this line public static int initRoll = init.die20; you are referring to the value of 'die20', but it is not set until much later, in the for loop, when you say init.d20(). I hope that helps a little, though I think your code needs work in other areas too.

Sam
  • 8,330
  • 2
  • 26
  • 51