光滑2D:渲染空指针异常

时间:2013-03-14 22:03:18

标签: java nullpointerexception slick2d

所以我的问题是当我创建的状态被调用时,我得到NullPointerException

事情是,它不是nul l,至少我是这么认为的。有人能告诉我NPE的来源吗?

PS:这是草率的代码,因为我没有清理它。

粗体线出现错误。

public class NewPlayerState extends BasicGameState {

    private int stateID;
    private boolean mouseDown;
    private int mouseX;
    private int mouseY;
    private Random rand;
    private String allTheStats;

    private Image classPreview;
    private Image genderSelector;

    private Player player;
    private Write write;

    private String className = "paladin";
    private String name = "JamacanGuy";
    private int hitPoints = 0;
    private int str = 0;
    private int dex = 0;
    private int sta = 0;
    private int intel = 0;
    private int cha = 0;
    private boolean male = true;

    private int hpMod = 0;
    private int strMod = 0;
    private int dexMod = 0;
    private int staMod = 0;
    private int intelMod = 0;
    private int chaMod = 0;

    private Input input;

    public NewPlayerState(int stateID) {
        this.stateID = stateID;
    }

    /* 
     *  
     *  Initiation method, brings up relevant variables and get this Initializes them!
     *  
     */

    @Override
    public void init(GameContainer gc, StateBasedGame sbg)
            throws SlickException {
        // Ready the PARSER!!!!
        rand = new Random();
        write = new Write();
        // init classPreview as paladin
        classPreview = new Image("gfx/player/previews/paladin_male.png");
        // init gender icons
        genderSelector = new Image("gfx/extra/new_player/gender_selector.png");
        allTheStats = "Hitpoints: " + hitPoints + " + HP Modifier: " + hpMod + " = " + (hitPoints + hpMod) + "\n" +
                      "Strength: " + str + " + STR Modifier: " + strMod + " = " + (str + strMod) + "\n" +
                      "Dexterity: " + dex + " + DEX Modifier: " + dexMod + " = " + (dex + dexMod) + "\n" +
                      "Stamina: " + sta + " + STA Modifier: " + staMod + " = " + (sta + staMod) + "\n" +
                      "Inteligence: " + intel + " + INT Modifier: " + intelMod + " = " + (intel + intelMod) + "\n" +
                      "Charisma: " + cha + " + CHA Modifier: " + chaMod + " = " + (cha + chaMod) + "\n" 
                      ;
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
            throws SlickException {
        renderAll(g, sbg);

    }


    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta)
            throws SlickException {
        input = gc.getInput();

        mouseDown = input.isMouseButtonDown(0);
        mouseX = input.getMouseX();
        mouseY = input.getMouseY();

        checkGender();
        updatePreview();
        updateStatModifier();
        updateStatDisplay();
    }


    private void renderAll(Graphics g , StateBasedGame sbg) throws SlickException {

        // draw background
        g.setColor(Color.white);
        g.fillRect(875, 455, 305, 405);
        // draw background

        // draw class preview

         **classPreview.draw(880, 460, 300, 400);**  // Error is here

        // draw class preview

        // Select your class
        // Paladin
        if (mouseOver(875, 410, 80, 40)) {
            g.setColor(Color.darkGray);
            if (mouseDown) {
                className = "paladin";
            }
        } else {
            g.setColor(Color.blue);
        }
        g.fillRoundRect(875, 410, 80, 40, 5);
        g.setColor(Color.white);
        g.drawString("Paladin", 885, 420);
        // Paladin

        // Select your class

        // Select Gender
        genderSelector.draw(875, 300, 220, 100);
        // Select Gender

        // Roll for your stats
            // Roll button
                if(!mouseOver(100, 200, 100, 50)){
                    g.setColor(Color.white);
                }else if(mouseOver(100, 200, 100, 50)){
                    g.setColor(Color.darkGray);
                    if(mouseDown){
                        rollForStats();
                    }
                }
                    g.fillRoundRect(100, 200, 100, 50, 10);
                    g.setColor(Color.black);
                    g.drawString("Roll", 130, 215);
            // Roll button

            // Display stats
                    //Ugghhghghghghhghghghghgh!!!!!!
                    g.setColor(Color.white);
                    g.drawString(allTheStats, 80, 26);
            // Display stats

        // Roll for your stats

        // exit and write to file, then open new level
                if(!mouseOver(100, 600, 100, 50)){
                    g.setColor(Color.white);
                }else if(mouseOver(100, 600, 100, 50)){
                    g.setColor(Color.darkGray);
                    if(mouseDown){
                        closeUp(sbg);
                    }
                }
                g.fillRoundRect(100, 600, 100, 50, 10);

                g.setColor(Color.black);
                g.drawString("Play!", 130, 615);
        // exit and write to file, then open new level


    }

    private void closeUp(StateBasedGame sbg) throws SlickException {

        //set all that data
        player = new Player();

        player.setClassName(className);
        player.setName(name);
        player.setHealth(hitPoints);
        player.setStr(str);
        player.setDex(dex);
        player.setSta(sta);
        player.setCha(cha);
        player.setIntel(intel);
        player.setStaMod(staMod);
        player.setChaMod(chaMod);
        player.setDexMod(dexMod);
        player.setStrMod(strMod);
        player.setIntelMod(intelMod);
        player.setGender(male);

        //save to xml 
        XStream xstream = new XStream(new StaxDriver());
        String xml = xstream.toXML(player);

        write.writeToFile(xml, "save");

        // begin game
        //Main.ps.setFirstLoad(true);
        sbg.enterState(Main.PLAYSTATE, new FadeOutTransition(), new FadeInTransition());

        // prevent multiple clicks
        mouseDown = false;
    }

    private void rollForStats(){
        hitPoints = rand.nextInt(20) + 1;
        str = rand.nextInt(20) + 1;
        dex = rand.nextInt(20) + 1;
        sta = rand.nextInt(20) + 1;
        intel = rand.nextInt(20) + 1;
        cha = rand.nextInt(20) + 1;
    }

    private boolean mouseOver(int x, int y, int width, int height) {
        if (mouseX > x && mouseX < x + width && mouseY > y
                && mouseY < y + height) {
            return true;
        } else {
            return false;
        }
    }

    private void updateStatDisplay() {
        allTheStats = "Hitpoints: " + hitPoints + " + HP Modifier: " + hpMod + " = " + (hitPoints + hpMod) + "\n" +
                  "Strength: " + str + " + STR Modifier: " + strMod + " = " + (str + strMod) + "\n" +
                  "Dexterity: " + dex + " + DEX Modifier: " + dexMod + " = " + (dex + dexMod) + "\n" +
                  "Stamina: " + sta + " + STA Modifier: " + staMod + " = " + (sta + staMod) + "\n" +
                  "Inteligence: " + intel + " + INT Modifier: " + intelMod + " = " + (intel + intelMod) + "\n" +
                  "Charisma: " + cha + " + CHA Modifier: " + chaMod + " = " + (cha + chaMod) + "\n" 
                  ;
    }

    private void updatePreview() throws SlickException {
        switch(className){
        case "paladin":
            if(male){
                classPreview = new Image("gfx/player/previews/paladin_male.png");
            }else {
                classPreview = new Image("gfx/player/previews/paladin_female.png");
            }
            break;
        default: 
                classPreview = new Image("gfx/player/previews/paladin_male.png");
            break;
        }

    }

    private void checkGender() throws SlickException {
        if (!mouseOver(875, 300, 220, 100)) {
            genderSelector = new Image(
                    "gfx/extra/new_player/gender_selector.png");
        } else if (mouseOver(875, 300, 220, 100)) {
            if (mouseX > 985) {
                genderSelector = new Image(
                        "gfx/extra/new_player/male_selected.png");
                if (mouseDown) {
                    male = true;
                }
            } else {
                genderSelector = new Image(
                        "gfx/extra/new_player/female_selected.png");
                if (mouseDown) {
                    male = false;
                }
            }
        }

    }

    public void updateStatModifier() {
        switch (className) {
        case "paladin":
            if(male){
                hpMod = 6;
                strMod = 6;
                dexMod = 2;
                staMod = 4;
                intelMod = 5;
                chaMod = 3;
            }else{
                hpMod = 6;
                strMod = 3;
                dexMod = 2;
                staMod = 4;
                intelMod = 5;
                chaMod = 5;
            }
            break;
        }
    }

    @Override
    public int getID() {
        return stateID;
    }
}

1 个答案:

答案 0 :(得分:0)

而不是这个

classPreview.draw(880, 460, 300, 400);

你可以尝试

g.drawImage(classPreview, 800, ...

在这里查看slick2d Graphics类的不同drawImage()方法: http://slick.cokeandcode.com/javadoc/org/newdawn/slick/Graphics.html