Can anyone help me please, I cant figure out what I am doing wrong or missing. I need to enter a sentence and write it to a file over and over again until the word done is typed then it should exit the program. please help.
output:
You received and Error
Enter a Sentence.....
Exception in thread "main" java.lang.NullPointerException
at StringtoFile.addline(StringtoFile.java:31)
import java.util.*;
import java.io.*;
import java.lang.*;
public class StringtoFile {
public static void main(String[] args) throws IOException
{
StringtoFile h = new StringtoFile ();
h.openFile();
h.addline();
h.closefile();
}
private Scanner scan, X;
public void openFile(){
try{
X = new Scanner(new File("testFile.txt"));
}
catch(Exception e){
System.out.println("You received and Error");
}
}
public void addline(){
System.out.println("Enter a Sentence.....");
String str, casefinish = "done";
str = X.nextLine();
if(casefinish.equalsIgnoreCase("done")){
//break;
}
else{
while (str!="done");
str = X.nextLine();
}
}
public void closefile(){
X.close();
}
}
output:
You received and Error
Enter a Sentence.....
Exception in thread "main" java.lang.NullPointerException
at StringtoFile.addline(StringtoFile.java:31)