//This Is the Class
private String words;
private File textFile;
private Scanner inputFile;
StringBuilder stringBuilder = new StringBuilder();
public Scramble(String j) throws FileNotFoundException
{
File textFile = new File(j);
Scanner inputFile = new Scanner(textFile);
}
public String getRealWord() throws IOException
{
//Make the loop while !null
//if null close the document
while (inputFile.hasNext())
{
stringBuilder.append(inputFile);
}
inputFile.close();
return stringBuilder.toString();
}
This is the call to the class in the main method String word = theScramble.getRealWord(); What portion should I change to avoid the null pointer exception
You're re-declaring your variables in the local scope. Within your Scramble
constructor:
File textFile = new File(j);
...is declaring a new variable called textFile
, which hides the instance member also called textFile
.
You'll want to change:
public Scramble(String j) throws FileNotFoundException
{
File textFile = new File(j);
Scanner inputFile = new Scanner(textFile);
}
To:
public Scramble(String j) throws FileNotFoundException
{
textFile = new File(j);
inputFile = new Scanner(textFile);
}
This way, you're referring to the instance variables, not the local variables.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.