Home > front end >  java.lang.NoSuchMethodError exception when creating an instance of child class having a constructor
java.lang.NoSuchMethodError exception when creating an instance of child class having a constructor

Time:01-13

Here I have a constructor in super class and corresponding constructor in child class.

As far as I know in such cases in child constructor I need to use the super keyword.

I have written my program as such -

public class inheritance_demo6 {
    public static void main(String[] args) {
        Dog dog = new Dog("Rohu");
        System.out.println("My name is: "   dog.getName());
        dog.eat();
    }
}

class Animal {
    protected String name;    
    
    Animal(String name) {
        this.name = name;
    }

    public void eat() {
        System.out.println("I am eating...");
    }

    public String getName() {
        return name;
    }

}

class Dog extends Animal {

    Dog(String name) {
        super(name);
    }
}

However on execution I am getting compile time error -

Exception in thread "main" java.lang.NoSuchMethodError: 'void Dog.<init>(java.lang.String)'
        at inheritance_demo6.main(inheritance_demo6.java:3)

It says the error is in line:3. In line:3 I am creating an instance of the class Dog. So if I understand correctly the error is due to some incorrect code while writing the child class constructor.

But I am unable to determine where the error can be.

I am using Visual Studio Code editor on Ubuntu.

UPDATE:

If I run the code on terminal independently it runs perfectly. It gives error only on VSCode

Here is my screenshot on VSCode -

VSCode Screenshot1

VSCode Screenshot2

My Java Version -

$ java --version
openjdk 11.0.13 2021-10-19
OpenJDK Runtime Environment (build 11.0.13 8-Ubuntu-0ubuntu1.21.10)
OpenJDK 64-Bit Server VM (build 11.0.13 8-Ubuntu-0ubuntu1.21.10, mixed mode, sharing)

Here I tried running a simple program "Hello World" in VSCode to check if there is any error in VSCode installation or some settings maybe. But it runs perfectly . Here is the screenshot -

VSCode Screenshot3

UPDATE:

I moved the three classes to three different files -

InheritanceDemo6.java -

public class InheritanceDemo6 {
    public static void main(String[] args) {
        Dog dog = new Dog("Rohu");
        System.out.println("My name is: "   dog.getName());
        dog.eat();
    }
}

Animal.java -

public class Animal {
    protected String name;    
    
    Animal(String name) {
        this.name = name;
    }

    public void eat() {
        System.out.println("I am eating...");
    }

    public String getName() {
        return name;
    }
}

Dog.java -

public class Dog extends Animal {
    Dog(String name) {
        super(name);
    }
}

Here, getting some strange errors -

In class Dog.java getting the error -

Animal cannot be resolved to a typeJava(16777218)

In class InheritanceDemo6.java getting the error -

Dog cannot be resolved to a typeJava(16777218)

On doing a ls on terminal, all the three java files are in the same directory.

Interestingly here compilation fails even when doing it independently in terminal. Here is the error -

payel@payel-Lenovo-ideapad-330-15IKB:~/VisualStudioCode/John_Purcell_Java_Basics$ java InheritanceDemo6.java
InheritanceDemo6.java:3: error: cannot find symbol
        Dog dog = new Dog("Rohu");
        ^
  symbol:   class Dog
  location: class InheritanceDemo6
InheritanceDemo6.java:3: error: cannot find symbol
        Dog dog = new Dog("Rohu");
                      ^
  symbol:   class Dog
  location: class InheritanceDemo6
2 errors
error: compilation failed

Here, the error in VSCode is -

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
        The constructor Dog(String) is undefined
        The method getName() is undefined for the type Dog

        at InheritanceDemo6.main(InheritanceDemo6.java:5)

CodePudding user response:

I run your code and it executes very well with a few modification. Change inheritance_demo6 to Inheritance_demo6. But it is better to use InheritanceDemo6. After that, save all your code in the file InheritanceDemo6.java, and execute it with: java InheritanceDemo6.java (if you are using Java8 or above - I am using Java11). You will get the result:

My name is: Rohu
I am eating...

UPDATE 1:

In Java, the name of a class starts with a capital letter. Also it is better to user a camel case notation for composite names. Use MyClass instead of My_class for class name. And use myVariable instead of my_variable for variable name.

UPDATE 2: I run the code on Eclipse and it works perfectly.

CodePudding user response:

The problems seems to be the structuring of programs in VSCode. I was writing all the programs under same folder and for some reasons it was not working (although earlier on, similar programs were working fine).

I took the help of the following question on stack overflow -

How to make packages in java project in visual Studio Code

Then I created a separate package InheritanceDemo.

Then I created three files - App.java, Animal.java and Dog.java all under the same package - InheritanceDemo.

Now my program is running without any error.

CodePudding user response:

The problem lies within that the application entry point(Class containing main() method) resides at the VS-Code project/root level, which will not necessarily points VS-code to the correct Class-path. Open/expand VS-code explorer and look in the JAVA PROJECTS view, to see which classes are contained within the class-path.

A good way to start a new project is to use VS-code integrated project manager: open VS-code > ctrl shift p > "create java project" > choose an option with or without management tools (no build tools, maven, gradle, etc

ps: using no build tools will still use VS-Code to debug, compile & run your code.

  •  Tags:  
  • Related