简体   繁体   中英

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.

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(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM