Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java "java.lang.NoSuchMethodError: main"

To my understanding of the error, the most common cause is because I haven't included "public static void main (String[] args)", but I've done this prior to discovering the error, which is leaving me stumped. Can anyone help me out?

import java.io.*;
class basketBall
{
    private String name;    
    private double number;
    private String team;

    // declare getter method public
    public String getName()
    {
        return name;
    }

    // declare setter method public
    public void setName(String n)
    {
        name = n;
    }

    // declare getter method public
    public String getTeam()
    {
        return team;
    }

    // declare setter method public
    public void setTeam(String t)
    {
        team = t;
    }

    // declare getter method public
    public double getNumber()
    {
        return number;
    }

    // declare setter method public
    public void setNumber(double num)
    {
        number = num;
    }

    // declare dribble method
    void dribble()
    {
        System.out.println (name + ", " + number + " dribbles down the court...");
    }

    // declare shoot method
    void shoot()
    {
        System.out.println (name + " shoots... And he scores, for the " + team + "'s!");
    }
}

// test class for basketBall class
class basketBallTester
{
    public static void main (String[] args)
    {
        //construct player and fills in its objects 
        basketBall Player1 = new basketBall();
        // fill in objects of player1
        Player1.setName("Ethan");
    Player1.setTeam("Vikings");
    Player1.setNumber(15);

        // call methods
        Player1.dribble();
        Player1.shoot();
    }
}
like image 673
Mashu Avatar asked Jan 17 '26 03:01

Mashu


1 Answers

You have several options to run your program this. One of them is:

  1. Create a separate basketBallTester.java file, and place there your class basketBallTester.
  2. Compile both files: basketBallTester.java and basketBall.java
  3. Run your program with java basketBallTester
like image 98
Pablo Santa Cruz Avatar answered Jan 19 '26 16:01

Pablo Santa Cruz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!