0

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)
5
  • 1
    url object is probably null. Commented Apr 25, 2014 at 20:14
  • which lines are 21 and 49? Commented Apr 25, 2014 at 20:14
  • 2
    Just to clarify, you are only getting one Null pointer exception and that is at line 49. Line 21 is simply part of the call stack, meaning it is where fileToPath was called. The bottom line of the stack trace (the entire error output you got) is the actual place it occured. All the the lines above it just show you how the code got there. Commented Apr 25, 2014 at 20:15
  • MainTestCharm_saveToMemory.class.getResource(filename); is going to look for the file on the CLASSPATH. You would use this if you are running your application from a jar file that also contains the contextPasquier99.txt file, or have included a directory on the CLASSPATH that contains that file. Is this what you want? If you're trying to open a file on the file system, you should use java.io.File. Commented Apr 25, 2014 at 20:20
  • @Frakcool I edited the question to mention the lines! Commented Apr 25, 2014 at 20:27

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

System.exit(-1)? That's a new one on me. Usually that's 0 or a positive value (or have I been playing with Unix too much?)
It doesn't matter the value. If it's different than 0, it's an error.
fair enough. Guess the Unix has corrupted my brain :)
@tase92 Where should I keep "contextPasquier99.txt" so that getResource is able to find it?
You have to be very careful using relative paths. class.getResource and ClassLoader.getResource have different relative paths. Relative paths are a nightmare.
|
1

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()

5 Comments

@ShaneCoder why are you asking me?
How can I fix the 'null' return of MainTestCharm_saveToMemory.class.getResource(filename);
@nimesh.iitd Make sure there's a resource in your JAR that points at "contextPasquier99.txt"
I have kept the contextPasquier99.txt in the same folder as this program i'm trying to run. Should I keep it somewhere else?
Maybe try "/contextPasquier99"? I'm literally just guessing, as I do not know the structure of your JAR.
1

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.

Comments

1

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"));

Comments

0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.