-1

One of the JPanels in a swing GUI I am designing uses AWT graphics for drawing figures that are only drawable at runtime. Eclipse WindowBuilder no longer wants to draw any of my swing GUI as there are now null pointer exceptions in the awt paint code. Note that this is not a generic question about null pointer exceptions. This is specific to WindowBuilder and how to handle code that doesn't make sense in the context of WindowBuilder. Is there a method by which I could ignore the awt paint code if WindowBuilder is trying to interpret my swing gui?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class previewPanel extends JPanel {
private static final long serialVersionUID = 3787425749772291816L;
public final static int WIDTH = 918;
public final static int HEIGHT = 125;

public void paint(Graphics g) {
    BufferedImage img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_3BYTE_BGR);
    Graphics gBuffer = img.getGraphics();
    gBuffer.setColor(Color.WHITE);
    gBuffer.fillRect(0, 0, WIDTH, HEIGHT);

    businessClass.object.paint(gBuffer);

    g.drawImage(img, 0, 0, this);

    try {
        Thread.sleep(20);
    } catch (Exception e) {
    }
    repaint();
}

In this particular example, previewPanel is embedded inside of an overall swing gui I am working with in WindowBuilder and businessClass.object is only initialized at runtime (Main invokes the initialization of businessClass, not the gui itself). I have only shown one example, but there are many other cases where I call the painting code of a runtime object or list of objects that need to be drawn with awt graphics.

Andy
  • 237
  • 3
  • 14
  • 2
    The heuristic for debugging a NullPointerException is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Aug 22 '17 at 00:13
  • 2
    You've many other problems in that code -- never call sleep within a Swing gui much less its painting method and never call repaint within a painting method. Use a [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) instead. – Hovercraft Full Of Eels Aug 22 '17 at 00:14
  • Thanks for the suggestions. I will rework to be more swing compliant. I strongly disagree that this a duplicate of a generic null pointer question. I know exactly why I'm getting a null pointer exception: I explained it thoroughly in the original question. This is a question about multiple execution environments (actually running the program versus using WindowBuilder). I'm asking how to partition code that only makes sense in another environment. – Andy Aug 22 '17 at 18:14

1 Answers1

-2

I found a workaround by wrapping the run time only code in a null pointer check against the businessClass singleton. WindowBuilder won't pass this check, so the code is skipped over. It's a dirty solution. I was hoping for compile flags or whatnot.

Andy
  • 237
  • 3
  • 14