Hi everyone I'm having an issue with my code that needs an urgent fix. I'll begin by slapping in my code.
private void readTweetFile(String filename){ try{ Scanner input = new Scanner(new FileInputStream(filename)); ArrayList<String> tempList; ArrayList<String> tempListTwo; while(input.hasNext()){ String temp = input.nextLine(); Scanner sc = new Scanner(temp); while(sc.hasNext()){ String word = sc.next(); if(wordFrequency.containsKey(word)){ wordFrequency.put(word, wordFrequency.get(word)+1); tempListTwo = wordIndex.get(word); tempListTwo.add(temp); wordIndex.put(word, tempListTwo); } else if(!wordFrequency.containsKey(word)){ wordFrequency.put(word, 1); tempList = new ArrayList<String>(); tempList.add(temp); wordIndex.put(word, tempList); } } } } catch(IOException e){System.out.println("Fail: " + e);} }
The idea of this code is to read a file of tweets and map how many times each word was said by setting the key to the map the word and the value to that key as the frequency. The second map is a map of strings to arraylists where for every word in the file, it has a list of tweets that the word was in, (Obviously it is an arraylist of strings.)
The null pointer exception is pointing at these lines...
if(wordFrequency.containsKey(word)){
and...
readTweetFile(File);
The latter line of code being in a separate method that calls on this method so I believe it is of no consequence to the exception. Sorry if the answer is obvious I'm still a beginner programmer.