Constructor undefined

From JavaErrors

Jump to: navigation, search

The error message

 The constructor Foo(Bar) is undefined

can happen for several reasons.


Missing constructor

The most straightforward reason to get this error is that there is no constructor for Foo that takes an argument Bar. For example:

WRONG

 public class Foo {
   public Foo() {
     // constructor stuff
   }
 }


RIGHT

 public class Foo {
   public Foo() {
     // constructor stuff
   }
   public Foo(Bar aBar) {
     // other constructor stuff
   }
 }

Bad import statement somewhere else

This error message can also be the result of a cascade of errors elsewhere.

If you have an incorrect import statement so that the Bar class is not properly resolved in Foo.java, then the constructor undefined message can show up elsewhere.

For example, if you have Baz.java:

 import a.b.c.Foo;
 import a.b.c.Bar;  // correct
 public class Baz {
   private Foo aFoo;
   public Baz() {
     aFoo = Foo(new Bar());
   }
 }

and an error in file Foo.java

 import a.b.d.Bar; // wrong!
 public class Foo {
   public Foo(Bar aBar) {
     // constructor stuff
   }
 }  

then you will get errors in Foo.java that Bar cannot be resolved to a type and an error in Baz.java that the constructor Foo(Bar) is undefined.