Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Input/Output program Compiles but doesn't run

Thread view: 
janicethorne - 30 Mar 2006 16:58 GMT
Can anyone help me with this error? I have included the code after the
explaination.
This code uses the rectangle class (which I just included in the code,
although I was supposed to import it). The code should take in values from
the user, calculate them using the methods stored in the rectangle class, and
display them in the GUI. I was able to get my code to compile, but when it
runs, I get "Exception in thread "main" java.lang.NoSuchMethodError: main. I
have been over and over the code. I can't see what I'm doing wrong. If you
can help in any way, I would really appreciate it. I think I included
everything I was supposed to, but I am a beginner and can't always tell if I
need more. Any help will be appreciated. thank you.

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

public class Program81 {

    JFrame outputFrame;
    JLabel enterWidth;
    JLabel enterLength;
    JLabel calculateArea;
    JLabel calculatePerim;
    JTextField dataWidth;
    JTextField dataLength;
    JButton calculate;
    JButton clear;
    ButtonHandler button;

    private class ButtonHandler implements ActionListener {

            public void actionPerformed(ActionEvent event)  {

                rectangle r = new rectangle();

                r.setWidth(Integer.parseInt(dataWidth.getText()));
                r.setLength(Integer.parseInt(enterLength.getText()));

                calculateArea.setText("The area of your rectangle is " + r.calcArea());
                calculatePerim.setText("The perimeter of your rectangle is " + r.
calcPerimeter());
            }
    }//end of action handler

   public void main ( String args[] ) throws IOException {
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

    //Declare a class JFrame variable
    outputFrame = new JFrame();

    //Declare a container variable
    Container outputPane;
    outputPane = outputFrame.getContentPane();

    //default close action
    outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //default size
    outputFrame.setSize(300, 300);

    //set class flow layout argument
    outputPane.setLayout(new FlowLayout());

    //create components
    enterWidth = new JLabel("Enter Width");
    enterLength = new JLabel("Enter Length");
    calculateArea = new JLabel("");
    calculatePerim = new JLabel("");
    dataWidth = new JTextField(15);
    dataLength = new JTextField(15);
    calculate = new JButton("Calculate");
    clear = new JButton("Clear fields");

    button = new ButtonHandler();
    calculate.addActionListener(button);

    //add components to the frame
    outputPane.add(BorderLayout.WEST, enterWidth);
    outputPane.add(BorderLayout.EAST, dataWidth);
    outputPane.add(BorderLayout.WEST, enterLength);
    outputPane.add(BorderLayout.EAST, dataLength);
    outputPane.add(BorderLayout.SOUTH, calculate);
    outputPane.add(BorderLayout.SOUTH, clear);
    outputPane.add(BorderLayout.SOUTH, calculateArea);
    outputPane.add(BorderLayout.SOUTH, calculatePerim);

    // make visible
    outputFrame.setVisible(true);

}//end of main

public  class rectangle  {
        private int length;
        private int width;

        public rectangle() {
            length = 1;
            width = 1;
        }

        public rectangle(int l, int w){
            length = l;
            width = w;
        }

        public int calcArea() {
            int area = length * width;
            return area;
        }

        public int calcPerimeter() {
            int perimeter = 2 * (length + width);
            return perimeter;
        }

        public void setWidth(int w1) {
                if (w1 >= 1 && w1 <= 20)
                width = w1;
                else
                width = 1;
        }

        public void setLength(int l1) {
                if (l1 >= 1 && l1 <= 20)
                length = l1;
                else
                length = 1;
        }

        public int getWidth() {
            return width;
            }

        public int getLength() {
            return length;
            }

}//end of rectangle class

    }//end of program81
Roedy Green - 30 Mar 2006 18:43 GMT
> "Exception in thread "main" java.lang.NoSuchMethodError: main.

see http://mindprod.com/jgloss/main.html

or

http://mindprod.com/jgloss/runerrormessages.html#NOSUCHMETHODERROR
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Carl - 30 Mar 2006 19:21 GMT
> Can anyone help me with this error? I have included the code after the
> explaination.
[quoted text clipped - 7 lines]
> everything I was supposed to, but I am a beginner and can't always tell if I
> need more. Any help will be appreciated. thank you.

The java main method with the signature

public static void main(String[] args)

is special and will be used to start the application. You have not
declared this method, hence the "NoSuchMethodError: main" error; the
main method with this _exact_ signature cannot be found.

You have declared an instance method with the signature
main(String[] args).

Note that simply adding the static modifier to your main method will
not work as your main method references non static instance variables.

Hope that helps,
Carl.
janicethorne - 30 Mar 2006 19:43 GMT
>> Can anyone help me with this error? I have included the code after the
>> explaination.
[quoted text clipped - 18 lines]
>Hope that helps,
>Carl.

Carl:

Thanks for your reply. Yes, I found that out when I added the static modifier
to the code and tried to compile it. I then declared my variables private and
static along with making the main method static. The only error I am getting
now is:

C:\Documents and Settings\JThorne\Desktop\Homework\Program81.java:74: non-
static variable this cannot be referenced from a static context
    button = new ButtonHandler();

The button is in my main method. Why is this error occurring?

Signature

"The man who does not read good books has no advantage over the man who
cannot read them."

Carl - 30 Mar 2006 20:24 GMT
> Thanks for your reply. Yes, I found that out when I added the static modifier
> to the code and tried to compile it. I then declared my variables private and
[quoted text clipped - 6 lines]
>
> The button is in my main method. Why is this error occurring?

Just as the message states, you are attempting to access the non static
variable name "this" from within a static context.

I think you need to re-think your solution to your original problem.
Although it may satisfy the compilers requirements, it is unecessary
overuse of the static modifier.
The problem with your first code was not that your method was not
static, but that you were missing a main method.

I would suggest going back to your original source, renaming the method
you had named main() to something more descriptive of what it does
(like showWindow() ?) and creating a correct main() method that
instantiates the object and calls this new method. There is no good
reason to have to declare all of your GUI components static.

Hope this helps,
Carl.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.