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:
Code java:
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
}
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.
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.
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.