【问题标题】:getClass().getResource method not workinggetClass().getResource 方法不起作用
【发布时间】:2017-01-28 15:34:13
【问题描述】:

我一直在尝试运行此代码,以便能够查看指定文件夹中的图像,但我不断收到完整的错误列表,我不知道如何修复它们。

错误:线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException 在 fatface.FatFace.getImages(FatFace.java:27)

Problem is shown on this image

【问题讨论】:

  • 请勿发布图片或外部链接,请在此处复制代码。 mcve

标签: java image netbeans-8


【解决方案1】:

尝试使用

ClassLoader.getSystemClassLoader().getResource("fatface/pictures").getFile()

例子

File f = new File(ClassLoader.getSystemClassLoader().
             getResource("fatface/pictures/").getFile());  

System.out.println(Arrays.deepToString(f.list()));

输出: [温度.jpg]

【讨论】:

  • 他们为什么要试试这个?
  • 因为 getClass().getResource() 方法返回资源,如果资源在同一个包中,否则返回 null。看到这个帖子stackoverflow.com/questions/12103371/… 因此加载资源的正确方法是 ClassLoader.getSystemClassLoader().getResource()
  • 所有这些都属于你的答案。
猜你喜欢
  • 1970-01-01
  • 2015-05-28
  • 2018-11-10
  • 1970-01-01
  • 2013-01-13
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 2016-05-27
相关资源
最近更新 更多