java.lang.NullPointerException component argument pData
I've been searching all sources possible to find out where a chain of errors ending in "NullPointerException: component argument pData" comes from and have found nothing that can help solve my problem.I was told that because this related to full-screen settings, the Gaming Forums might be able to help. My code runs but ends with a series of errors that occasionally freezes my computer when the program closes. The errors provide no reference to where it is originating in my program, and I've confirmed that it takes place after the final command in my program. I have no idea what to fix to end this error; I've narrowed down the flawed coding to a relatively small block, and to recreate the error, simply run the coding. I would GREATLY appreciate any help on this, as I have futilely spent countless attempting to fix it. And thanks in advance.
// creates the main block of code for this Test program
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import javax.swing.*;
import java.util.Random;
import java.util.LinkedList;
import java.util.Iterator;
import java.awt.Graphics2D.*;
import java.io.*;
public class TestError implements KeyListener
{
private final DisplayMode POSSIBLE_MODES[] = {
new DisplayMode(800, 600, 32, 0),
new DisplayMode(800, 600, 24, 0),
new DisplayMode(800, 600, 16, 0),
new DisplayMode(640, 480, 32, 0),
new DisplayMode(640, 480, 24, 0),
new DisplayMode(640, 480, 16, 0)};
private ScreenManager screen;
public boolean isRunning, testingFlag = false;
public static void main(String[] args) throws IOException
{
TestError OOG = new TestError();
OOG.run();
}
//manages running the important methods of the program
public void run() throws IOException
{
isRunning = true;
screen = new ScreenManager();
try {
DisplayMode displayMode =
screen.findFirstCompatibleMode(POSSIBLE_MODES);
screen.setFullScreen(displayMode);
Window window = screen.getFullScreenWindow();
window.addKeyListener(this);
animationLoop();}
finally {
screen.restoreScreen();
System.out.println("Test1");
}
}
public Image loadImage(String fileName)
{
return new ImageIcon(fileName).getImage();
}
public void animationLoop()
{
long startTime = System.currentTimeMillis();
long currTime = startTime;
while (isRunning)
{
long elapsedTime = System.currentTimeMillis() - currTime;
currTime += elapsedTime;
//call the Graphics2d class, draw the image, then dispose Graphics
Graphics2D g = screen.getGraphics();
draw(g);
screen.update();
g.dispose();
//nap time
try { Thread.sleep(20); }
catch (InterruptedException ex) {}
}
}
public void keyPressed (KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ESCAPE)
{
isRunning = false;
}
e.consume();
}
public void keyTyped(KeyEvent e)
{
e.consume();
}
public void keyReleased(KeyEvent e)
{
e.consume();
}
public void draw(Graphics g)
{
int drawOffsetX, drawOffsetY; /*the offset will change where the pics are drawn
so that the screen scrolls with the char*/
drawOffsetX = 0;
drawOffsetY = 0;
//draws a simple background
g.setColor(Color.BLUE);
g.fillRect(0,0,800,800);
}
}
//manages initializing and displaying full screen graphics mode
class ScreenManager
{
private GraphicsDevice device;
//creates and instance of ScreenManager
public ScreenManager()
{
GraphicsEnvironment environment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
}
//returns the list of compatible display modes for this system
public DisplayMode[] getCompatibleDisplayModes()
{
return device.getDisplayModes();
}
//finds the first compatible display mode
//returns null if no compatible modes are found
public DisplayMode findFirstCompatibleMode(DisplayMode modes[])
{
DisplayMode goodModes[] = device.getDisplayModes();
for (int i = 0; i < modes.length; i++)
{
for (int j = 0; j < goodModes.length; j++)
{
if(displayModesMatch(modes, goodModes[j]))
return modes;
}
}
return null;
}
//obtains the current display mode
public DisplayMode getCurrentDisplayMode()
{
return device.getDisplayMode();
}
/* Determines if two display modes match (if they have the same resolution,
* bit depth, and refresh rate. The bit depth is ignored if one mode has a
* bit depth of DispalyMode.BIT_DEPTH_MULTI; refresh rate is ignored if one
* is DisplayMode.REFRESH_RATE_UNKNOWN.*/
public boolean displayModesMatch(DisplayMode mode1, DisplayMode mode2)
{
if (mode1.getWidth() != mode2.getWidth() ||
mode1.getHeight() != mode2.getHeight())
{
return false;
}
if (mode1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
mode2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
mode1.getBitDepth() != mode2.getBitDepth())
{
return false;
}
if (mode1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN &&
mode2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN &&
mode1.getRefreshRate() != mode2.getRefreshRate())
{
return false;
}
return true;
}
/* Changes the window to the full screen. If it has to be, the current
* display mode is used. The display uses a ButterStrategy with two
* buffers in order to allow the drawing to be more effective*/
public void setFullScreen(DisplayMode displayMode)
{
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setIgnoreRepaint(true);
frame.setResizable(false);
device.setFullScreenWindow(frame);
if (displayMode != null && device.isDisplayChangeSupported())
{
try
{
device.setDisplayMode(displayMode);
}
catch(IllegalArgumentException ex){}
}
frame.createBufferStrategy(2);
}
/* Get graphics context for the display. The ScreenManager uses double
* buffering, so applications MUST call update() to show any graphics
* drawn... there is NO automatic repaint();
* The application must dispose of the graphics context*/
public Graphics2D getGraphics()
{
Window window = device.getFullScreenWindow();
if (window != null)
{
BufferStrategy strategy = window.getBufferStrategy();
return (Graphics2D)strategy.getDrawGraphics();
}
else
{
return null;
}
}
//update method, which must be called (there is no auto repaint)
public void update()
{
Window window = device.getFullScreenWindow();
if (window != null)
{
BufferStrategy strategy = window.getBufferStrategy();
if (!strategy.contentsLost())
{
strategy.show();
}
}
//Sync the display on some problems, like Linux
Toolkit.getDefaultToolkit().sync();
}
//returns the window currently used in full screen mode
public Window getFullScreenWindow()
{
return device.getFullScreenWindow();
}
//returns width or height currently used in full screen mode
public int getWidth()
{
Window window = device.getFullScreenWindow();
if (window != null)
{
return window.getWidth();
}
else
{
return 0;
}
}
public int getHeight()
{
Window window = device.getFullScreenWindow();
if (window != null)
{
return window.getHeight();
}
else
{
return 0;
}
}
//restores the screen's display mode
public void restoreScreen()
{
Window window = device.getFullScreenWindow();
if (window != null)
{
window.dispose();
}
device.setFullScreenWindow(null);
}
//creates an image compatible with the current display
public BufferedImage createCompatibleImage(int w, int h,
int transparency)
{
Window window = device.getFullScreenWindow();
if (window != null)
{
GraphicsConfiguration gc = window.getGraphicsConfiguration();
return gc.createCompatibleImage(w, h, transparency);
}
return null;
}
}

