3 Replies - 5020 Views - Last Post: 18 April 2010 - 02:19 PM Rate Topic: -----

#1 gymangel812  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 15-February 10

Invalid method declaration; return type required error

Posted 18 April 2010 - 01:13 PM

I'm getting a ".../Transfer3.java:40: invalid method declaration; return type required
public Transfer()" error when I compile the code below. Why might this be? Thanks :)
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;

public class Transfer3 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();
	JLabel idLabel = new JLabel ("Student ID:");
		JTextField id = new JTextField();
	JLabel courseNumLabel = new JLabel("Transfer Course Number:");
		JTextField courseNum = new JTextField();
	JLabel localCourseLabel = new JLabel("Local Course Number:");
		JTextField localCourse = new JTextField();

	// construct buttons
	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 buttons 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
}
	public void actionPerformed(ActionEvent e)
	{
		String arg = e.getActionCommand();

		if (checkFields())
		{
			if (arg.equals("Submit"))

			try
			{
				output.writeUTF(name.getText());
				output.writeUTF(id.getText());
				output.writeUTF(courseNum.getText());
				output.writeUTF(localCourse.getText());

				JOptionPane.showMessageDialog(null,"The transfer credit infromation has been saved.","Submission Successful", JOptionPane.INFORMATION_MESSAGE);
			} // 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();
				System.out.println();
			} // ends try statement
			catch(IOException c)
			{
				System.exit(1);
			}// ends catch statement
			System.exit(0);
		} // ends else statement
	}// ends action performed method

	public boolean checkFields()
	{
		if((name.getText().compareTo("")<1)			||
			(id.getText().compareTo("")<1)			||
			(courseNum.getText().compareTo("")<1)	||
			(localCourse.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
		name.setText("");
		id.setText("");
		courseNum.setText("");
		localCourse.setText("");
		name.requestFocus();
	} // ends clear fields method
} // ends transfer class


Is This A Good Question/Topic? 0
  • +

Replies To: Invalid method declaration; return type required error

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Invalid method declaration; return type required error

Posted 18 April 2010 - 01:15 PM

Since your class is called Transfer3, the constructor must also be called Transfer3(), otherwise it is not recognized by the compiler as the constructor and requires a return type.
Was This Post Helpful? 1
  • +
  • -

#3 gymangel812  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 15-February 10

Re: Invalid method declaration; return type required error

Posted 18 April 2010 - 02:15 PM

View Postxclite, on 18 April 2010 - 12:15 PM, said:

Since your class is called Transfer3, the constructor must also be called Transfer3(), otherwise it is not recognized by the compiler as the constructor and requires a return type.

that fixed it but now i am getting more errors when i click submit button:
"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Transfer.actionPerformed(Transfer.java:111)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.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(DefaultB uttonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
istener.java:234)
at java.awt.Component.processMouseEvent(Component.jav a:5488)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3093)
at java.awt.Component.processEvent(Component.java:525 3)
at java.awt.Container.processEvent(Container.java:196 6)
at java.awt.Component.dispatchEventImpl(Component.jav a:3955)
at java.awt.Container.dispatchEventImpl(Container.jav a:2024)
at java.awt.Component.dispatchEvent(Component.java:38 03)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4212
)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3892)

at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3822)
at java.awt.Container.dispatchEventImpl(Container.jav a:2010)
at java.awt.window.dispatchEventImpl(window.java:1766 )
at java.awt.Component.dispatchEvent(Component.java:38 03)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 463)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchTh
read.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:157)

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

at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:110)"

the exit button isn't working either.
Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Invalid method declaration; return type required error

Posted 18 April 2010 - 02:19 PM

I could be wrong, but it looks like you need to instantiate output (your DataOutputStream) with the new operator.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1