简体   繁体   中英

Exception in thread "main" java.lang.NoSuchMethodError: 'void Car.setYear(java.lang.Integer)'

在此处输入图片说明 在此处输入图片说明 I am writing this code for a class and it is asking me for the following problem statement: Problem Statement Design a class named Car that has the following fields:

year : The year field is an integer that holds the car's year.

make : the make field is a String that holds the make of the car.

speed : the speed field is an integer that holds the car's current speed.

In addition, the class should have the following constructor and other methods:

Constructor : the constructor should accept the car's year >model and make as parameters. These values should be assigned to the object's yearand make fields. The constructor should also assign 0 to the speed field.

Accessors : design appropriate accessor methods to get the values stored in an object's year, make, and speed fields.

I am receiving the error message. "Exception in thread "main" java.lang.NoSuchMethodError: 'void Car.setYear(java.lang.Integer)' at Mod10MyCar.main(Mod10MyCar.java:21)"

Is anyone able to help me out here?

import java.util.Scanner;

public class Mod10MyCar {

public static void main(String[] args) {

    // Declare input variables
    Integer inputYear;
    String inputMake;
    Integer inputSpeed;

    // Create a Car object
    Car Type = new Car();

    Scanner keyboard = new Scanner(System.in);

    // get values of a car
    System.out.println("What is the year of the car?");
    inputYear = keyboard.nextInt();
    Type.setYear(inputYear);

    System.out.println("What is the make of the car?");
    inputMake = keyboard.nextLine();
    Type.setMake(inputMake);

    //Show values of car
    System.out.println("The car year is " + Type.getYear());
      System.out.println("The make is " + Type.getMake());

}
}

class Car {

// Fields
String make;
Integer year;
Integer speed;

// Constructor
public void Car(String make, Integer year) {

    this.year = year;
    this.make = make;
    this.speed = 0;
}

// Mutators
public void setYear(Integer year) {
    this.year = year;
}

public void setMake(String make) {
    this.make = make;
}

public void setSpeed(Integer speed) {
    this.speed = speed;
}

// Accessors
public String getMake() {
    return make;
}

public Integer getYear() {
    return year;
}

public Integer getSpeed() {
    return speed;
}
}

I do not know the IDE that you are using but from this I can assume that you are only compiling the Mod10MyCar class and not Car as you would want to. I don't know how it works on this IDE but try compiling both. Or make two different .java so you are sure when you compile the whole project will be compiled.

在此处输入图片说明

Also for java i would reccomend Intellij IDE. It is free for students.

There are following issues with your code:

  1. default Constructor is missing
  2. parameterised constructor is void type, which is wrong. Ideally, constructors have no return type
  3. Car Type = new Car(); since there is no default constructor, this call makes no sense.

Use the following code :

    import java.util.Scanner;

public class Mod10MyCar {

public static void main(String[] args) {

    // Declare input variables
    int inputYear;
    String inputMake;

    Scanner keyboard = new Scanner(System.in);

    // get values of a car
    System.out.println("What is the year of the car?");
    inputYear = keyboard.nextInt();

    System.out.println("What is the make of the car?");
    keyboard = new Scanner(System.in);
    inputMake = keyboard.nextLine();
    
    Car Type = new Car(inputMake, inputYear);

    //Show values of car
    System.out.println("The car year is " + Type.getYear());
      System.out.println("The make is " + Type.getMake());
      System.out.println("Speed :" + Type.getSpeed());

}
}

class Car {

// Fields
String make;
int year;
int speed;

Car() {}

// Constructor
public Car(String make, Integer year) {
    this.year = year;
    this.make = make;
}

// Mutators
public void setYear(Integer year) {
    this.year = year;
}

public void setMake(String make) {
    this.make = make;
}

public void setSpeed(Integer speed) {
    this.speed = speed;
}

// Accessors
public String getMake() {
    return make;
}

public Integer getYear() {
    return year;
}

public Integer getSpeed() {
    return speed;
}
}

Ok. First of all, I want to thank each and every one of you for taking the time to help me. It appears that something weird was going on with how I saved the .java file.

I basically created a whole new .java file named myCar3.java and copied all of the source code and pasted it in the new file also changing the public class name to match it. This fixed whatever weird issue was going on.

To add to the weirdness, I reopened the Mod10MyCar.java file (the one I posted) and the error went away. Seems like it was most likely the IDE that was "glitching". Consider this a issue with the IDE and not the source code.

Thank you all again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM