首页 \ 问答 \ ClassLoader.getResource()上的NullPointerException(NullPointerException on ClassLoader.getResource())

ClassLoader.getResource()上的NullPointerException(NullPointerException on ClassLoader.getResource())

我正在开发一个小项目,我需要将文件从应用程序JAR移动到JAR之外的另一个位置。

这就是我的JAR文件的样子:

MyApp.JAR
   |- META-INF
       |- MANIFEST.MF
   |- files
       |- myFile.exe
   |- MainClass.class

我想做的是将myFile.exe移动到C:\\ (例如)。 所以在MainClass我试过:

23. Path path = Paths.get(getClass().getClassLoader().getResource("/files/myFile.exe").toURI());
24. byte[] bytes = Files.readAllBytes(path);

25. FileOutputStream fos = new FileOutputStream("C:\\myFile.exe");
26. fos.write(b);
27. fos.close();

运行我的应用程序后,我得到一个NullPointerException ,这是它:

java.lang.NullPointerException
    at MainClass.<init>(MainClass.java:23)
    at MainClass.main(MainClass.java:66)

(第66行是用new MainClass();调用构造函数的地方new MainClass();

我知道NPE可能被抛出,因为无法找到myFile.exe ,但我无法弄清楚为什么......当我打开JAR时,我可以看到带有exefiles夹。 也许我正在以错误的方式访问该文件? 我在StackOverflow上尝试了许多其他方法,但没有任何作用......

提前致谢,对不起我的英语。


i'm working on a little project and i need to move a file from the application JAR to another location outside the JAR.

This is how my JAR file looks like:

MyApp.JAR
   |- META-INF
       |- MANIFEST.MF
   |- files
       |- myFile.exe
   |- MainClass.class

What i'd like to do is move myFile.exe to C:\\ (for example). So in the MainClass i tried with:

23. Path path = Paths.get(getClass().getClassLoader().getResource("/files/myFile.exe").toURI());
24. byte[] bytes = Files.readAllBytes(path);

25. FileOutputStream fos = new FileOutputStream("C:\\myFile.exe");
26. fos.write(b);
27. fos.close();

After running my application i get a NullPointerException, here's it:

java.lang.NullPointerException
    at MainClass.<init>(MainClass.java:23)
    at MainClass.main(MainClass.java:66)

(Line 66 is where i call the constructor with new MainClass();)

I know the NPE is probably thrown because myFile.exe cannot be found, but i can't figure out why... When i open the JAR i can see the files folder with the exe inside. Maybe i'm accessing the file in a wrong way? I tried many other ways that i found here on StackOverflow but nothing works...

Thanks in advance and sorry for my english.


原文:https://stackoverflow.com/questions/34373172
更新时间:2023-11-08 21:11

最满意答案

值得阅读R FAQ 7.31并考虑浮点表示的准确性。

我无法在当前的版本(1.9.2)中重现这一点。 运用

R version 3.0.3 (2014-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)

我猜测行为的变化将与此新闻项目有关。

o数值数据仍然像以前一样在公差范围内连接和分组,但不是公差为sqrt(.Machine $ double.eps)== 1.490116e-08(与base :: all.equal的默认值相同)现在已将有效数字舍入为最后2个字节,apx 11 sf这更适用于大型(1.23e20)和小型(1.23e-20)数字,并且通过简单的位旋转更快。 一些函数提供了一个“容差”参数但是没有被传递,所以已被删除。 我们的目标是在将来的版本中添加一个全局选项(例如,2个,1个或0个字节的舍入)。


马特更新

是的,这是v1.9.2中的故意更改,data.table现在将0.00000000000000000000000000010区分开来(正如user3340145正确认为的那样),因为上面从NEWS突出显示了改进的舍入方法。

我还添加了Rick对测试套件的回答的for循环测试。

顺便说一句,#5369现在已在v1.9.3中实现(尽管这个问题都不需要这些):

o bit64 :: integer64现在可用于分组和连接,#5369。 感谢James Sams突出UPC。

o新函数setNumericRounding()可用于在加入或分组“numeric”类型的列#5369时减少1个字节或0个字节的舍入。 请参阅v1.9.2中的setNumericRounding和NEWS项中的示例。 getNumericRounding()返回当前设置。

请注意,现在(从v1.9.2开始)舍入有关有效数的准确性; 即有效数字的数量。 0.0000000000000000000000000001 == 1.0e-28精确到1平方英尺,因此新的舍入方法不会将其与0.0组合在一起。

简而言之,问题的答案是:从v1.8.10升级到v1.9.2或更高版本。


It is worth reading R FAQ 7.31 and thinking about the accuracy of floating point represenations.

I can't reproduce this in the current cran version (1.9.2). using

R version 3.0.3 (2014-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)

My guess that the change in behaivour will be related to this news item.

o Numeric data is still joined and grouped within tolerance as before but instead of tolerance being sqrt(.Machine$double.eps) == 1.490116e-08 (the same as base::all.equal's default) the significand is now rounded to the last 2 bytes, apx 11 s.f. This is more appropriate for large (1.23e20) and small (1.23e-20) numerics and is faster via a simple bit twiddle. A few functions provided a 'tolerance' argument but this wasn't being passed through so has been removed. We aim to add a global option (e.g. 2, 1 or 0 byte rounding) in a future release.


Update from Matt

Yes this was a deliberate change in v1.9.2 and data.table now distinguishes 0.0000000000000000000000000001 from 0 (as user3340145 rightly thought it should) due to the improved rounding method highlighted above from NEWS.

I've also added the for loop test from Rick's answer to the test suite.

Btw, #5369 is now implemented in v1.9.3 (although neither of these are needed for this question) :

o bit64::integer64 now works in grouping and joins, #5369. Thanks to James Sams for highlighting UPCs.

o New function setNumericRounding() may be used to reduce to 1 byte or 0 byte rounding when joining to or grouping columns of type 'numeric', #5369. See example in ?setNumericRounding and NEWS item from v1.9.2. getNumericRounding() returns the current setting.

Notice that rounding is now (as from v1.9.2) about the accuracy of the significand; i.e. the number of significant figures. 0.0000000000000000000000000001 == 1.0e-28 is accurate to just 1 s.f., so the new rounding method doesn't group this together with 0.0.

In short, the answer to the question is : upgrade from v1.8.10 to v1.9.2 or greater.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。