1

So I have a program that will calculate exponential growth, and it looks like it should work...but when I try to run it in NetBeans it says class has no main method. Another question about this, when I compile a file, where will it be saved, and will it be runnable? thanks for any answers. heres the code

import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Answerfinder extends JFrame {
private static final int WIDTH = 400;
private static final int HEIGHTH = 300;

private JLabel Ial, Ratelabel,earn1, earn2, earn3, earn4, earn5, earn10;
private JLabel equalabel;
private JTextField ratein,initialamin, earn1out, earn2out, earn3out, earn4out, earn5out, earn10out;
private JButton calculatebut;
private CalculateButtonHandler cbhandler;

public static void main(String args) {
    Answerfinder find = new Answerfinder();

}

public Answerfinder() {

    // adding the labels
    Ial = new JLabel("Enter your initial amount:",SwingConstants.RIGHT);
    Ratelabel = new JLabel("Enter growth percentage (just numbers):",SwingConstants.RIGHT);
    earn1 = new JLabel("Total by year 1:",SwingConstants.RIGHT);
    earn2 = new JLabel("Total by year 2:",SwingConstants.RIGHT);
    earn3 = new JLabel("Total by year 3:",SwingConstants.RIGHT);
    earn4 = new JLabel("Total by year 4:",SwingConstants.RIGHT);
    earn5 = new JLabel("Total by year 5:",SwingConstants.RIGHT);
    earn10 = new JLabel("Total by year 10:",SwingConstants.RIGHT);
    equalabel = new JLabel("Press this to calculate: ");

    //adding the textfields
    ratein = new JTextField();
    initialamin = new JTextField();
    earn1out = new JTextField();

    //adding button
    calculatebut = new JButton("Calculate");
    cbhandler = new CalculateButtonHandler();
    calculatebut.addActionListener(cbhandler);

    // Sets title of program
    setTitle("Exponential Growth Calculator");
   // Sets size of frame, whether or not it's visible, and what do on close
    setSize(WIDTH, HEIGHTH);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    // establishes container and sets the layout
    Container pane = getContentPane();
    pane.setLayout(new GridLayout (8,2));

    pane.add(Ial);
    pane.add(initialamin);
    pane.add(Ratelabel);
    pane.add(ratein);
    pane.add(equalabel);
    pane.add(calculatebut);
    pane.add(earn1);
    pane.add(earn1out);
    pane.add(earn2);
    pane.add(earn2out);
    pane.add(earn3);
    pane.add(earn3out);
    pane.add(earn4);
    pane.add(earn4out);
    pane.add(earn5);
    pane.add(earn5out);
    pane.add(earn10);
    pane.add(earn10out);
}

private class CalculateButtonHandler implements ActionListener {


    @Override
    public void actionPerformed(ActionEvent e) {

        double rate, initamount, returns1, returns2, returns3, returns4, returns5, returns10;

        rate = Double.parseDouble(ratein.getText());
        if (rate > 1) {
            rate = (rate / 100) + 1;
        } else {
            rate = rate +1;
        }
        initamount = Double.parseDouble(initialamin.getText());
        returns1 = initamount * Math.pow(rate, 1);
        returns2 = initamount * Math.pow(rate, 2);
        returns3 = initamount * Math.pow(rate, 3);
        returns4 = initamount * Math.pow(rate, 4);
        returns5 = initamount * Math.pow(rate, 5);
        returns10 = initamount * Math.pow(rate, 10);
    }
}

}

2
  • 2
    As is usually the case, the error message told you exactly where to look. The main lesson to get from this is that this will almost always true for your future errors as well. Mar 11, 2014 at 1:31
  • 1
    Voting to close as a "simple typographical error". Mar 11, 2014 at 1:33

4 Answers 4

4

You have:

public static void main(String args) {

You mean:

public static void main(String[] args) {

Alternatively:

public static void main(String... args) {


The main method must take an array of parameters. Specifically, from JLS 12.1.4:

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String. Therefore, either of the following declarations is acceptable:

public static void main(String[] args)

public static void main(String... args)
2
  • Thank you so much. Gah, so many stupid mistakes made today...
    – NickM
    Mar 11, 2014 at 1:33
  • Re: "Another question about this, when I compile a file, where will it be saved, and will it be runnable?" By default it will be compiled to a .class file in the same directory that the source file is in, but IDEs like Eclipse or NetBeans may change that behavior, so it depends. Eclipse creates a bin directory parallel to the source tree; I'm not familiar with NetBeans but I presume it's similar. It will be runnable only if it has a properly declared main.
    – Jason C
    Mar 11, 2014 at 1:35
1

REPLACE:

 public static void main(String args) {
    Answerfinder find = new Answerfinder();
 }

WITH:

 public static void main(String[] args) {         //was missing []
    Answerfinder find = new Answerfinder();
 }

OR:

 public static void main(String...args) {         //could also use ...
    Answerfinder find = new Answerfinder();
 }
1

main() takes an array of Strings as its argument(s).

You can do either:

public static void main(String[] args)

or

public static void main(String... args)
0

Netbeans will by default compile the project to the bin directory which it will create at the root of where you application is stored.

1
  • I now have a .jar file, however it will not run when double clicked
    – NickM
    Mar 11, 2014 at 2:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.