954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Exception in thread "main" java.lang.NoSuchMethodError: main

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);
}

}

wendellrob
Newbie Poster
3 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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.

wendellrob
Newbie Poster
3 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
public static void main(String[] args){
      new Pay();
}

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

quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 123
Solved Threads: 106
 

I don't understand!

wendellrob
Newbie Poster
3 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 123
Solved Threads: 106
 

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
PopeJareth
Light Poster
29 posts since Jun 2009
Reputation Points: 11
Solved Threads: 3
 

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.

freelancelote
Junior Poster in Training
89 posts since Aug 2008
Reputation Points: 34
Solved Threads: 2
 
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);
     }

}
jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
 

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

parihar2992
Newbie Poster
1 post since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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

mitch9654
Light Poster
48 posts since Nov 2009
Reputation Points: 13
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You