BufferedImage 的 getGraphics() 中出现空指针异常?
public void saveImage(String path){
BufferedImage image = (BufferedImage) createImage(500, 500);
Graphics gImage = image.getGraphics(); //<<<<<<<<--- exception
paint(gImage);
image = image.getSubimage(0,0,500,500);
try {
ImageIO.write(image, "png", new File(path+".png"));
}
catch (Exception e){}
}
问题出在哪里??
public void saveImage(String path){
BufferedImage image = (BufferedImage) createImage(500, 500);
Graphics gImage = image.getGraphics(); //<<<<<<<<--- exception
paint(gImage);
image = image.getSubimage(0,0,500,500);
try {
ImageIO.write(image, "png", new File(path+".png"));
}
catch (Exception e){}
}
Where is the problem ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,方法
createImage(int, int)
返回null
。原因在 文档:Apparently the method
createImage(int, int)
is returningnull
. The reason is explained in the documentation:您的 NPE 可能不是来自 getGraphics,而是来自尝试取消引用 null
image
变量。如果您的组件不可显示,createImage
将返回null
。Your NPE is probably not coming from getGraphics but from trying to dereference a null
image
variable. If your component is not displayable,createImage
returnsnull
.