[英]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.