简体   繁体   English

尝试绘制多边形时发生NullPointerException

[英]NullPointerException when trying to draw a polygon

When I'm trying to draw a polygon I get a NullPointerException . 当我尝试绘制多边形时,出现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. 您没有使用正确的Graphics对象。 This should be done in a JComponent's paintComponent(...) method, and you should use the Graphics object passed in by the JVM. 这应该在JComponent的paintComponent(...)方法中完成,并且应该使用JVM传递的Graphics对象。 Either that or draw this in a BufferedImage using a Graphics2D object obtained from it via createGraphics() . 或者使用通过createGraphics()从其获得的Graphics2D对象在BufferedImage中绘制此对象。

Regarding your edit: yikes! 关于您的编辑:

This is guaranteed to throw a NPE every time. 这样可以确保每次都抛出一个NPE。

Bar bar = null
bar.someMethod(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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