简体   繁体   English

如何在给定代码中修复java.lang.NullPointerException?

[英]How can I fix the java.lang.NullPointerException in the given code?

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. 当我在NETBEANS 6.1.0中运行以下程序时,在第21和49行中得到了“ java.lang.NullPointerException”。我是Java新手,请帮助解决该错误。

line 21. database.loadFile(fileToPath("contextPasquier99.txt")); 第21行。database.loadFile(fileToPath(“ contextPasquier99.txt”));;

line 49. return java.net.URLDecoder.decode(url.getPath(),"UTF-8"); 第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)

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); MainTestCharm_saveToMemory.class.getResource(filename);是什么? return? 返回? I'm guessing null , causing a NPE on the next line when you do url.getPath() 我猜是null ,当您执行url.getPath()时,在下一行导致NPE

Your .getResource(filename) is probably returning a null . 您的.getResource(filename)可能返回null You should test that url has a value before using it; 您应该在使用之前测试url是否具有值。 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 "/"; 编辑:为使getResource(filename)在包外工作,文件名应以“ /”开头; 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 . 它只会在package ca.pfv.spmf.test内部package ca.pfv.spmf.test

The cause of NullPointerExcception is located in line 48. NullPointerExcception的原因位于第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 . 当您尝试获取资源的URL时,如方法getResource 文档中所述,您得到了null 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. 这是位于用于加载类的搜索路径中的文件。

Mostly your file "contextPasquier99.txt" exist in some path but you might have not specified it with complete correct path. 通常,您的文件“ contextPasquier99.txt”存在于某些路径中,但您可能未使用完整正确的路径指定它。

if so your URL url = MainTestCharm_saveToMemory.class.getResource(filename); 如果是这样,则您的URL url = MainTestCharm_saveToMemory.class.getResource(filename);

returns null for invalid file path and this propogates the null pointer exception to below line 对于无效的文件路径返回null,这会将null指针异常传播到下面的行

 return java.net.URLDecoder.decode(url.getPath(),"UTF-8");

as url is calling its method but seems url is null. 因为url正在调用其方法,但url为null。

and this exception propogates to caller ie 并且此异常传播给呼叫者,即

 database.loadFile(fileToPath("contextPasquier99.txt"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复java.lang.NullPointerException(android)? - How do I fix my java.lang.NullPointerException (android)? 如何修复BootReciever java.lang.NullPointerException? - How to fix BootReciever java.lang.NullPointerException? 如何修复java.lang.NullPointerException - How to fix java.lang.NullPointerException java.lang.NullPointerException如何修复 - java.lang.NullPointerException how to fix 如何修复Jaspersoft studio插件中由TraceGovernor引起的java.lang.NullPointerException? - How can I fix java.lang.NullPointerException caused by TraceGovernor in Jaspersoft studio plugin? “ java.lang.NullPointerException”错误(错误详细信息,在本文中)我该如何解决? - “java.lang.NullPointerException” Error(Error details, there in the article) How can i fix it? 如何在此代码中处理java.lang.NullPointerException - How to handle java.lang.NullPointerException in this code 我如何避免java.lang.NullPointerException错误 - How can i avoid java.lang.NullPointerException error 我在Java中遇到了指针问题。 如何修复java.lang.NullPointerException? - I am having a problem with pointers in java. How do I fix a java.lang.NullPointerException? 如何修复NetBeans生成的代码中的'线程异常'AWT-EventQueue-0“java.lang.NullPointerException”? - How to fix 'Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException' on code generated by NetBeans?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM