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时,我可以看到带有exe
的files
夹。 也许我正在以错误的方式访问该文件? 我在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
toC:\\
(for example). So in theMainClass
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 becausemyFile.exe
cannot be found, but i can't figure out why... When i open the JAR i can see thefiles
folder with theexe
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
最满意答案
值得阅读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.0000000000000000000000000001
与0
区分开来(正如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 distinguishes0.0000000000000000000000000001
from0
(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 with0.0
.In short, the answer to the question is : upgrade from v1.8.10 to v1.9.2 or greater.
相关问答
更多-
使用with data.table(Using by with data.table)[2021-12-08]
我想我会关闭这一个。 如果遇到同样的问题,请运行str(x.small)并查看向量是否以“Named”开头,检查问题列是否为命名向量。 出于某种原因,在对命名向量进行操作时使用by参数会导致问题。 这似乎是早期版本的data.table中的一个小错误,它在更高版本中进行了修补。 要解决此问题,请升级data.table或仅使用unname()作为@Frank建议: x.small[,rank(unname(prediction)), by=group] I guess I'll just close th ... -
感谢@DaisyLee指出这个问题。 看来你设法挖掘出一个相当模糊和非常古老的测试,这个测试在你的代码上错误地发生了错误。 我提出了一个关于这个问题的问题并提出了一个解决方案 您的代码现在应该在data.table版本1.9.8+上运行。 一旦你更新了,你应该能够运行你最初尝试过的代码片段而不会错误,而不需要使用as.data.table或setDT 。 如果你有兴趣, 这里是修复的代码。 Thanks @DaisyLee for pointing out this issue. It seems you ...
-
开关 import path = module('path'); 至 import path = require('path'); Switch import path = module('path'); to import path = require('path');
-
在data.table中创建组(Create groups in data.table)[2022-05-28]
out <- dt[, .(V2 = myGroup), by = V1] # V1 V2 # 1: A d # 2: A e # 3: A f # 4: B d # 5: B e # 6: B f # 7: C d # 8: C e # 9: C f all.equal(out, result) # [1] TRUE 编辑 Per dt[, CJ(V1, myGroup)]的评论,你可以等同地和更惯用地做: dt[, CJ(V1, myGroup)] ( C ... -
其他方式: 编辑。 table(.)是罪魁祸首。 将其更改为完成data.table语法。 dt.out<- dt[, .N, by=list(user,country)][, list(country[which.max(N)], max(N)/sum(N)), by=user] setnames(dt.out, c("V1", "V2"), c("country", "support")) # user country support # 1: 3 ...
-
print.data.table有一个参数row.names ,默认设置为TRUE。 只需使用 > print(DT, row.names=FALSE) V1 V2 1 3 2 2 3 1 print.data.table has an argument row.names, which is set to TRUE by default. Just use > print(DT, row.names=FALSE) V1 V2 1 3 2 2 3 1
-
以下是我将如何处理这个问题: f <- function(columns) { Get <- if (!is.numeric(columns)) match(columns, names(DT)) else columns columns <- names(DT)[Get] DT[, sum(N), by = columns] } 第一行( Get.. )将“列”保留为数字,如果它已经是数字,或者如果它们不是数字,则将它从字符转换为数字。 用一些示例数据进行测试: set.seed(1) DT ...
-
将data.table转换为data.frame会在v1.8.10中出错(conversion of data.table to data.frame gives error in v1.8.10)[2022-09-20]
更新到1.9.2修复了该问题。 谢谢,阿伦! updating to 1.9.2 fixed the issue. Thanks, Arun! -
值得阅读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的默认值相同)现在已将有效数字舍入为最后 ...
-
正如Andrie和mnel,我也无法复制。 要测试data.table类型的安装: test.data.table() 在我的上网本上,这将返回: [.. snip ..] All 717 tests in test.data.table() completed ok in 50.040sec 如果它准确地返回717那么你就是好的。 新闻 1.8.2的NOTES部分是您发现1.8.2有717个测试的地方。 是否有可能升级到1.8.2但没有重新加载软件包,或者当你在一个软件升级但没有重新加载其他R会话时运 ...