简体   繁体   English

哈希表NullPointerException

[英]Hashtable NullPointerException

I have an assignment to design a simple spam filter using a hash table to store a set of "bad words." 我分配了一个工作,使用哈希表设计一个简单的垃圾邮件过滤器,以存储一组“坏词”。 We discussed in class what a hash table is and how it's used (ie. how elements are inserted, linear probing, quadratic probing, chained probing, etc.), but we never really discussed how to use the Java API Hashtable, which is required for this assignment. 我们在课堂上讨论了什么是哈希表以及如何使用哈希表(即元素如何插入,线性探测,二次探测,链式探测等),但是我们从未真正讨论过如何使用Java API哈希表。为此作业。 anyway, I've tried my best to implement it, however, I'm getting an exception that I cannot seem to trace with my debugger. 无论如何,我尽了最大的努力来实现它,但是,我遇到了一个异常,我似乎无法使用调试器进行跟踪。

I've given up with inserting code here, I always have to spend time formatting it properly and it still doesn't look good. 我已经放弃在这里插入代码了,我总是不得不花时间对其进行正确的格式化,但看起来还是不太好。 So I'm putting it on Pastebin instead. 因此,我将其放在Pastebin上。 It should make your lives easier too since it does syntax highlighting and line counting as well. 由于它还可以突出显示语法和行数,因此也应该使您的生活更轻松。

SpamFilter class SpamFilter类
SpamFilterDriver class SpamFilterDriver类

I get NullPointerExceptions at the following lines in the SpamFilterDriver class: 我在SpamFilterDriver类的以下几行中获得NullPointerExceptions:
line 78 78行
line 96 96行
line 115 115行

Any help would be appreciated. 任何帮助,将不胜感激。 I'm sure it's probably something silly, but I'm just not seeing it at the moment. 我敢肯定这可能很愚蠢,但是我暂时还没有看到。



Also, please note that the code is not finished yet in the least. 另外,请注意,代码至少还没有完成。 The fact that the SpamFilter implements Serializeable will be used later on. SpamFilter实现Serializeable的事实将在以后使用。 Also, there are some empty methods, again, they will be implemented later, I just need to solve this problem first. 另外,还有一些空方法,它们将在以后实现,我只需要先解决这个问题即可。

you have to initialize your SpamFilter filter; 您必须初始化SpamFilter filter; in your SpamFilter Class. 在您的SpamFilter类中。 before calling any of its methods. 在调用其任何方法之前。

on line 78, you are calling a method isBadWord() . 在第78行,您正在调用方法isBadWord()

try initializing filter like below before calling any of its method. 在调用任何方法之前,请尝试如下初始化filter if you dont initialize your filter the default value is null and boom when you call a method with null BOOM BOOM NPE is thrown 如果不初始化过滤器,则默认值为null,并且在调用具有null的方法时会增加BOOM BOOM NPE被引发

    private static SpamFilter filter = new SpamFilter();// 

In each one of those lines, observe that you are invoking a method of filter . 在每一行中,观察到您正在调用filter方法。 Java is throwing getting a NullPointerException because filter 's value is null . Java将抛出NullPointerException因为filter的值为null

Why is its value null ? 为什么其值为null Because you never assigned it a value; 因为您从未为其分配值; you merely declared it. 你只是宣布而已。 You can set its value right as you declare it or, since it's a static field, you can use a static constructor to assign it a value. 您可以在声明它时立即设置其值,或者由于它是静态字段,所以可以使用静态构造函数为其分配值。

Your filter is not be initialized hence failing wherever its being used. 您的filter未初始化,因此无论在何处使用都会失败。 Its only declared in the top as private static SpamFilter filter; 它仅在顶部声明为private static SpamFilter filter; , which makes filter as null. ,使filter为null。

To fix the problem, initialize the filter either at declaration as 要解决此问题,请在声明为时将filter初始化为

      private static SpamFilter filter = new SpamFilter(); 

or in main() method before while loop as 或在while main()循环之前的main()方法中

      filter = new SpamFilter();

Looks like you never initialize the filter property, so you get a NullPointerException whenever you try and use it. 看起来您从未初始化过filter属性,因此无论何时尝试使用它,都会得到NullPointerException。

Try replacing line 6 with: 尝试将第6行替换为:

private static SpamFilter filter = new SpamFilter();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM