Reputation: 1428
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)
Upvotes: 0
Views: 5105
Reputation: 13129
There are sometimes that we have
val uri = Uri.parse(url)
var fileName = URLDecoder.decode(uri.lastPathSegment, "UTF-8")
Reemember that uri.lastPathSegment
is Nullable and could return null
/**
* Gets the decoded last segment in the path.
*
* @return the decoded last segment or null if the path is empty
*/
@Nullable
public abstract String getLastPathSegment();
You should always check if the path is not null before continuing to decode
val uri = Uri.parse(url)
if(uri.lastPathSegment != null)
var fileName = URLDecoder.decode(uri.lastPathSegment, "UTF-8")
Upvotes: 0
Reputation: 2732
Mostly your file "contextPasquier99.txt" exist in some path but you might have not specified it with complete correct path.
if so your URL url = MainTestCharm_saveToMemory.class.getResource(filename);
returns null for invalid file path and this propogates the null pointer exception to below line
return java.net.URLDecoder.decode(url.getPath(),"UTF-8");
as url is calling its method but seems url is null.
and this exception propogates to caller i e
database.loadFile(fileToPath("contextPasquier99.txt"));
Upvotes: 1
Reputation: 899
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
.
Upvotes: 1
Reputation: 309
The cause of NullPointerExcception is located in line 48.
URL url = MainTestCharm_saveToMemory.class.getResource(filename);
When you tried to obtain the URL of resource you got the null
as stated in documentation of method getResource
. Thus, you must check the existence of resource before run the code. It will help you to solve this problem. BTW do not forgot that the resource is not a regular file located anywhere on the filesystem. It's a file located in the the search path used to load classes.
Upvotes: 1
Reputation: 4824
Without line numbers, this is a bit tricky, but I'm going to go out on a limb...
What does MainTestCharm_saveToMemory.class.getResource(filename);
return?
I'm guessing null
, causing a NPE on the next line when you do url.getPath()
Upvotes: 1