david
david

Reputation: 89

NullPointerException when trying to draw a polygon

When I'm trying to draw a polygon I get a NullPointerException. This is how I am trying to do it:

Roi roi = imp.getRoi();
Polygon p = roi.getPolygon();
Graphics g=null;
g.drawPolygon(p.xpoints, p.ypoints, p.npoints);

Please suggest how I can fix this.

Upvotes: 0

Views: 184

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285460

You're not using the right Graphics object. This should be done in a JComponent's paintComponent(...) method, and you should use the Graphics object passed in by the JVM. Either that or draw this in a BufferedImage using a Graphics2D object obtained from it via createGraphics().

Regarding your edit: yikes!

This is guaranteed to throw a NPE every time.

Bar bar = null
bar.someMethod(); 

Upvotes: 1

Related Questions