简体   繁体   English

为什么我的`getClass()。getResource(“ logs”)`返回null?

[英]why is my `getClass().getResource(“logs”)` returns null?

I have this code: 我有以下代码:

this.fileName = getClass().getResource("logs").toURI().toString();

which fails for null pointer exception because getClass().getResource("logs") returns null 由于getClass().getResource("logs")返回null ,因此失败导致null指针异常

what am I missing? 我想念什么?

在此处输入图片说明

You're looking for a resource which is relative to the LogUtils class. 您正在寻找与LogUtils类相关的资源。

You could use getClass().getResource("/logs") or you could use getClass().getClassLoader().getResource("logs") . 您可以使用getClass().getResource("/logs") ,也可以使用getClass().getClassLoader().getResource("logs")

(In either case, I'm not 100% convinced it'll work when that's a folder rather than an actual resource...) (无论哪种情况,我都不是100%确信当它是文件夹而不是实际资源时它会工作...)

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

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