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 5 of 5

Thread: i get an error when i run it

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i get an error when i run it

    import java.io.File;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Scanner;
     
     
     
    public class WordCounter 
    {
    	// 2 fields for file name and hashmap
    	private ArrayList<String> fn;
    	private HashMap <String, Integer> hm;
    	//private Scanner x;
     
    	// constructor
    	public WordCounter(String frame)
    	{
    		countwords(frame);
     
    	}
    	public ArrayList<String> getfn() {
    		return fn;
    	}
    	public HashMap<String, Integer> gethm() {
    		return hm;
    	}
    	// methods
    	private void countwords (String fname)
    	{
    		try{
    			Scanner x = new Scanner (new File(fname));
    			while (x.hasNextLine()){
    				String[]words = x.nextLine().split("[^a]+");
    				for(String s: words)
    					System.out.println(s);
    		System.out.println(Collections.frequency(fn, hm));
    		}}
    		catch (Exception e){
    			System.err.println(e);
    		}
    	}
    	public void print() {
    		System.out.println("Word:\t\tFrequency:");
    		System.out.println("====================================");
    		for(int i=0; i< fn.size(); i++){
    			System.out.println();
    		}
    	}
     
     
    	public static void main(String[] args) throws Exception  
    	{
    		String fname = "test-file.txt";
    		if (args.length > 0)
    			fname = args[0];
     
    		WordCounter wc = new WordCounter(fname);
    		wc.print();
    	}
     
     
    }

    Output

    Word: Frequency:
    ====================================
    java.lang.NullPointerException
    Exception in thread "main" java.lang.NullPointerException
    at WordCounter.print(WordCounter.java:46)
    at WordCounter.main(WordCounter.java:59)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: i get an error when i run it

    java.lang.NullPointerException
    Exception in thread "main" java.lang.NullPointerException
    at WordCounter.print(WordCounter.java:46)
    There is a variable with a null value on line 46. Look at line 46 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 46 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: i get an error when i run it

    so the line is :
    for(int i=0; i< fn.size(); i++){

    however i beilieve i need that for the program.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: i get an error when i run it

    Quote Originally Posted by shad0wblade890 View Post
    so the line is :
    for(int i=0; i< fn.size(); i++){

    however i beilieve i need that for the program.
    Follow the advice that Norm gave you. One of the variables on that line is null. When do you assign a value to fn?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: i get an error when i run it

    okey i figure out why is null, because fn doesnt have a value set. so i need to read the ttext file that i imported in to my project (just reads 1 letter like ...... A ) and prints how many times its prints out.

Similar Threads

  1. Run time error
    By gautammuktsar@gmail.com in forum Java Theory & Questions
    Replies: 2
    Last Post: February 10th, 2014, 04:31 PM
  2. Run time error
    By kmaka in forum Java Theory & Questions
    Replies: 1
    Last Post: February 8th, 2013, 07:19 PM
  3. Run Time Error!!!
    By RSelvan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 20th, 2012, 07:40 AM
  4. Run Time Error.
    By seymour001 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 10th, 2011, 12:10 PM
  5. [SOLVED] Run time error
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2011, 11:05 AM