从方法调用的变量不起作用

时间:2014-05-01 21:36:37

标签: java

我正在尝试为我的RPG游戏创建一个文本框效果,但我不断获得nullpointers,因为我的变量为null,但我在一个方法中设置它们。我还需要能够更新方法中的变量,这是无效的。这就是NPE正在发生的原因。这是我的代码:

    package me.loafayyy.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Timer;
import java.util.TimerTask;

import me.loafayyy.main.RPG;
import me.loafayyy.main.RPGPane;
import me.loafayyy.main.RPGState;

public class GuiManager {

    public int opacity = 0;
    public int step = 0;
    public static boolean transitioning = false;
    public static boolean dialogue = false;
    public  String textlines1;
    public String textlines2;
    public String textlines3;

    public boolean rect = false;

    public Timer timer = new Timer();

    public void createTransition() {
        transitioning = true;
        rect = true;
    }

    public void setOpacity(int op) {
        opacity = op;
    }

    public int getOpacity() {
        return opacity;
    }

    public  void createDialogue(String line1, String line2, String line3, int dur) { // my method which I'm setting the variables.
        if (!dialogue) {
            dialogue = true;
            this.textlines1 = line1;
            this.textlines2 = line2;
            this.textlines3 = line3;

            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    dialogue = false;
                }

            }, dur);
        } else {
            System.out
                    .println("ERROR: Could not create dialogue, one is already displaying!");
        }
    }

    public void render(Graphics g) {

        for (int i = 0; i < 4; i++) {
            if (transitioning) {
                rect = false;
                if (step == 0) {
                    if (opacity < 255) {
                        setOpacity(getOpacity() + 1);
                    } else if (opacity == 255) {
                        step = 1;
                    }
                } else if (step == 1) {
                    if (opacity < 256 && opacity != 0) {
                        setOpacity(getOpacity() - 1);
                    } else if (opacity == 0) {
                        transitioning = false;
                        step = 0;
                    }
                }
            }
        }

        Graphics2D g2 = (Graphics2D) g.create();
        if (transitioning) {
            if (RPG.state == RPGState.MAIN) {
                RPGPane.main.render(g);
            } else if (RPG.state == RPGState.PAUSED) {
                RPGPane.paused.render(g);
            } else if (RPG.state == RPGState.INGAME) {
                RPGPane.ingame.render(g);
            }
            g2.setColor(new Color(0, 0, 0, getOpacity()));
            g2.fillRect(0, 0, RPG.getWidth(), RPG.getHeight());
        }

        if (RPG.state == RPGState.INGAME) {
            if (dialogue) {
                g2.drawString(textlines1, 300, 300); // <--- null right here
                // do other textlines
            }
        }
    }
}

新代码:

  package me.loafayyy.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Timer;
import java.util.TimerTask;

import me.loafayyy.main.RPG;
import me.loafayyy.main.RPGPane;
import me.loafayyy.main.RPGState;

public class GuiManager {

    public int opacity = 0;
    public int step = 0;
    public static boolean transitioning = false;
    public static boolean dialogue = false;
    public  String textlines1;
    public String textlines2;
    public String textlines3;

    public boolean rect = false;

    public Timer timer = new Timer();

    public GuiManager () {
        textlines1 = "";
    }

    public void createTransition() {
        transitioning = true;
        rect = true;
    }

    public void setOpacity(int op) {
        opacity = op;
    }

    public int getOpacity() {
        return opacity;
    }


    public void render(Graphics g) {

        for (int i = 0; i < 4; i++) {
            if (transitioning) {
                rect = false;
                if (step == 0) {
                    if (opacity < 255) {
                        setOpacity(getOpacity() + 1);
                    } else if (opacity == 255) {
                        step = 1;
                    }
                } else if (step == 1) {
                    if (opacity < 256 && opacity != 0) {
                        setOpacity(getOpacity() - 1);
                    } else if (opacity == 0) {
                        transitioning = false;
                        step = 0;
                    }
                }
            }
        }

        Graphics2D g2 = (Graphics2D) g.create();
        if (transitioning) {
            if (RPG.state == RPGState.MAIN) {
                RPGPane.main.render(g);
            } else if (RPG.state == RPGState.PAUSED) {
                RPGPane.paused.render(g);
            } else if (RPG.state == RPGState.INGAME) {
                RPGPane.ingame.render(g);
            }
            g2.setColor(new Color(0, 0, 0, getOpacity()));
            g2.fillRect(0, 0, RPG.getWidth(), RPG.getHeight());
        }

        if (RPG.state == RPGState.INGAME) {
            if (dialogue) {
                g2.drawString(textlines1, 300, 300);
                // do other textlines
            }
        }
    }

    public  void createDialogue(String line1, String line2, String line3, int dur) {
        if (!dialogue) {
            dialogue = true;
            this.textlines1 = line1;
            this.textlines2 = line2;
            this.textlines3 = line3;

            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    dialogue = false;
                }

            }, dur);
        } else {
            System.out
                    .println("ERROR: Could not create dialogue, one is already displaying!");
        }
    }
}

我是用另一个班级来调用它的:

mng.createDialogue("test1", "test2", "test3", 1000);

堆栈跟踪:

Exception in thread "Thread-10" java.lang.NullPointerException: String is null
at sun.java2d.SunGraphics2D.drawString(Unknown Source)
at me.loafayyy.gui.GuiManager.render(GuiManager.java:98)
at me.loafayyy.main.RPGPane.render(RPGPane.java:107)
at me.loafayyy.main.RPGPane.run(RPGPane.java:82)
at java.lang.Thread.run(Unknown Source)

RPGPane是我的JPanel。

1 个答案:

答案 0 :(得分:1)

我认为问题是您可以在render方法之前调用createDialogue方法。因此,textlines1可以取消分配/ null。

解决此问题的一种方法是接受@ Takendarkk的建议并将变量初始化为“”。另一种方法是问自己,“如果没有设置textlines1,这个类是否有意义?如果没有,可以在GuiManager的构造函数中设置它。