-1

Can someone help me find out why my code doesn't work? I litterally have no clue why it won't work. It says java.lang.InstantiationException and every post i search in the internet tells me that abstract classes cannot be instantiated, but this is not an abstract class or am i wrong?

import acm.program.ConsoleProgram;

public class ComplexNumber extends ConsoleProgram{

    double re;
    double im;
    double a1;
    double a2;
    double b1;
    double b2;

    public ComplexNumber(double real, double imaginary) {
        double re = real;
        double im = imaginary;
    }

public ComplexNumber(ComplexNumber cn) {
    double re = getReal();
    double im = getImaginary();
}

    private double getReal() {
        return re;
    }

    private double getImaginary() {
        return im;
    }

    public String toString() {
        return "" + re + " " + im + "*i";
    }

    private ComplexNumber add(ComplexNumber cn2) {
        a1 = re;
        a2 = cn2.getReal();
        b1 = im;
        b2 = cn2.getImaginary();
        return new ComplexNumber(a1+a2,b1+b2);
    }

    @Override
    public void run() {
        ComplexNumber cn1 = new ComplexNumber(1.0, 2.0);
        ComplexNumber cn2 = new ComplexNumber(3.0, 4.0);
        cn1.add(cn2).toString();
    }
}

I would really appreciate it, if somebody could help me out.

java.lang.InstantiationException: ComplexNumber Laden: ComplexNumber.class kann nicht instanziiert werden. 
  at java.lang.Class.newInstance(Unknown Source) 
  at sun.applet.AppletPanel.createApplet(Unknown Source) 
  at sun.applet.AppletPanel.runLoader(Unknown Source) 
  at sun.applet.AppletPanel.run(Unknown Source) 
  at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.NoSuchMethodException: ComplexNumber.<init>() 
  at java.lang.Class.getConstructor0(Unknown Source) ... 5 mor
azro
  • 53,056
  • 7
  • 34
  • 70
  • How are you trying to run it and what is the full error message? – khelwood Dec 01 '18 at 23:49
  • java.lang.InstantiationException: ComplexNumber Laden: ComplexNumber.class kann nicht instanziiert werden. at java.lang.Class.newInstance(Unknown Source) at sun.applet.AppletPanel.createApplet(Unknown Source) at sun.applet.AppletPanel.runLoader(Unknown Source) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.NoSuchMethodException: ComplexNumber.() at java.lang.Class.getConstructor0(Unknown Source) ... 5 more – dorian dorianthiago Dec 01 '18 at 23:56
  • What do you mean with, how am i trying to run it? Im new to Java and used to code ruby :/ – dorian dorianthiago Dec 01 '18 at 23:57

2 Answers2

1

The issue seems to be that you are using this class as an applet. Your browser (or whatever other applet framework your using) is trying to instantiate a ComplexNumber without passing any arguments to the constructor. But your ComplexNumber class does not have a suitable constructor - you've got one version with two double parameters and another with a ComplexNumber parameter. But you'll need one with no parameters at all. Maybe something like

public ComplexNumber() {
    re = 0.0;
    im = 0.0;
}

You should also make the fixes suggested in George Zougianos' answer - without them, your class is still broken.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
-1

Inside your constructor (both of them) you declare new variables which are local. This means, that the field called "re" does not get any value, so, the getter will try to return a non instantiated field.

Check this class, with the proper field/variable declaration (Some extra comments inside the code :) )

public class ComplexNumber extends ConsoleProgram {
    double re;
    double im;

    public ComplexNumber(double real, double imaginary) {
        re = real;
        im = imaginary;
        // double re = real will declare new local variable. So the field
        // this.re will never get a value.
    }

    public ComplexNumber(ComplexNumber cn) {
        // Alternative constructor will use the original constructor.
        this(cn.getReal(), cn.getImaginary());
    }

    private double getReal() {
        return re;
    }

    private double getImaginary() {
        return im;
    }

    public String toString() {
        return "" + re + " " + im + "*i";
    }

    private ComplexNumber add(ComplexNumber cn2) {
        // a1,a2,b1,b2 variables can be local, no need to be fields.
        double a1 = re;
        double a2 = cn2.getReal();
        double b1 = im;
        double b2 = cn2.getImaginary();
        return new ComplexNumber(a1 + a2, b1 + b2);
    }

    @Override
    public void run() {
        ComplexNumber cn1 = new ComplexNumber(1.0, 2.0);
        ComplexNumber cn2 = new ComplexNumber(3.0, 4.0);
        cn1.add(cn2).toString();
    }
}
George Z.
  • 6,643
  • 4
  • 27
  • 47
  • But how can i fix it? Do I really need to use fields? I never used them while coding in ruby and honestly I don't understand how they're supposed to work. In ruby you could just pass the arguments into the constructor and just return the attributes. How do I change the value of the fields for the object? – dorian dorianthiago Dec 02 '18 at 00:09
  • Fields are visible to the whole class (all methods of the class). Local variables are visible only to the method they are declared. These are Java very basic ( and not only) stuff and can be found in almost all tutorials/courses/examples online :) – George Z. Dec 02 '18 at 00:13