Paint和paintComponent的NullPointerException问题

时间:2018-12-11 15:27:56

标签: java nullpointerexception

我一直在阅读有关NullPointerException的信息,以及与它在同一页上的Graphics有关的问题-因为它们与我所面临的问题密切相关。

我正在尝试通过使用paint和paintComponent将符号映射转换为2D切片,并且我俩都一直面对NullPointerException,但是我似乎找不到在需要的地方错误地应用了null值一个对象。

这是完整的代码:

(MapsBasic是一个地图生成器类,而TileType只是一个充满Image tile的类)

public class Launcher extends JFrame {

private BufferedImage Background
        = ImageIO.read(new FileInputStream("res/textures/background2.png"));
protected BufferedImage image
        = ImageIO.read(new FileInputStream("res/textures/floortileset.png"));
protected TileSet objectSet = new TileSet(image);
private MapsBasic meps = new MapsBasic();
private TileType type;
private char[] c = meps.getSymbolList();
private char[][] t = meps.getTiles();
// private Image dbImage;
private Graphics g;
private int width, height;

public Launcher() throws IOException {
    width = 480;
    height = 480;
    setTitle("GG RPG");
    setSize(width, height); //width and height in pixels
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void paintComponent (Graphics g) {
    meps.getSymbolList();
    meps.generateDisplayRandomSymbols();

    for (int i = 0; i < meps.getSize(); i++) {
        for (int j = 0; j < meps.getSize(); j++) {
            if (t[i][j] == c[1]) {
                g.drawImage(type.enemy(), i * 32, j * 32, null);
            }
            if (t[i][j] == c[2]) {
                g.drawImage(type.npc(), i * 32, j * 32, null);
            }
            if (t[i][j] == c[0]) {
                g.drawImage(type.player(), i * 32, j * 32, null);
            } else {
                for (int k = 3; k < c.length; k++) {

                    if (c[k] == ';') {
                        g.drawImage(type.grass1(), i * 32, j * 32, null);
                    }
                    if (c[k] == '`') {
                        g.drawImage(type.grass2(), i * 32, j * 32, null);
                    }
                    if (c[k] == '.') {
                        g.drawImage(type.grass3(), i * 32, j * 32, null);
                    }
                    if (c[k] == ',') {
                        g.drawImage(type.grass4(), i * 32, j * 32, null);
                    }
                    if (c[k] == '^') {
                        g.drawImage(type.grass5(), i * 32, j * 32, null);
                    }
                    if (c[k] == '#') {
                        g.drawImage(type.grass6(), i * 32, j * 32, null);
                    }
                    if (c[k] == '*') {
                        g.drawImage(type.water(), i * 32, j * 32, null);
                    }
                    if (c[k] == '=') {
                        g.drawImage(type.desert(), i * 32, j * 32, null);
                    }
                }
            }
        }
    }

}

public void paint(Graphics g) {
    super.paint(g);
    g.setColor(Color.black);
    g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
    g.drawImage(Background, 1, 1, 500, 500, null);
    g.drawString("Message", 180, 265);
    g.drawString("Message2 :)", 60, 280);

    //start game
    Scanner input = new Scanner(System.in);
    int wasd = input.next().charAt(0);
    if (wasd == 'y' || wasd == 'Y') {
        g.clearRect(0, 0, 480, 480);
    }

    //dbImage = createImage(getWidth(), getHeight());
    //g = dbImage.getGraphics();
    paintComponent(g);

}

这是给出的异常消息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ggrpg.Launcher.paintComponent(Launcher.java:61)
at ggrpg.Launcher.paint(Launcher.java:108)
at javax.swing.RepaintManager$4.run(RepaintManager.java:842)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

老实说,我只是想寻找一种替代的方法-但是我真的对如何将随机生成的符号图转换为图形没有其他想法,而我只停留在这个唯一的想法上。 / p>

1 个答案:

答案 0 :(得分:0)

问题似乎是您从未初始化type。因此,无论何时调用type.enemy()之类的东西,都会抛出NullPointerException,因为尚未定义type

解决方案是执行类似private TileType type = new TileType();

的操作