com.rs.game.player.Dm.initiative(Dm.java:27)中的java.lang.NullPointerException

时间:2015-03-01 23:11:27

标签: java arrays

我无法弄清楚为什么这会一直指向null。我是一个相当新的程序员。但是在这里我制作了一个Dice类和一个命令类。 我试过改变变量和各种东西。我想我只是不确定为什么它指向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;
    }
}

/ *****我现在只使用d20 ...... **** /

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 个答案:

答案 0 :(得分:0)

在这一行public static int initRoll = init.die20;中,您指的是&#39; die20&#39;的值,但是直到很久以后,在for循环中,当您说init.d20()时,它不会被设置。我希望这有点帮助,但我认为你的代码也需要在其他领域工作。