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

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

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

	//Construct labels and text boxes
	JLabel nameLabel = new JLabel("Name:                    ");
		JTextField name = new JTextField(30);
	JLabel idLabel = new JLabel ("Student ID");
		JTextField id = new JTextField(30);
	JLabel courseNumLabel = new JLabel("Transfer Course Number:			");
		JTextField courseNum = new JTextField(30);
	JLabel localCourseLabel = new JLabel("Local Course Number:");
		JTextField localCourse = new JTextField(30);

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

	public static void main(String[] args)
	{
		Transfer f = new Transfer();
		f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		f.setSize(450,300);
		f.setTitle("Transfer Course Substitutions");
		f.setVisible(true);
	} //ends main method

	public Transfer()
	{
		Container c = getContentPane();
		c.setLayout((new BorderLayout()));
		fieldPanel.setLayout(new GridLayout(4,2));
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

		//Add fields to rows
		fieldPanel.add(nameLabel);
		fieldPanel.add(name);

		fieldPanel.add(idLabel);
		fieldPanel.add(id);

		fieldPanel.add(courseNumLabel);
		fieldPanel.add(courseNum);


		fieldPanel.add(localCourseLabel);
		fieldPanel.add(localCourse);

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

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

		//add functionality to buttons
		submitButton.addActionListener(this);
		exitButton.addActionListener(this);

		try
		{
			new DataOutputStream(new FileOutputStream("Transfer.dat"));
		} //ends try statement
		catch (IOException io)
		{
			System.exit(1);
		} //ends catch statement

		addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and submit the file?", "File Submittions", JOptionPane.YES_NO_OPTION);
					if (answer == JOptionPane.YES_OPTION)
						System.exit(0);
				} //ends windowClosing statement
			} //ends window adpater
		); //ends window listener
}//ends transfer constructor
	public void actionPerformed(ActionEvent e)
	{
		String arg = e.getActionCommand();
		if (arg == "Submit")
		{
			try
			{
				output.writeUTF(name.getText());
				output.writeUTF(id.getText());
				output.writeUTF(courseNum.getText());
				output.writeUTF(localCourse.getText());
			} //ends try statement
			catch(IOException ex)
			{
				System.exit(1);
			} //ends catch statement
			clearFields();
		} //ends if statement
		else //code to execute if the user clicks Exit
		{
			try
			{
				output.close();
			} //ends try statement
			catch(IOException c)
			{
				System.exit(1);
			}//ends catch statement
			System.exit(0);
		} //ends else statement
	}//ends action performed method
	public void clearFields()
	{
		//Clear fields and reset the focus
		name.setText("");
		id.setText("");
		courseNum.setText("");
		localCourse.setText("");
		name.requestFocus();
	} //ends clear fields method
} //ends transfer class

Ok I have attached the "weird" message I am getting in the command prompt in the back.
I dont understand what causes this. I think it comes from something in my action performed method, but I don't understand what.
Appreciate any guidance/help.

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by KSUliz
0

seriously, you're not wasting bandwith by posting your exceptions on the forum instead of expecting us to open an attachement. very few of us actually will, you know

0

seriously, you're not wasting bandwith by posting your exceptions on the forum instead of expecting us to open an attachement. very few of us actually will, you know

Well since I didnt know what all of it meant, and what all was important I thought it would be helpful, at least when I help people screenshots are helpful, and I did not know how to copy and paste from that screen I just attached...figured out how to do that, so theres the exceptions that I am getting in the command prompt and hopefully someone can help

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Transfer.actionPerformed(Transfer.java:111)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:18
49)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

0

Have you tried reading the errors that you get? Because If you had the solution would be very easy:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Transfer.actionPerformed(Transfer.java:111)

You get a NullPointerException at the Transfer.java file, at the actionPerformed method at line 111.

You are using something which is null. Initialize it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.