Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Canvas in JInternalFrame throws NPE

0 views
Skip to first unread message

Stewart Gordon

unread,
Mar 12, 2002, 7:25:12 PM3/12/02
to
The code below is part of a project I'm doing.

At this line in the code below:

win.setVisible(true);

it stops with a NullPointerException. It seems to work OK if I put the
canvas straight into a JFrame or Frame.

----------
Exception in thread "main" java.lang.NullPointerException: peer
at sun.awt.windows.WCanvasPeer.create(Native Method)
<snip 18 lines>
at javax.swing.JComponent.setVisible(JComponent.java:1885)
at AnimationWindow.main(AnimationWindow.java:30)
----------
// comments removed in this sample
import java.awt.*;
import javax.swing.*;

public class AnimationWindow extends JInternalFrame {
Canvas cvs;

public AnimationWindow() {
super("Pendula", true, true, true, true);

cvs = new Canvas();
getContentPane().add(cvs);

setSize(300, 300);
}

public static void main(String[] a) {
JFrame frame = new JFrame();
JDesktopPane desktop = new JDesktopPane();
AnimationWindow win = new AnimationWindow();

desktop.add(win);
win.setVisible(true);

frame.setContentPane(desktop);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
----------

Any ideas?

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on the
'group where everyone may benefit.


Vilya Harvey

unread,
Mar 12, 2002, 8:50:06 PM3/12/02
to
Stewart Gordon wrote:
> The code below is part of a project I'm doing.
>
> At this line in the code below:
>
> win.setVisible(true);
>
> it stops with a NullPointerException. It seems to work OK if I put
> the canvas straight into a JFrame or Frame.

<snipped the code example>

> Any ideas?

Yep. Canvas is a heavyweight component, but a JInternalFrame is a
lightweight one. I'd guess the problem you're seeing is a symptom of that.

Use a JPanel, or any other class derived from JComponent, instead of the
Canvas and the problem should go away.

Vil.
--
Vilya Harvey
vi...@lineone.net
http://website.lineone.net/~vilya

Stewart Gordon

unread,
Mar 14, 2002, 5:41:03 AM3/14/02
to
Vilya Harvey <vi...@lineone.net> wrote in message
news:3C8EB04E...@lineone.net...
<snip>

> Yep. Canvas is a heavyweight component, but a JInternalFrame is a
> lightweight one. I'd guess the problem you're seeing is a symptom of that.
>
> Use a JPanel, or any other class derived from JComponent, instead of the
> Canvas and the problem should go away.

Thanks. I've ended up subclassing JComponent and making this the whole
content pane.

0 new messages