Problem with a NullPointerException error

Here is my code (it is only one class out of a program but I thought maybe I could get some help without haveing to post the whole program since it is fairly long.):

import java.util.Formatter;

import java.io.FileNotFoundException;

public class PrintPage

{

public Formatter output;

public void Header()

{

TodaysDate day = new TodaysDate();

System.out.printf("%47s", "Robotics Design Report");

System.out.println();

System.out.printf("%49s", "Erik Lindow, Design Manager");

System.out.println();

System.out.printf("%39s", "Report Date ");

day.DayShort();

System.out.println("\n");

}

public void Header2()

{

System.out.printf("%41s%9s%21s\n", "Airplane", "Wing", "Minimum Air Speed");

System.out.printf("%40s%10s%9s%10s\n", "Weight", "Area", "m/2", "mph");

System.out.printf("" + "%13s%10s", "--", "");

System.out.printf("%10s%10s\n", "", "");

}

public void FileHeader1()

{

TodaysDate day = new TodaysDate();

output.format("%47s", "Robotics Design Report" + "\n");

output.format("%49s", "Erik Lindow, Design Mangaer" + "\n");

output.format("%39s", "Report Date ");

day.DayShort();

output.format("\n\n");

}

public void FileHeader2()

{

output.format("%41s%9s%21s\n", "Airplane", "Wing", "Minimum Air Speed");

output.format("%40s%10s%9s%10s\n", "Weight", "Area", "m/2", "mph");

output.format("" + "%13s%10s", "--", "");

output.format("%10s%10s\n", "", "");

}

}

The problem I am having is that when I run the program I get a NullPointerException error at this line:

output.format("%47s", "Robotics Design Report" + "\n");

I don't know why I am getting the error.Also, I am trying to use Formatter to print out the results of this to a file that can be printed....is this the right syntax to do this?

Thanks

Erik

[1986 byte] By [Icarus1078a] at [2007-11-26 18:24:08]
# 1
You haven't instantiated output. output is just a reference. You have to assign an object instance to the reference otherwise you will get a NullPointerException because you have declared output as a field and it would be assigned null.
qUesT_foR_knOwLeDgea at 2007-7-9 5:58:12 > top of Java-index,Java Essentials,Java Programming...
# 2
>import java.util.Formatter;I guess itsimport java.util.logging.Formatter;Which version of Java r u using?
AbiSSa at 2007-7-9 5:58:12 > top of Java-index,Java Essentials,Java Programming...
# 3
As quest_ said, you probably "forgot" to instantiate output.Also, is there a compelling reason for ouput to be a public data member of the class? If not, try understanding the concept of encapsulation.
karma-9a at 2007-7-9 5:58:12 > top of Java-index,Java Essentials,Java Programming...
# 4
I am using JCreator 4.00 LE on the 1.6 Java Runtime.Also how would I instantiate output?ThanksErik
Icarus1078a at 2007-7-9 5:58:12 > top of Java-index,Java Essentials,Java Programming...
# 5
> Also how would I instantiate output?? By using one of Formatter's constructor and the new keyword. And what JCreator or any other IDE has to do with this? Please try to understand the basics of programming before writing "fairly long" programs, for your own good.
karma-9a at 2007-7-9 5:58:12 > top of Java-index,Java Essentials,Java Programming...
# 6

Sorry about this but I am actually in a Java class and I am stuck on my program and I cannot get anyone else to help me so I thought I would post this here. I am trying to learn the Java language and this program was the first one assigned to us. So I am not trying to dive in this fast but I have to so that I can get this program done.

Icarus1078a at 2007-7-9 5:58:12 > top of Java-index,Java Essentials,Java Programming...
# 7

> Sorry about this but I am actually in a Java class

And they asked you to write a fairly long program without teaching you how to instantiate objects in Java? Doesn't look good at all. Consider reading a good book on the subject, like [url=http://www.mindview.net/Books/TIJ/]Thinking In Java[/url]. The electronic version of the book is free.

Also, the [url=http://www.exampledepot.com/]Java Developer's Almanac[/url] has extensive code samples.

karma-9a at 2007-7-9 5:58:12 > top of Java-index,Java Essentials,Java Programming...
# 8
Just to let yall know I got it working.Thanks for the adviceErik
Icarus1078a at 2007-7-9 5:58:12 > top of Java-index,Java Essentials,Java Programming...