Hi i have this project for class in which i have to ask the user to input a file name add 5 lines to it close the file open it again and add anothr 5 lines, then post everything to the screen here is my code
import java.io.*; import java.util.*; public class siasJorge_project_03 { public static void main(String[] args) { PrintWriter outputStream=null; Scanner keyboard = new Scanner(System.in); String line = " "; int input = 0; int input2 = 0; String name = " "; System.out.println("Please enter the name of the file we will be working on, dont forget to add the extensiuon type...\n"); name = keyboard.next(); try { BufferedReader inputStream = new BufferedReader(new FileReader(name)); System.out.println("Please enter 5 lines of text: "); for (input=0; input<5; input++) { line = keyboard.nextLine(); outputStream.println(line); } inputStream.close(); outputStream = new PrintWriter(new FileOutputStream(name, true)); System.out.println("Please enter 5 more lines of text: "); for (input2 = 0; input2 < 5; input2++) { line = keyboard.nextLine(); outputStream.println(line); } inputStream.close(); line = inputStream.readLine(); while (line != null) { System.out.println("The content of " + name + " is : " + line); line = inputStream.readLine(); } } catch (FileNotFoundException e) { System.out.println("File " + name + " was not found"); System.out.println(" or could not be opened. "); } catch (IOException e) { System.out.println("Error reading from file " + name); } } }
The code compiles but crashes giving me a Exception in thread "main" java.lang.NullPointerException at sisJorge_project_03.main<siasJorge_project_03.java :27>
any clue what i am doing wrong??
ThankYou