File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes null Pointer Exception from applet viewer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "null Pointer Exception from applet viewer" Watch "null Pointer Exception from applet viewer" New topic

null Pointer Exception from applet viewer

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 397
After Craig Wood (in the Other API's forum) graciously provided me with a nice swing Java2D example, I wanted to run it is as an applet. I have succeeded in converting it -- sort of.

I'm getting the stack trace below when I run it in the applet viewer. (The stack trace is from the appletviewer, not my code).
Below that, I have posted my modification of Craig's swing example.

I suspect there is something wrong with the way I converted his swing example to an applet. Craig uses a JScrollPane in his main program and I wanted JScrollPane within the Applet. So I created an JApplet within the JScrollPain within a second JApplet. Was JApplet overkill for the inner applet: could I have used JPanel?

Thank you,

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: component argument pData

at Method)



at sun.awt.Win32GraphicsConfig.createSurfaceData(




at java.awt.event.InvocationEvent.dispatch(

at java.awt.EventQueue.dispatchEvent(

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(

at java.awt.EventDispatchThread.pumpEventsForHierarchy(

at java.awt.EventDispatchThread.pumpEvents(

at java.awt.EventDispatchThread.pumpEvents(


Compilation finished at Sun Oct 08 10:33:09

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;

* See
* From Craig Wood
* $Log:,v $
* Revision 1.2 2006/10/07 21:14:45 siegfried
* Add applet compatibility.
* Revision 1.1 2006/10/07 19:54:31 siegfried
* initial from Craig Wood
* Begin commands to execute this file using Java with bash
* export CLASSPATH=.
* javac -g
* javac -g
* cat <<EOF >PanAndZoomTmp.html
* <HTML>
* <HEAD>
* <TITLE>Pan And Zoom</TITLE>
* </HEAD>
* <H1>Graphics Only</H1>
* <HR>
* <H2>Example:</H2>
* <APPLET CODE="GraphicsOnly.class" WIDTH=500 HEIGHT=400 >
* <B>Error! You must use a Java enabled browser.</B>
* </BODY>
* </HTML>
* appletviewer PanAndZoomTmp.html
* rm PanAndZoomTmp.html
* # IEXPLORE file://PanAndZoom.html
* # java GraphicsOnly
* rm GraphicsOnly.class
* End commands to execute this file using Java with bash
public class GraphicsOnly extends JApplet {
class InnerGraphicsOnly extends JPanel implements ChangeListener {
Shape[] shapes;
Dimension size;
double scale = 1.0;
public InnerGraphicsOnly() {
size = new Dimension(10, 10);
setBackground(new Color(240, 200, 200));
public void stateChanged(ChangeEvent e) {
int value = ((JSlider) e.getSource()).getValue();
scale = value / 100.0;
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
if (shapes == null)
// Keep shapes centered on panel.
double x = (getWidth() - scale * size.width) / 2;
double y = (getHeight() - scale * size.height) / 2;
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.scale(scale, scale);
g2.setPaint(new Color(240, 240, 200));
public Dimension getPreferredSize() {
int w = (int) (scale * size.width);
int h = (int) (scale * size.height);
return new Dimension(w, h);
private void initShapes() {
shapes = new Shape[3];
int w = getWidth();
int h = getHeight();
shapes[0] = new Rectangle2D.Double(w / 16, h / 16, w * 7 / 8, h * 7 / 8);
shapes[1] = new Line2D.Double(w / 16, h * 15 / 16, w * 15 / 16, h / 16);
shapes[2] = new Ellipse2D.Double(w / 4, h / 4, w / 2, h / 2);
size.width = w;
size.height = h;
private JSlider getControl() {
JSlider slider = new JSlider(JSlider.HORIZONTAL, 10, 400, 100);
return slider;

public void init(){
GraphicsOnly.InnerGraphicsOnly app = new InnerGraphicsOnly();
getContentPane().add(new JScrollPane(app));
getContentPane().add(app.getControl(), "Last");
public static void main(String[] args) {
GraphicsOnly outer = new GraphicsOnly();
GraphicsOnly.InnerGraphicsOnly app = InnerGraphicsOnly();
JFrame f = new JFrame();
f.getContentPane().add(new JScrollPane(app));
f.getContentPane().add(app.getControl(), "Last");
f.setSize(400, 400);
f.setLocation(200, 200);
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I'm getting the stack trace below when I run it in the applet viewer.
I get the same exception although it doesn't occur everytime I close the appletviewer. This started in j2se 1.5 and did not happen in j2se 1.4. I never researched it and have not been able to find a way to avoid it; just figured someone would get it worked out eventually.
Was JApplet overkill for the inner applet: could I have used JPanel?
I haven't tried your code but probably, yes. An Applet is an extension of Panel (AWT) and a JApplet is and extension of Applet (Swing). So a JApplet can host a JPanel. What makes an Applet/JApplet special is that they are top–level containers.
Here is how I would create a JApplet to host the GraphicsOnly component:

The only change required in the GraphicsOnly file is relaxing the access modifier of the getControl method from private to protected so the applet can call/access it.
I agree. Here's the link:
subject: null Pointer Exception from applet viewer
It's not a secret anymore!