When I run the program below (in NETBEANS 6.1.0 ), I get a 'java.lang.NullPointerException' in line 21 and 49. I'm a java novice please help to fix the error.
line 21. database.loadFile(fileToPath("contextPasquier99.txt"));
line 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");
}
}
I get the following:
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)
Your .getResource(filename) is probably returning a null
. You should test that url
has a value before using it; something like:
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
}
EDIT:
For getResource(filename)
to work outside the package, the filename should start with "/"; for example:
database.loadFile(fileToPath("/contextPasquier99.txt"));
If it's called like:
database.loadFile(fileToPath("contextPasquier99.txt"));
it will only look inside the package ca.pfv.spmf.test
.