I am having a small bit of trouble with a certain program. Here's the program requires. The program developed in Chapter 8 sent data to an external, sequential data named payment11192007 (or payment plus the system date for your computer). It opened a connection to the DataOutputStream and used the writeUTF() methods to transfer data to the disk. Write a new program named PaymentReader to read the data into a Swing interface that displays each record.

Ok, so here is the first code that contains the program for chapter 8.

/*
	Chapter 8     BillPayer Power & Light
	Programmer:   ?????????
	Date:	      March 3, 2010
	Program Name: Bill Payer
*/

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;

public class BillPayer extends JFrame implements ActionListener
{
	//Declare output stream
	DataOutputStream output;

	//Construct a panel for each row
	JPanel firstRow = new JPanel();
	JPanel secondRow = new JPanel();
	JPanel thirdRow = new JPanel();
	JPanel forthRow = new JPanel();
	JPanel fifthRow = new JPanel();
	JPanel sixthRow = new JPanel();
	JPanel seventhRow = new JPanel();
	JPanel eigthRow = new JPanel();

	//Construt a panel for the fields and buttons
	JPanel fieldPanel = new JPanel();
	JPanel buttonPanel = new JPanel();

	//Construct labels and text boxes
	JLabel acctNumLabel = new JLabel("Account Number:                           ");
		JTextField acctNum = new JTextField(15);
	JLabel pmtLabel = new JLabel("Payment Amount:");
		JTextField pmt = new JTextField(10);
	JLabel firstNameLabel = new JLabel("First Name:                 ");
		JTextField firstName = new JTextField(10);
	JLabel lastNameLabel = new JLabel("Last Name:");
		JTextField lastName = new JTextField(20);
	JLabel addressLabel = new JLabel("Address:");
		JTextField address = new JTextField(35);
	JLabel cityLabel = new JLabel("City:                             ");
		JTextField city = new JTextField(10);
	JLabel stateLabel = new JLabel("State:");
		JTextField state = new JTextField(2);
	JLabel zipLabel = new JLabel("Zip:");
		JTextField zip = new JTextField(9);

	//Construct button
	JButton submitButton = new JButton("Submit");

	public static void main(String[] args)
	{
		//set the look and feel of the interface
	try
	{
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

	}
	catch(Exception e)
	{
		JOptionPane.showMessageDialog(null,"The UIManager could not set the Look and Feel for this application.","Error",JOptionPane.INFORMATION_MESSAGE);
	}

	BillPayer f = new BillPayer();
	f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	f.setSize(450,300);
	f.setTitle("Crandall Power and Light Customer Payments");
	f.setResizable(false);
	f.setLocation(200,200);
	f.setVisible(true);
	}

	public BillPayer()
	{
		Container c = getContentPane();
		c.setLayout((new BorderLayout()));
		fieldPanel.setLayout(new GridLayout(8,1));
		FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
			firstRow.setLayout(rowSetup);
			secondRow.setLayout(rowSetup);
			thirdRow.setLayout(rowSetup);
			forthRow.setLayout(rowSetup);
			fifthRow.setLayout(rowSetup);
			sixthRow.setLayout(rowSetup);
			seventhRow.setLayout(rowSetup);
			eigthRow.setLayout(rowSetup);
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

	//Add Fields to rows
	firstRow.add(acctNumLabel);
	firstRow.add(pmtLabel);

	secondRow.add(acctNum);
	secondRow.add(pmt);

	thirdRow.add(firstNameLabel);
	thirdRow.add(lastNameLabel);

	forthRow.add(firstName);
	forthRow.add(lastName);

	fifthRow.add(addressLabel);

	sixthRow.add(address);

	seventhRow.add(cityLabel);
	seventhRow.add(stateLabel);
	seventhRow.add(zipLabel);

	eigthRow.add(city);
	eigthRow.add(state);
	eigthRow.add(zip);

	//Add rows to panel
	fieldPanel.add(firstRow);
	fieldPanel.add(secondRow);
	fieldPanel.add(thirdRow);
	fieldPanel.add(forthRow);
	fieldPanel.add(fifthRow);
	fieldPanel.add(sixthRow);
	fieldPanel.add(seventhRow);
	fieldPanel.add(eigthRow);



    //Add button to panel
    buttonPanel.add(submitButton);

    //Add panels to frame
    c.add(fieldPanel, BorderLayout.CENTER);
    c.add(buttonPanel, BorderLayout.SOUTH);

    //Add functionality to buttons
    submitButton.addActionListener(this);

    //Get the current date and open the file
    Date today = new Date();
    SimpleDateFormat myFormat = new SimpleDateFormat("MMddyyyy");
    String filename = "payments" + myFormat.format(today);

    try
    {
		output = new DataOutputStream(new FileOutputStream(filename));
	}
	catch(IOException io)
	{
		JOptionPane.showMessageDialog(null,"The program could not create a storage location. Please check the disk drive and then run the program again.","Error",JOptionPane.INFORMATION_MESSAGE);

		System.exit(1);
	}

	addWindowListener(
		new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				int answer = JOptionPane.showConfirmDialog(null,"Are you sure you want to submit the file?", "File Submisson", JOptionPane.YES_NO_OPTION);
				if(answer == JOptionPane.YES_OPTION);
					System.exit(0);
			}
		}
	);
	}


	public void actionPerformed(ActionEvent e)
	{
		String arg = e.getActionCommand();

		if(checkFields())
		{
			try
			{
				output.writeUTF(acctNum.getText());
				output.writeUTF(pmt.getText());
				output.writeUTF(firstName.getText());
				output.writeUTF(lastName.getText());
				output.writeUTF(address.getText());
				output.writeUTF(city.getText());
				output.writeUTF(state.getText());
				output.writeUTF(zip.getText());

				JOptionPane.showMessageDialog(null,"The payment information has been saved.", "Submission Successful", JOptionPane.INFORMATION_MESSAGE);
			}
			catch(IOException c)
			{
				System.exit(1);
			}
			clearFields();
		}
	}

	public boolean checkFields()
	{
		if ((acctNum.getText().compareTo("")<1)	  ||
		    (pmt.getText().compareTo("")<1)	      ||
		    (firstName.getText().compareTo("")<1) ||
		    (lastName.getText().compareTo("")<1)  ||
		    (address.getText().compareTo("")<1)	  ||
		    (city.getText().compareTo("")<1)	  ||
		    (state.getText().compareTo("")<1)	  ||
		    (zip.getText().compareTo("")<1))
		{
			JOptionPane.showMessageDialog(null,"You must complete all fields.","Data Entry Error",JOptionPane.WARNING_MESSAGE);
			return false;
		}
		else
		{
			return true;
		}
	}

	public void clearFields()
	{
		//Clear fields and reset the focus
		acctNum.setText("");
		pmt.setText("");
		firstName.setText("");
		lastName.setText("");
		address.setText("");
		city.setText("");
		state.setText("");
		zip.setText("");
		acctNum.requestFocus();
	}
}

Now here is the code i'm writing (the one that needs to read the data).

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;


public class DataRead extends JFrame
{
	//construct components
	JTextPane textPane = new JTextPane();




	//create the content pane
	public Container createContentPane()
	{

		//create the JTextPanel and center panel
		JPanel centerPanel = new JPanel();
			textPane = addTextToTextPane();
			JScrollPane scrollPane = new JScrollPane(textPane);
				scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				scrollPane.setPreferredSize(new Dimension(580, 580));
			centerPanel.add(scrollPane);

		//create Container and set attributes
		Container c = getContentPane();
			c.setLayout(new BorderLayout(1100,50));
			c.add(centerPanel,BorderLayout.CENTER);

		return c;
	}

	//method to add new text to the JTextPane
		public JTextPane addTextToTextPane()
		{
			DataInputStream input;
			Document doc = textPane.getDocument();
			try
			{
				input = new DataInputStream(new FileInputStream("11192007"));
				acctNum.setText(readUTF());

			}
			catch(IOException e)
			{
				System.err.println("Couldn't insert text");
			}

			return textPane;
	}

	//main method executes at run time
		public static void main(String args[])
		{
			JFrame.setDefaultLookAndFeelDecorated(true);
			DataRead f = new DataRead();
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			f.setContentPane(f.createContentPane());
			f.setSize(800,575);
			f.setVisible(true);
		}
}

Now, obvisouly it won't read the acctNum since it hasn't been identified. But for some odd reason, it won't read the readUTF method. It gives me this error: java:44: cannot find symbol
symbol : method readUTF()
location: class DataRead
acctNum.setText(readUTF());

So, how am I suppose to actually make the program read data files using the readUTF method?

Thanks for the help.

It is because that method doesn't exist (readUTF) when you call it like this: readUTF(); Now if that method was part of another class, like DataInputStream then maybe you should have done this:

input = new DataInputStream(new FileInputStream("11192007"));
acctNum.setText(input.readUTF());

Also check the API for DataInputStream and FileInputStream. If you get any other errors refer to them and see what arguments the methods you are using take and what they return

It is because that method doesn't exist (readUTF) when you call it like this: readUTF(); Now if that method was part of another class, like DataInputStream then maybe you should have done this:

input = new DataInputStream(new FileInputStream("11192007"));
acctNum.setText(input.readUTF());

Also check the API for DataInputStream and FileInputStream. If you get any other errors refer to them and see what arguments the methods you are using take and what they return

Well I tried that and the API gets screwy.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;

public class DataRead extends JFrame
{
	//construct components
	JTextPane textPane = new JTextPane();
	JLabel acctNum;
	DataInputStream input;




	//create the content pane
	public Container createContentPane()
	{

		//create the JTextPanel and center panel
		JPanel centerPanel = new JPanel();
			textPane = addTextToTextPane();
			JScrollPane scrollPane = new JScrollPane(textPane);
				scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				scrollPane.setPreferredSize(new Dimension(580, 580));
			centerPanel.add(scrollPane);

		//create Container and set attributes
		Container c = getContentPane();
			c.setLayout(new BorderLayout(1100,50));
			c.add(centerPanel,BorderLayout.CENTER);

		return c;
	}

	//method to add new text to the JTextPane
		public JTextPane addTextToTextPane()
		{
			Document doc = textPane.getDocument();
			try
			{
				input = new DataInputStream(new FileInputStream("payment03062010"));
				acctNum.setText(input.readUTF());
			}
			catch(IOException io)
			{
				JOptionPane.showMessageDialog(null,"Could not find data file","Error",JOptionPane.INFORMATION_MESSAGE);
				System.exit(1);
			}

			return textPane;
	}

	//main method executes at run time
		public static void main(String args[])
		{
			JFrame.setDefaultLookAndFeelDecorated(true);
			DataRead f = new DataRead();
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			f.setContentPane(f.createContentPane());
			f.setSize(800,575);
			f.setVisible(true);
		}
}

I checked my book and it says: To create a program to read the payment data, declare a variable with the DataInputStream.

DataInputStream input;

Then, wrap it in the FileInputStream buffer.

input = new DataInputStream)new FileInputStream("payment11192007"));

Finally, code a readUTF() method for each field to read the data file.

name.setText(readUTF());

I'm really confused.

It looks to me like you're using DataInputStream and FileInputStream properly. Where are you getting stuck? Is it doing anything from the setText line?

It looks to me like you're using DataInputStream and FileInputStream properly. Where are you getting stuck? Is it doing anything from the setText line?

Well, when I compile the code, it gives me this text in the API:

Exception in thread "main" java.lang.NullPointerException
at DataRead.addTextToTextPane(DataRead.java:44)
at DataRead.createContantPane(DataRead.java:23)
at DataRead.main(Data.java:68)

Maybe it's because I'm declaring the acctNum/others as JPanels?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;

public class DataRead extends JFrame
{
	//construct components
	JTextPane textPane = new JTextPane();
	JLabel acctNum, pmt, zip, lastName, firstName, state, city, address;
	DataInputStream input;




	//create the content pane
	public Container createContentPane()
	{

		//create the JTextPanel and center panel
		JPanel centerPanel = new JPanel();
			textPane = addTextToTextPane();
			JScrollPane scrollPane = new JScrollPane(textPane);
				scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				scrollPane.setPreferredSize(new Dimension(580, 580));
			centerPanel.add(scrollPane);

		//create Container and set attributes
		Container c = getContentPane();
			c.setLayout(new BorderLayout(1100,50));
			c.add(centerPanel,BorderLayout.CENTER);

		return c;
	}

	//method to add new text to the JTextPane
		public JTextPane addTextToTextPane()
		{
			Document doc = textPane.getDocument();
			try
			{
				input = new DataInputStream(new FileInputStream("payment03062010"));
				acctNum.setText(input.readUTF());
				pmt.setText(input.readUTF());
				firstName.setText(input.readUTF());
				lastName.setText(input.readUTF());
				address.setText(input.readUTF());
				city.setText(input.readUTF());
				state.setText(input.readUTF());
				zip.setText(input.readUTF());
			}
			catch(IOException io)
			{
				JOptionPane.showMessageDialog(null,"Could not find data file","Error",JOptionPane.INFORMATION_MESSAGE);
				System.exit(1);
			}

			return textPane;
	}

	//main method executes at run time
		public static void main(String args[])
		{
			JFrame.setDefaultLookAndFeelDecorated(true);
			DataRead f = new DataRead();
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			f.setContentPane(f.createContentPane());
			f.setSize(800,575);
			f.setVisible(true);
		}
}

It's because acctNum was never initialized (i.e. you never said new JLabel())

Well after declaring them and compiling the code, it works, but the textpane shows nothing.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;

public class DataRead extends JFrame
{
	//construct components
	JTextPane textPane = new JTextPane();
	JLabel acctNum = new JLabel();
	JLabel pmt = new JLabel();
	JLabel zip = new JLabel();
	JLabel lastName = new JLabel();
	JLabel firstName = new JLabel();
	JLabel state = new JLabel();
	JLabel city = new JLabel();
	JLabel address = new JLabel();
	DataInputStream input;




	//create the content pane
	public Container createContentPane()
	{

		//create the JTextPanel and center panel
		JPanel centerPanel = new JPanel();
			textPane = addTextToTextPane();
			JScrollPane scrollPane = new JScrollPane(textPane);
				scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				scrollPane.setPreferredSize(new Dimension(580, 580));
			centerPanel.add(scrollPane);

		//create Container and set attributes
		Container c = getContentPane();
			c.setLayout(new BorderLayout(1100,50));
			c.add(centerPanel,BorderLayout.CENTER);

		return c;
	}

	//method to add new text to the JTextPane
		public JTextPane addTextToTextPane()
		{
			Document doc = textPane.getDocument();
			try
			{
				input = new DataInputStream(new FileInputStream("payment03062010"));
				acctNum.setText(input.readUTF());
				pmt.setText(input.readUTF());
				firstName.setText(input.readUTF());
				lastName.setText(input.readUTF());
				address.setText(input.readUTF());
				city.setText(input.readUTF());
				state.setText(input.readUTF());
				zip.setText(input.readUTF());
			}
			catch(IOException io)
			{
				JOptionPane.showMessageDialog(null,"Could not find data file","Error",JOptionPane.INFORMATION_MESSAGE);
				System.exit(1);
			}

			return textPane;
	}

	//main method executes at run time
		public static void main(String args[])
		{
			JFrame.setDefaultLookAndFeelDecorated(true);
			DataRead f = new DataRead();
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			f.setContentPane(f.createContentPane());
			f.setSize(800,575);
			f.setVisible(true);
		}
}

Now I tried add(acctNum) as a test and that didn't even work, so i'm still a bit stuck, but thanks for the help guys.

That is because you never called any methods that would add text to the JTextPane. See this tutorial on Text Panes. Furthermore, your addTextToTextPane method returns a JTextPane which is unnecessary. Since your JTextPane is an instance variable, your addTextToTextPane method should just be declared as returning void.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.