Help! I am running out of ides
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
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
Re: Help! I am running out of ides
Quote:
java.lang.NullPointerException
at sisJorge_project_03.main<siasJorge_project_03.java :27>
Look at line 27 and find the variable that has a null value. Then backtrack in the code to find out why that variable does not have a valid value.
Re: Help! I am running out of ides
would that be for the line = keyboard.nextLine(); ??
Re: Help! I am running out of ides
Can you look in your editor and count the lines?
Or add a println that prints out the value of all the variables on the line and see if any of them are null.