• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

null

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
greetings folks. I have a problem using the simple class shown below. It provides a couple of methods which create and print data to a file. There is also a brief class that creates a FileStorage object and uses the methods. Both compile fine. In runtime however, I get the following error: "Exception in thread "main" java.lang.NullPointerException" Can anyone shown me where I'm going wrong here, I get the feeling I've missed something really obvious. Also what does this message mean? Here's what I wrote:
import java.io.*;
class FileStorage {
public FileStorage(String fileName) throws Exception {
File storage = new File(fileName);
FileOutputStream diskFileStream =
new FileOutputStream(fileName);
PrintStream target = new PrintStream(diskFileStream);
}
public void print(String s) {
System.out.print(s);
target.print(s);
}
public void println(String s) {
System.out.println(s);
target.println(s);
}
File fileName;
FileOutputStream diskFileStream;
PrintStream target;
}
and now for a quick try out:
import java.io.*;

class TryFileStorage {
public static void main(String [] args) throws Exception {

String theData = "I'm a string to be saved to disc";
String fileName = "TheFile";

FileStorage discFile = new FileStorage(fileName);
discFile.println(theData);
discFile.print(theData);
}
}
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Get rid of declarations from FileStorage class
//File fileName;
//FileOutputStream diskFileStream;
//PrintStream target;
2) put the declaration/definition of the stream in a try catch block
try
{
File storage = new File(fileName);
FileOutputStream diskFileStream = new FileOutputStream(fileName);
PrintStream target = new PrintStream(diskFileStream);
}
catch (Exception e)
{
}
3) change 'target' to 'this' in the print and println methods
this.print(s);
this.println(s);
4) I was able to get rid of the error but now the problem is that it keeps printing to the screen forever but doesn't write anything to the file. However,The file does get created.
5) I would have declared/defined the streams in the print and println method with nested try catch meaning a try-catch should be around the 'this.print(s)' method.
That's the end of my 2 cents. I'll keep working on it.
Shama

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
You have defined the variables with the same name as the class variables and in the constructor of the class,what is happening is the variable target is being assigned in the constructor but it is limited within the scope of the constructor,and the class level variable "target" is initaialized to null by default..so remove the declaration inside the constructor..it should not be
FileOutputStream diskFileStream;
PrintStream target;
in the constructor but just say
diskFileStream =new FileOutputStream(fileName);
target = new PrintStream(diskFileStream);
Now the "target" is assigned a value and is not null,so when you call a method on target it will not raise a null pointer exception.
Surya
 
mark pays
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhh! I declared the various file variables twice. Sometimes it takes someone else to point out the obvious. I removed the declarations from the constructor body and just used the instance variables to create the file. It works just fine now, thanks folks.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic