Good morning,
I would like to ask for your help, I am working on getting a text file read using using a java applet, the content of that text file is generated with an object in the class TowersOfHanoiExecute
TowersOfHanoi towersOfHanoi = new TowersOfHanoi( totalDisks );
after which I was planning to use line 23 and 24 in the TowersOfHanoiExecute class
readFileApplet myreadFileApplet = new readFileApplet();
myreadFileApplet.readFile();
to read the text file into the Applet.
unfortunatly here is the error message I get:
Exception in thread "main" java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Applet.java:152)
at readFileApplet.readFile(readFileApplet.java:26)
at TowersOfHanoiExecute.main(TowersOfHanoiExecute.java:24)
I think line 23 and 24 in the TowersOfHanoiExecute class is where my problem is , but I really have no idea why?
Could somebody pelase help me figure it out.
Thanks
John
The classes:
TowersOfHanoiExecute.java class
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class TowersOfHanoiExecute
{
public static void main(String[] args)
{// TODO Auto-generated method stub
int startPeg = 1; //Value 1 is used to indicate startPeg in output
int endPeg = 3; //Value 3 is used to indicate endPeg in output
int tempPeg = 2; //Value 2 is used to indicate tempPeg in output
int totalDisks = 3; //number of disks
System.out.println("TowerOfHanoi text file has been created");
TowersOfHanoi towersOfHanoi = new TowersOfHanoi( totalDisks );
//read into applet
readFileApplet myreadFileApplet = new readFileApplet();
myreadFileApplet.readFile();
PrintStream stdOut=null;
try
{
stdOut = new PrintStream(new FileOutputStream ("TowerOfHanoi.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.setOut(stdOut);
//initial nonrecursive call: move all disks.
towersOfHanoi.solveTowers( totalDisks, startPeg, endPeg, tempPeg );
}//end main
}//end class TowersofHanoiExecute
class: Towers Of Hanoi
public class TowersOfHanoi
{
private int numDisks;//Variable representing the number of disks to move
public TowersOfHanoi( int Disks )
{
int disks = 0;
setNumDisks(disks);
} //end TowersOfHanoi constructor
// Recursively move the disks through the towers
public void solveTowers( int disks, int sourcePeg, int destinationPeg,
int tempPeg)
{
try
{
//Base case -- only one disk to move
if ( disks == 1 )
{
//instead of the system.out.printf use the textArea.append
System.out.printf( "\n%d --> %d\r", sourcePeg, destinationPeg );
return;
} //end if
//recursion step -- move disk to tempPeg, then to destinationPeg
//move(disk -1) disks from sourcePeg to tempPeg recursively
solveTowers( disks - 1, sourcePeg, tempPeg, destinationPeg );
//move the last disk from the sourcePeg to the destinatinPeg
System.out.printf( "\n%d --> %d\r", sourcePeg, destinationPeg );
// Move(disk-1) disks from tempPeg to destinationPeg
solveTowers( disks - 1, tempPeg, destinationPeg, sourcePeg );
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}//end solveTowers
public void setNumDisks(int numDisks)
{
this.numDisks = numDisks;
}//end setNumDisks
public int getNumDisks()
{
return numDisks;
}//getNumDisks
}//end class TowersOfHanoi
class: FileViewer
import java.awt.Button;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
* This class creates and displays a window containing a TextArea, in which the
* contents of a text file are displayed.
*/
@SuppressWarnings("serial")
public class FileViewer extends Frame implements ActionListener {
String directory; // The default directory to display in the FileDialog
TextArea textarea; // The area to display the file contents into
/** Convenience constructor: file viewer starts out blank */
public FileViewer() {
this(null, null);
}
/** Convenience constructor: display file from current directory */
public FileViewer(String filename) {
this(null, filename);
}
/**
* The real constructor. Create a FileViewer object to display the specified
* file from the specified directory
*/
public FileViewer(String directory, String filename) {
super(); // Create the frame
// Destroy the window when the user requests it
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
// Create a TextArea to display the contents of the file in
textarea = new TextArea("", 24, 80);
textarea.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
textarea.setEditable(false);
this.add("Center", textarea);
// Create a bottom panel to hold a couple of buttons in
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));
this.add(p, "South");
// Create the buttons and arrange to handle button clicks
Font font = new Font("SansSerif", Font.BOLD, 14);
Button openfile = new Button("Open File");
Button close = new Button("Close");
openfile.addActionListener(this);
openfile.setActionCommand("open");
openfile.setFont(font);
close.addActionListener(this);
close.setActionCommand("close");
close.setFont(font);
p.add(openfile);
p.add(close);
this.pack();
// Figure out the directory, from filename or current dir, if necessary
if (directory == null) {
File f;
if ((filename != null) && (f = new File(filename)).isAbsolute()) {
directory = f.getParent();
filename = f.getName();
} else
directory = System.getProperty("user.dir");
}
this.directory = directory; // Remember the directory, for FileDialog
setFile(directory, filename); // Now load and display the file
}
/**
* Load and display the specified file from the specified directory
*/
public void setFile(String directory, String filename) {
if ((filename == null) || (filename.length() == 0))
return;
File f;
FileReader in = null;
// Read and display the file contents. Since we're reading text, we
// use a FileReader instead of a FileInputStream.
try {
f = new File(directory, filename); // Create a file object
in = new FileReader(f); // And a char stream to read it
char[] buffer = new char[4096]; // Read 4K characters at a time
int len; // How many chars read each time
textarea.setText(""); // Clear the text area
while ((len = in.read(buffer)) != -1) { // Read a batch of chars
String s = new String(buffer, 0, len); // Convert to a string
textarea.append(s); // And display them
}
this.setTitle("FileViewer: " + filename); // Set the window title
textarea.setCaretPosition(0); // Go to start of file
}
// Display messages if something goes wrong
catch (IOException e) {
textarea.setText(e.getClass().getName() + ": " + e.getMessage());
this.setTitle("FileViewer: " + filename + ": I/O Exception");
}
// Always be sure to close the input stream!
finally {
try {
if (in != null)
in.close();
} catch (IOException e) {
}
}
}
/**
* Handle button clicks
*/
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("open")) { // If user clicked "Open" button
// Create a file dialog box to prompt for a new file to display
FileDialog f = new FileDialog(this, "Open File", FileDialog.LOAD);
f.setDirectory(directory); // Set the default directory
// Display the dialog and wait for the user's response
f.show();
directory = f.getDirectory(); // Remember new default directory
setFile(directory, f.getFile()); // Load and display selection
f.dispose(); // Get rid of the dialog box
} else if (cmd.equals("close")) // If user clicked "Close" button
this.dispose(); // then close the window
}
/**
* The FileViewer can be used by other classes, or it can be used standalone
* with this main() method.
*/
@SuppressWarnings("deprecation")
static public void main(String[] args) throws IOException {
// Create a FileViewer object
Frame f = new FileViewer((args.length == 1) ? args[0] : null);
// Arrange to exit when the FileViewer window closes
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
// And pop the window up
f.show();
}
}