如何在使用getScaledInstance时避免出现空指针引用错误?

huangapple go评论77阅读模式
英文:

How to I avoid getting Null Pointer Deference after typing while using getScaledInstance?

问题

以下是翻译好的内容:

为了简洁起见,这是我的目标:

  • 将照片比例设置为 jPanel。最好的方式是
  • 它可以同时切换照片。(我正在使用 jLabels 来获取图标)

附:

  • 我可能需要向照片添加水印,所以最好您也可以帮助我。谢谢。
BufferedImage img = null;
try {
    img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
    e.printStackTrace();
}
Image dimg = img.getScaledInstance(jPanel.getWidth(), jPanel.getHeight(), Image.SCALE_SMOOTH);

我可以知道这个错误是在谈论什么吗?

空指针引用
英文:

To keep it short here is my objective:

  • to get the photo scale to the jPanel. the best
  • it could can switch photos at the same time. (I'm using jLabels to get icon)

ps:

  • I might need to add watermark to the photo so it may be best that you can help me too. Thank You.
BufferedImage img = null;
try {
    img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
    e.printStackTrace();
}
Image dimg = img.getScaledInstance(jPanel.getWidth(), jPanel.getHeight(),Image.SCALE_SMOOTH);

can i know is this error talking about:

null pointer deference 

答案1

得分: 3

你的异常处理是问题所在。

你的异常处理程序表示:

如果出现问题,向标准错误输出打印一些(但不是全部)有关问题的调试信息,然后继续执行代码。

这很少是正确的做法。

处理异常的最佳方式是实际上对其进行处理。仅记录日志并不能算作处理。

如果你无法处理它(在这种情况下,嘿,如何处理一个关键文件不存在呢? - 你不能,这是不合理的。也没有人指望你编写代码来处理已删除一半类文件的情况,对吧?) - 那么正确的替代方案就是将其继续抛出 - 在你的方法中添加throws。如果你不能这样做,正确的“呃,我不知道”答案是这样的:

} catch (IOException e) {
    throw new RuntimeException("未处理的异常", e);
}

然后空指针异常就不会发生。相反,你会得到有关实际问题的适当信息,即strawberry.jpg不在JVM进程的当前工作目录中。

要解决你实际问题的方法:如果你有静态资源,比如用户界面的草莓图片,将它们放在类文件最终所在的位置,并使用YourClass.class.getResource("strawberry.jpg")来获取它们;这将使用Java用于查找应用程序类文件的相同机制,即它甚至可以从你的JAR文件中获取它们,避免了任何与当前工作目录相关的问题。

英文:

Your exception handling is the problem.

Your exception handler says:

If a problem occurs, print some (but not all) debug info about it to standard error, and then keep on going with the code.

That is rarely the right move.

The best way to handle an exception is to actually handle it. Logging it isn't handling it.

If you can't handle it (and in this case, hey, how do you 'handle' a crucial file not being there? - You don't, that's not reasonable. Nobody expects you to write code to deal with half of your class files having been deleted either, right?) - then the right alternative is to just throw it onwards - add throws to your method. And if you can't do that, the right 'uhhh I dunno' answer is this:

} (catch IOException e) {
    throw new RuntimeException("unhandled", e);
}

and then the NPE would never have happened. Instead, you get proper info on the actual problem here, which is that strawberry.jpg is not in the current working directory of the JVM process.

To get to the solution to your actual problem: If you have static resources, such as an image of a strawberry for your user interface, put then where your class files end up and use YourClass.class.getResource("strawberry.jpg") to get at them; this uses the same mechanism java uses to find the class files of your app, i.e. it will even get them from within your jar files and such, and avoids any issues with current working directories.

huangapple
  • 本文由 发表于 2020年9月19日 16:49:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63966942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定