• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

null Pointer Exception from applet viewer

 
Ranch Hand
Posts: 420
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Siegfried

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

at sun.awt.windows.Win32SurfaceData.initOps(Native Method)

at sun.awt.windows.Win32SurfaceData.<init>(Win32SurfaceData.java:448)

at sun.awt.windows.Win32SurfaceData.createData(Win32SurfaceData.java:316)

at sun.awt.Win32GraphicsConfig.createSurfaceData(Win32GraphicsConfig.java:357)

at sun.awt.windows.WComponentPeer.replaceSurfaceData(WComponentPeer.java:332)

at sun.awt.windows.WComponentPeer.replaceSurfaceData(WComponentPeer.java:313)

at sun.awt.windows.WComponentPeer$2.run(WComponentPeer.java:353)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)


Compilation finished at Sun Oct 08 10:33:09


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

/*
* See http://www.iam.ubc.ca/guides/javatut99/uiswing/start/swingApplet.html
*
* From Craig Wood https://coderanch.com/forums/
* $Log: GraphicsOnly.java,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 GraphicsOnly.java
* javac -g PanAndZoom.java
* cat <<EOF >PanAndZoomTmp.html
* <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
* <HTML>
* <HEAD>
* <TITLE>Pan And Zoom</TITLE>
* </HEAD>
* <BODY BGCOLOR="#COCOCO" TEXT="BLACK">
* <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>
* </APPLET>
* </BODY>
* </HTML>
* EOF
* 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;
repaint();
revalidate();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if (shapes == null)
initShapes();
// 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(Color.blue);
g2.draw(at.createTransformedShape(shapes[0]));
g2.setPaint(Color.green.darker());
g2.draw(at.createTransformedShape(shapes[1]));
g2.setPaint(new Color(240, 240, 200));
g2.fill(at.createTransformedShape(shapes[2]));
g2.setPaint(Color.red);
g2.draw(at.createTransformedShape(shapes[2]));
}
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);
slider.setMajorTickSpacing(50);
slider.setMinorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.addChangeListener(this);
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 = outer.new InnerGraphicsOnly();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(app));
f.getContentPane().add(app.getControl(), "Last");
f.setSize(400, 400);
f.setLocation(200, 200);
f.setVisible(true);
}
}
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic