Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Map containsKey() giving Null Pointer Exception

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Wellington, NZ.
    Posts
    11
    My Mood
    Cold
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Map containsKey() giving Null Pointer Exception

    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.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Map containsKey() giving Null Pointer Exception

    Where and how did you declare wordFrequency?

  3. #3
    Junior Member
    Join Date
    May 2013
    Location
    Wellington, NZ.
    Posts
    11
    My Mood
    Cold
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Map containsKey() giving Null Pointer Exception

    It is a field in the class, funny you should ask I was just looking at it...

    private Map<String, Integer> wordFrequency;

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Map containsKey() giving Null Pointer Exception

    There you go. It's null.

  5. The Following User Says Thank You to PhHein For This Useful Post:

    Blinktwink (July 31st, 2013)

  6. #5
    Junior Member
    Join Date
    May 2013
    Location
    Wellington, NZ.
    Posts
    11
    My Mood
    Cold
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Map containsKey() giving Null Pointer Exception

    Oh wow I just realised that -.- I passed over it before because,

    private Map<String, Integer> wordFrequency = new Map<String, Integer>();

    wasn't working so I removed it to keep the compiler happy. I only just realised I needed to use HashMap.

    Must get some sleep before I embarrass myself on the forums further thanks a lot!

  7. #6
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Map containsKey() giving Null Pointer Exception

    Hello.
    You may be making one more mistake in your code.
    In the beginning when you are using scanner for file, you are checking for next word using scanner.hasNext() but reading the entire line. You may have to use scanner.next(). It will not be a problem is you are having one word in every line.
    Does you code still contain errors?

    Syed.

Similar Threads

  1. Null Pointer Exception
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 15th, 2012, 10:33 PM
  2. [B]Null Pointer Exception[/B]-- Please Help!!!
    By entwicklerin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 24th, 2012, 11:24 AM
  3. Help with null pointer exception
    By Dr.HughMan in forum Exceptions
    Replies: 35
    Last Post: November 30th, 2011, 09:00 PM
  4. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 06:46 PM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM

Tags for this Thread