COYG
COYG

Reputation: 1598

NoSuchMethodError when calling a method from different class

I am trying to call a method in a different class so I have initiated the class I am trying to access in the class I am calling it from, here is the code:

Car c = new Car();
Van Vans = new Van();
SportsCar s = new SportsCar();
MiniVan m  = new MiniVan();

while (SelectMotor < 1 || SelectMotor  > 4) {
    System.out.println("\t\t Choose the vechile you wish to add:");

    System.out.println("1.Car");
    System.out.println("2.Van");
    System.out.println("1.SportsCar");
    System.out.println("4.MiniVan");

    SelectMotor = in.nextInt(); 

    if (SelectMotor == 1) {
        c.AddCar();
    }

Whenever I run it I get this error:

NoMethodError

Here is the code where the method I am calling is contained:

import java.util.Scanner;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;

class Car extends Vehicle {
    Vehicle v = new Vehicle();
    WriteFile WriteCarDetails = new WriteFile(CarDetails, true);
    Scanner SaveCarDetails = new Scanner(System.SaveCarDetails);
    String WriteMe;

    void AddCar() {
        System.out.println("\n\n\t\t\tAdding a Car");
        System.out.print("\nEnter Make: ");
        v.SetMake = SaveCarDetails.next;
        System.out.print("\nEnter Model: ");
        v.SetModel = SaveCarDetails.next;
        System.out.print("\nEnter Litre: ");
        v.SetLitre = SaveCarDetails.nextDouble;
        System.out.println("\nEnter Top Speed: ");
        v.SetTopSpeed = SaveCarDetails.nextInt;
        System.out.println("\nEnter Gears: ");
        v.SetGears = SaveCarDetails.nextInt;
        System.out.println("\nEnter Doors: ");
        v.SetDoors = SaveCarDetails.nextInt;
    }
}

I'm new to Java, but have some experience in C#, I can't see why I'm getting the error showing in the picture above, as far as I can see the class I am trying to access is initiated, the name of the method matches. Am I getting the error because I am trying to access a subclass without accessing the superclass first? That's the only weird way I could think its going wrong. Thanks for any help!

Upvotes: 1

Views: 150

Answers (1)

CodeBlind
CodeBlind

Reputation: 4569

As others have suggested in the comments, you may be seeing this issue because the visibility of the AddCar method (consider renaming this to addCar as doing so will be more in-line with java's naming conventions). Changing this to public may help, but if it doesn't ...

Another possibility could be that you may have an older version of the Car class on the classpath that doesn't have the AddCar method. Since you were able to compile things just fine, it sounds to me like you have the wrong version of the Car class on the classpath in your deployment environment. Try recompiling everything and then make sure that the latest versions of all of your classes are on the classpath in your deployment area.

Upvotes: 1

Related Questions