Ilja Ilja - 7 months ago 17
Java Question

How to set a class visible from another?

I have a drawing board with some circles, triangles and rectangles. I want to be able to set them visible and not.

The classes name are

Circle
,
Triangle
,
Rectangle
and is extending
JComponent
, and they all have an unique color.

Someone suggested
Triangle c = null;
and then use
c.setVisible(true)
to make it visible - but it only gives
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
referring to
c.setVisible(true)


Anyone know a solution to this?

Answer

Please use :

Triangle c = new Triangle();

c.setVisible(true);

Instead of null

Reference variables with null value will always give you NullPointerException when you call it with dot(.) operator.