Java – why do I throw a NullPointerException
So it's a winter vacation for college, and I'm trying to be sharp in coding, so I just write the code of programs and algorithms. We only discuss but never code in class Anyway, what I'm studying today is a program. You give the computer a disturbing word, which outputs all the words that can be generated from these letters (from the English wordlist file we give)
Anyway, this is my code so far:
import java.io.*; import java.util.*; public class ProdFinder { private HashMap<Character,Integer> prodFinder = new HashMap<Character,Integer>(); private HashMap<Integer,LinkedList<String>> findWord = new HashMap<Integer,LinkedList<String>>(); private static final char[] letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; private static final int[] primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101}; public ProdFinder() throws FileNotFoundException { for (int i = 0; i < letters.length; i++) { prodFinder.put(letters[i],primes[i]); } Scanner sc = new Scanner(new File("EnglishWordList.txt")); while (sc.hasNextLine()) { String str = sc.nextLine(); int strProd = findProduct(str); if (findWord.containsKey(strProd)) { LinkedList<String> wordList = findWord.get(strProd); wordList.add(str); findWord.put(strProd,wordList); } else { LinkedList<String> wordList = new LinkedList<String>(); wordList.add(str); findWord.put(strProd,wordList); } } sc.close(); } public int findProduct(String x) { int product = 1; char[] str = x.tocharArray(); for (Character val: str) { product = product*prodFinder.get(val); } return product; } public void descramble(String x) { int prod = findProduct(x); if (findWord.containsKey(prod)) { System.out.println("The words that can be formed from the letters in " + x + " are: " + findWord.get(prod)); } else { System.out.println("No words can be formed from the letters in " + x + "."); } } }
Now, the error originated when I started putting all the major products of the number into the line of my HashMap (trying to map each major product to the LinkedList of the word whose letter multiplied by the number) Somehow, this is an exception thrown. For example, when I annotate that part of the code and run the findproduct method, it can work normally. I output it as a product that outputs lettesr in gold form
Any ideas about where the exception came from?
Edit: sorry, the stack trace is as follows:
Exception in thread "main" java.lang.NullPointerException at ProdFinder.findProduct(ProdFinder.java:44) at ProdFinder.<init>(ProdFinder.java:22) at Descramble.main(Descramble.java:7)
As far as I know, when I try to call findprod on STR, an error occurs:
int strProd = findProduct(str);
Solution
The problem is prodfinder Java: 44, because the stack trace indicates which is
product = product*prodFinder.get(val);
The null pointer exception is that prodfinder does not have the product / value of Val, so it returns null, and the program then tries to multiply the product
(any * null) causes NullPointerException
The reason why the prodfinder map has no value may be that when all words added to the map are lowercase characters, the word has some uppercase letters, special characters or numbers
Java debugger is a good resource for this kind of problem Using ides such as eclipse or NetBeans, you can quickly find the culprit use case by placing a breakpoint on line 44