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
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 :) )