![](/img/trans.png)
[英]How do I fix my java.lang.NullPointerException (android)?
[英]How can I fix the java.lang.NullPointerException in the given code?
当我在NETBEANS 6.1.0中运行以下程序时,在第21和49行中得到了“ java.lang.NullPointerException”。我是Java新手,请帮助解决该错误。
第21行。database.loadFile(fileToPath(“ contextPasquier99.txt”));;
第49行。return java.net.URLDecoder.decode(url.getPath(),“ UTF-8”);
package ca.pfv.spmf.test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import ca.pfv.spmf.algorithms.frequentpatterns.eclat_and_charm.AlgoCharm;
import ca.pfv.spmf.input.transaction_database_list_integers.TransactionDatabase;
import ca.pfv.spmf.patterns.itemset_set_integers_with_tids.Itemsets;
/**
* Example of how to use the CHARM algorithm from the source code.
* @author Philippe Fournier-Viger (Copyright 2009)
*/
public class MainTestCharm_saveToMemory {
public static void main(String [] arg) throws IOException{
// Loading the transaction database
TransactionDatabase database = new TransactionDatabase();
try {
database.loadFile(fileToPath("contextPasquier99.txt"));
} catch (IOException e) {
e.printStackTrace();
}
database.printDatabase();
// Applying the Charm algorithm
AlgoCharm algo = new AlgoCharm();
Itemsets closedItemsets = algo.runAlgorithm(null, database, 100000, 0.4, true);
// NOTE 0: We use "null" as output file path, because in this
// example, we want to save the result to memory instead of
// saving to a file
// NOTE 1: if you use "true" in the line above, CHARM will use
// a triangular matrix for counting support of itemsets of size 2.
// For some datasets it should make the algorithm faster.
// NOTE 2: 1000000 is the hashtable size used by CHARM for
// storing itemsets. Most users don't use this parameter.
// print the statistics
algo.printStats();
// print the frequent itemsets found
closedItemsets.printItemsets(database.size());
}
public static String fileToPath(String filename) throws UnsupportedEncodingException{
URL url = MainTestCharm_saveToMemory.class.getResource(filename);
return java.net.URLDecoder.decode(url.getPath(),"UTF-8");
}
}
我得到以下内容:
Exception in thread "main" java.lang.NullPointerException
at ca.pfv.spmf.test.MainTestCharm_saveToMemory.fileToPath(MainTestCharm_saveToMemory.java:49)
at ca.pfv.spmf.test.MainTestCharm_saveToMemory.main(MainTestCharm_saveToMemory.java:21)
没有行号,这有点棘手,但是我要走了...
MainTestCharm_saveToMemory.class.getResource(filename);
是什么? 返回? 我猜是null
,当您执行url.getPath()
时,在下一行导致NPE
您的.getResource(filename)可能返回null
。 您应该在使用之前测试url
是否具有值。 就像是:
public static String fileToPath(String filename) throws UnsupportedEncodingException{
URL url = MainTestCharm_saveToMemory.class.getResource(filename);
if (url != null) {
return java.net.URLDecoder.decode(url.getPath(),"UTF-8");
}
System.out.println("file: " + filename + "not found");
System.exit(-1); // or return empty string or null
}
编辑:为使getResource(filename)
在包外工作,文件名应以“ /”开头; 例如:
database.loadFile(fileToPath("/contextPasquier99.txt"));
如果这样称呼:
database.loadFile(fileToPath("contextPasquier99.txt"));
它只会在package ca.pfv.spmf.test
内部package ca.pfv.spmf.test
。
NullPointerExcception的原因位于第48行。
URL url = MainTestCharm_saveToMemory.class.getResource(filename);
当您尝试获取资源的URL时,如方法getResource
文档中所述,您得到了null
。 因此,您必须在运行代码之前检查资源是否存在。 这将帮助您解决此问题。 顺便说一句,不要忘记该资源不是文件系统上任何位置的常规文件。 这是位于用于加载类的搜索路径中的文件。
通常,您的文件“ contextPasquier99.txt”存在于某些路径中,但您可能未使用完整正确的路径指定它。
如果是这样,则您的URL url = MainTestCharm_saveToMemory.class.getResource(filename);
对于无效的文件路径返回null,这会将null指针异常传播到下面的行
return java.net.URLDecoder.decode(url.getPath(),"UTF-8");
因为url正在调用其方法,但url为null。
并且此异常传播给呼叫者,即
database.loadFile(fileToPath("contextPasquier99.txt"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.