-2

When I override the paint method of my GamePanel class, then, when I try to open the MazeGUI Design Tab, I get this error:

Internal Error
WindowBuilder encountered unexpected internal error. 

This could be caused by a WindowBuilder bug or by a misconfiguration issue, conflict,  partial update, etc.

Stack trace:
java.lang.NullPointerException
at maze.gui.GamePanel.paint(GamePanel.java:25)  
at javax.swing.JComponent.print(Unknown Source)  
at javax.swing.JComponent.paintChildren(Unknown Source)  
at javax.swing.JComponent.printChildren(Unknown Source)  
at javax.swing.JComponent.paint(Unknown Source)  
at javax.swing.JComponent.print(Unknown Source)  
at javax.swing.JComponent.paintChildren(Unknown Source)  
at javax.swing.JComponent.printChildren(Unknown Source)  
at javax.swing.JComponent.paint(Unknown Source)  

If I delete this line at GamePanel.java at paint override function

g.drawImage(maze.wall, 400, 400, null);

the design tab at MazeGUI already works. Very strange...

Anyway, if I run the java application, it displays the image without any errors.

Here I leave both classes java files: GamePanel.java:

package maze.gui;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class GamePanel extends JPanel {
    private int comprimento_sprites;
    private MazeGUI maze;

/**
 * Create the panel.
 */
public GamePanel(MazeGUI m) {
    comprimento_sprites = 40;
    maze = m;
    setFocusable(true);
}

@Override
public void paint(Graphics g) {
    super.paint(g);

g.drawImage(maze.wall, 400, 400, null);
}

}

MazeGUI.java:

package maze.gui;

import java.awt.EventQueue;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import maze.logic.Jogo;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class MazeGUI {

private JFrame frmMazeGame;
//IMAGES
    public BufferedImage wall;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MazeGUI window = new MazeGUI();
                window.frmMazeGame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MazeGUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    //faz load das imagens do jogo
    loadImages();

    frmMazeGame = new JFrame();
    frmMazeGame.setTitle("Maze Game");
    frmMazeGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    GamePanel tabuleiro = new GamePanel(this);
    frmMazeGame.getContentPane().add(tabuleiro);
}

private void loadImages(){

    try {
        wall = ImageIO.read(new File("images/bloco.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

Ah, let me add this: - I think all this problem is related to the loadImages function at MazeGUI, but I think I do it all alright. Already lost so many hours trying to fix this.
- Keep in mind: no error when I run the app (it even shows the image in the app) but I get the error above when I try to open the Design tab :/

Miguel Cunha
  • 663
  • 8
  • 16
  • Could you clean up the code to only show the important parts, and just the first parts of the stacktrace? Its getting very messy and complicated. – Haedrian Apr 10 '13 at 20:41
  • Done! :) I think you can understand much better the code now... – Miguel Cunha Apr 10 '13 at 20:45
  • 1
    The execution context for the design tab and the execution context when you run the program is different. This means that the design view can't find the image. Also. Don't override paint, use paintComponent instead and make sure you're calling super.paintComponent as well. Instead of basing the core logic on within the frame, use the panel instead. This makes your program more flexible – MadProgrammer Apr 10 '13 at 20:48
  • MadProgrammer: tried to use paintComponent and the problem still exists :s – Miguel Cunha Apr 10 '13 at 21:00

1 Answers1

0

You receive the error when loading the Design tab because of this line:

g.drawImage(maze.wall, 400, 400, null);

I don't have much experience with WindowBuilder, so I can't tell you how it works exactly, but it seems as though your MazeGUI object in the GamePanel is null when in Design mode. It's not a problem with your image loading, it's simply because of however WindowBuilder works.

To get the Design tab to load, you can simply throw a check for null in there:

if (maze != null && maze.wall != null) {
    g.drawImage(maze.wall, 400, 400, null);
}

As far as getting the image to actually show in the Design tab, I'm afraid I can't help you.

Ryan
  • 672
  • 8
  • 15