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

Thread: NullPointerException ... and does it work?

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointerException ... and does it work?

    I'm trying to write a code that will read in a keyword and files from the command line and search for that word in the specified files. I am not sure if it works, because I'm getting this exception and I'm not sure how to fix it. It's due within the next two hours, and I really need help just fixing it up and making sure it complies (and hopefully works!) I'm getting the error at line25 in the test and line 20 in the SearchFile (the keyword stuff)... I SO appreciate the help anyone can give me!

    Here is my SearchFile class:

    import java.util.ArrayList;
     
    public class SearchFile {
     
    	private ArrayList <String> fileContent;
    	public String specLine, lineNumber, fileName;
    	int wordFoundAt;
     
    	public SearchFile(){
    		fileContent = new ArrayList <String> ();
    	}
     
    	public void addLine(String newLine){
    		fileContent.add(newLine);
    	}
     
     
    	public void findWord(String keyWord){
    		for(int i = 0; i < fileContent.size(); i++){
    		wordFoundAt = specLine.indexOf(keyWord);	
    		}
    	}
     
    	public String getLine(){
    		String lineContent = fileContent.get(wordFoundAt);
    		return lineContent;
    	}
     
    	public String toString(){
    		  String wordFound;
    		  getLine();
    		  if (wordFoundAt == - 1){
    		      System.out.println("Sorry - that word could not be found");
    		    }else{
    		    	wordFound = fileName + ":" + wordFoundAt + "\n";      
    		    }
    	      wordFound = fileName + ":" + lineNumber + "\n";
    	      return wordFound;
    	}
    }
     
     
    And here is my test class:
    //import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.*;
     
    public class SearchFilesTest {
     
     
    			  public static void main(String[] args) 
    			  {
    				boolean again=true;
    				while(again){
    				try 
    				{	
    				File inFile = new File(args[1]); //the file to be used is first arg
    				Scanner input = new Scanner(inFile);
    				for(int noFiles = 0; noFiles < args.length; noFiles++)
    				{
    				SearchFile newSearch = new SearchFile();
    				String keyWord = (args[0]);	
    				while(input.hasNext())
    					{
    						String newLine = input.nextLine();
    						newSearch.addLine(newLine);
    					}//end of while 
    					newSearch.findWord(keyWord);
    				System.out.println(newSearch.toString());
    				again=false;
    				}//for
    				}//end try 
    				catch(IOException e){
    					System.out.println("Please provide a correct file name");
    					Scanner scan = new Scanner(System.in);
    					args[0]=scan.next();
    				}
    				catch(ArrayIndexOutOfBoundsException e) {
    			        System.out.println("Please provde a file in the command line");
    			        again=false;
    				}
    		}//end while again = true loop
    		} //end main method
    	}
    Last edited by KevinWorkman; April 18th, 2011 at 01:10 PM. Reason: added highlight tags


  2. #2
    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: NullPointerException ... and does it work?

    When do you initialize specLine or fileName? When do you use lineNumber?

    Code you post should be in SSCCE form- I added the highlight tags for you this time. Mentioning your deadline will only make you seem impatient, which will decrease your chances of getting help.
    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!

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException ... and does it work?

    Thanks so much! I'm new to this, so I'm apologize for not having it in proper format.
    I declare both at the top, but I don't think I initialize either... and I think that linNumber is the same as wordFoundAt, so maybe I'll replace that.

  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: NullPointerException ... and does it work?

    If you don't initialize a variable, that variable will be null (in the case of global Object variables). You can't dereference (use .anything) a null, or you'll get a NullPointerException.
    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!

Similar Threads

  1. NullPointerException error
    By blazerix in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 24th, 2011, 09:59 PM
  2. Problem with NullPointerException?
    By scooty199 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: November 6th, 2010, 05:13 PM
  3. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM