Can anyone tell me why I get the following error message when I execute this code: Exception in thread "main" java.lang.NoSuchMethodError: main

public class Pay
{
private double hoursWorked = 40;    
private double rateofPayPerHour = 10;   
private double withholdingRate = .15;
private double grossPay;    
private double netPay;

    public void main(String[] args)
    {
        computeNetPay(hoursWorked, rateofPayPerHour, withholdingRate);
    }
    public void computeNetPay(double hoursWorked, double rateofPayPerHour, double withholdingRate)
    {
        double withHolding;
        grossPay = hoursWorked * rateofPayPerHour;
        withHolding =  grossPay * withholdingRate;
        netPay = grossPay - withHolding;
        System.out.print(netPay);
    }

}

Edited 3 Years Ago by Nick Evan: Fixed formatting

You are missing missing "static" keyword in main method public static void main(String[] args)

Edited 7 Years Ago by peter_budo: n/a

Yeah, that what I thought, but when I had "static" to the declaration I get the following compile problems on my method call [computeNetPay(hoursWorked, rateofPayPerHour, withholdingRate);]: “Pay.java:11 non-static variable hoursWorked cannot be referenced from a static context” I’ll get this error message for all my variable in the method call.

public static void main(String[] args){
      new Pay();
}

Implement constructor Pay().
From constructor invoke computeNetPay - method.

The class that contians 'public static void main' must have static declared for all methods contained.

so your code should look like this...

public class Pay
{public static void main(String[] args)
{

//program
}//end main method

public static void myFunction(){
}//end myFunction

} //endClass

Edited 7 Years Ago by PopeJareth: n/a

wendellrob,
if you make all your variables and the computeNetPay() method static you'll be able to access them from main() (that should be also static). Since your variables are static you don't have to create an instance of your class (Pay).
If you don't want to declare your variables and computeNetPay() method static and still make a call to computeNetPay() from main() then you have to create in main() an instance of your class Pay. Then you can access these variables and method through the instance.
Of course you can also do what quuba mentioned: create an instance of your Pay class in main() method and call computeNetPay() method within the constructor.

The class that contians 'public static void main' must have static declared for all methods contained.

That is not true.

Static is used so that a class can call methods on itself without having to create a specific object of that class. Static methods in a class can be called without having to instantiate an object of that type. If you changed your computeNetPay() method to static then you could call it in the main() method. However the OO way is to use the main method to create an instance of that class, in the form of an object, and then use it's constructor to call methods (which is what freelancelote and quuba said). An example using your code would be.

public class Pay{

     private double hoursWorked = 40;
     private double rateofPayPerHour = 10;
     private double withholdingRate = .15;
     private double grossPay;
     private double netPay;

     //added static modifier
     public static void main(String[] args){
              //this instantiates an object of the type Pay, which calls its constructor
              new Pay();
      }
      //Pay's constructor, a special type of method that creates object
      public Pay(){
                //now this is where you can run your method
               computeNetPay(hoursWorked, rateofPayPerHour, withholdingRate);
      }
      public void computeNetPay(double hoursWorked, double rateofPayPerHour, double withholdingRate){
              double withHolding;
              grossPay = hoursWorked * rateofPayPerHour;
              withHolding = grossPay * withholdingRate;
              netPay = grossPay - withHolding;
              System.out.print(netPay);
     }

}

Edited 7 Years Ago by jasimp: n/a

import java.applet.Applet;

import java.awt.Graphics;

public class DrawLineExample extends Applet{

public void paint(Graphics g){

g.drawLine(10,10,50,50);


g.drawLine(10,50,10,100);


g.drawLine(10,10,50,10);

}

}
while running i got error message

import java.applet.Applet;

import java.awt.Graphics;

public class DrawLineExample extends Applet{

public void paint(Graphics g){

g.drawLine(10,10,50,50);


g.drawLine(10,50,10,100);


g.drawLine(10,10,50,10);

}

}
while running i got error message

Please create a new thread. Each thread is for one problem most of the time

Thanks,
Mitch

This article has been dead for over six months. Start a new discussion instead.