15

In Java, Why am I getting this error:

Error: The constructor WeightIn() is undefined

Java Code:

public class WeightIn{
  private double weight;
  private double height;

  public WeightIn (double weightIn, double heightIn){
    weight = weightIn;
    height = heightIn;
  }
  public void setWeight(double weightIn){
    weight = weightIn;
  }
  public void setHeight(double heightIn){
    height = heightIn;
  }
}

public class WeightInApp{
  public static void main (String [] args){
    WeightIn weight1 = new WeightIn();         //Error happens here.
    weight1.setWeight(3.65);
    weight2.setHeight(1.7);
  }
}

I have a constructor defined.

3
  • 9
    The compiler is right. The constructor WeightIn() is not defined. Commented Aug 10, 2013 at 5:58
  • 1
    can you correct the code and then past a copy so i can understand it please Commented Aug 10, 2013 at 6:24
  • Scrit still doesnt work Commented Aug 10, 2013 at 6:34

8 Answers 8

25

Add this to your class:

public WeightIn(){
}
  • Please understand that default no-argument constructor is provided only if no other constructor is written
  • If you write any constructor, then compiler does not provided default no-arg constructor. You have to specify one.
Sign up to request clarification or add additional context in comments.

Comments

7

With your current implementation, you can't do WeightIn weight1 = new WeightIn(); since default constructor is not defined.

So you can add

public WeightIn(){
}

Or you can do this

WeightIn weight1 = new WeightIn(3.65,1.7) // constructor accept two double values

Comments

3

The compiler is encountering a call to a "WeightIn()" no argument constructor, on this line:

WeightIn weight1 = new WeightIn();         //Error happens here.

The compiler is looking for a matching constructor in the class definition, and its not finding it. That's the error. (You do have a constructor defined: "WeightIn(double,double)" but that takes two arguments, and is not match.)

Several ways to fix this.

The easiest is to change the code in your main method to pass two arguments.

WeightIn weight1 = new WeightIn( 3.65, 1.7); 
//weight1.setWeight(3.65);
//weight2.setHeight(1.7);

The calls to the setWeight and setHeight methods are redundant, since the members are already assigned values by the constructor method.

3 Comments

+1 This is the only answer that actually clears up the cause of the error: it's not in the definition of the constructor, it's in the calling of a constructor that doesn't match any of the defined constructor signatures. This insight shifts the focus of the question from "did I not write my class constructor properly" to "did I not call my class constructor properly" which makes a difference to a n00b (like me)'s understanding of the source of the problem. All the other answers seem to imply that you must write a parameterless constructor before you overload it, which is not the case.
@TomAuger : you are absolutely correct. there is no requirement to provide a no argument constructor. we may want to do that, but its not necessary. (you are right that some of the answers misleadingly use the phrase "you need to" and provide only one solution, as if that is the only solution. (whenever i read an answer that says "you need to" I'm always questioning that, asking is that really true? does one really need to, or are there other, possibly better, alternatives?
@TomAuger: you are also correct that many of the answers here skip over the question that was asked "Why am I getting this error?" and attempt to answer other questions.
1

You do not have the constructor WeightIn() .Create it or give parameters in main method to constructor.

Comments

1
WeightIn weight1 = new WeightIn();  

The default constructor is not defined. Please define it like this:-

public weightIn()
    {
    }

Comments

1
In Java, Why am I getting this error:

    Error: The constructor WeightIn() is undefined

It's simply because you didn't have the matching constructor for your class:

public class WeightIn {
  ...

  public WeightIn (double weightIn, double heightIn){
    weight = weightIn;
    height = heightIn;
  }

  ...
}

you need to add the public WeightIn() {}.

In Java, the default constructor is automatically generated by the compiler if you didn't defined it. So, when you define the following class:

public class WeightIn {
  private double weight;
  private double height;

  // didn't define a constructor.
  public void setWeight(double weightIn){
    weight = weightIn;
  }
  public void setHeight(double heightIn){
    height = heightIn;
  }
}

compiler will generating a matching default constructor for you. So, your class is implicitly having a default constructor like this:

public class WeightIn {
  private double weight;
  private double height;

  // automatically generated by compiler
  public WeightIn() {
    // do nothing here.
  }

  // didn't define a constructor.
  public void setWeight(double weightIn){
    weight = weightIn;
  }
  public void setHeight(double heightIn){
    height = heightIn;
  }
}

when you instantiate the class with:

WeightIn weight = new WeightIn(); 

everything is alright.

But when you're adding a constructor by yourself, the default constructor will not generated by the compiler. So, when you're creating the class with:

public class WeightIn {
  ...

  // this won't automatically generated by compiler
  // public WeightIn() {
  //   nothing to do here.
  //}

  public WeightIn (double weightIn, double heightIn){
    weight = weightIn;
    height = heightIn;
  }

  ...
}

You won't have the default constructor (i.e public WeightIn(){}). And using the following will raise an error because you have no matching constructor:

 WeightIn weight = new WeightIn();

Comments

0

First of all, you should know that one .java file can have only one public class.

You are getting error because you have written parameterised constructor and accessing a default constructor. To fix this error write:

WeightIn weight1 = new WeightIn(5.2, 52.2); 

instead of

WeightIn weight1 = new WeightIn();

Comments

-1

It took me a while but I think I finally figured out a way for this program to work. I separated the classes into different files and renamed the weight Class to Record just so that it's more obvious instead of everything being named some variation of weight. I also added an Output class to keep the code in the main as clean and minimal as possible. I hope this is similar to what you were hoping to do.
Original Code: "Constructor"

public class WeightIn{
  private double weight;
  private double height;

  public WeightIn (double weightIn, double heightIn){
    weight = weightIn; //needs to be : this.weight = weight
    height = heightIn; //needs to be : this.height = height
  }


Project: weightInApp
Package: weightInApp
Class: Record.Java

package weightInApp;

        class Record 
        {
            private double weight; //declare variables
            private double height;
        Record (double weight, double height) {  //Parameterized Constructor method
                    this.weight = weight;  //this is the correct way 
                    this.height = height;
    //if you want to use weightIn and/or heightIn you have to be consistent acrosss 
    //the whole class. I decided to drop "In" off the variables for brevity.
                }
                //Getters and setters
                public double getWeight() { 
                    return weight;
                }
                public void setWeight(double weight) {
                    this.weight = weight;
                }
                public double getHeight() {
                    return height;
                }
                public void setHeight(double height) {
                    this.height = height;
                }
        }


Project: weightInApp
Package: weightInApp
Class: Output.Java
This class outputs the set values to a table on the console. This can be manually altered to add more data. you could also consider tracking the date of the record and adding that to this output. you would need to add the requisite variables, getters, and setters in the Record Class for that functionality.

package weightInApp;

public class Output {
    static void output (Record weightIn1, Record weightIn2)
    {
        int count = 0;
        final Object[][] table = new Object[3][3];
        table[0] = new Object[] { "Record", "Weight", "Height"};
        table[1] = new Object[] { count +=1, weightIn1.getWeight(), weightIn1.getHeight() };
        table[2] = new Object[] { count+=1, weightIn2.getWeight(), weightIn2.getHeight() };
        for (final Object[] row : table) {
            System.out.format("%15s%15s%15s\n", row);
        }

}
}


Project: weightInApp
Package: weightInApp
Class: Main.Java

package weightInApp;
import weightInApp.Record; //imports methods and variables from the other classes
import weightInApp.Output;
public class  Main {

    public static void main (String [] args){  
        Record weightIn1 = new Record(0,0);  
//previous line of code creates a new Record object named weightIn1 
//previous line of code also sets both the values of weight and height to 0.
        weightIn1.setWeight(3.65); //sets weight for weightIn1
        weightIn1.setHeight(1.70);//sets Height for WeightIn1
        Record weightIn2 = new Record(0, 0); 
        weightIn2.setWeight(3.00);
        weightIn2.setHeight(1.75);
 //previous 3 lines are the same as above for weightIn1 
//except this is for a second object named weightIn2
        Output.output(weightIn1, weightIn2); //calls runs passes values to output method
}
}

Sample Output

output sample from console

1 Comment

Or just add a default constructor?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.