I have to assign values read from the file to the Model1 class and execute the run() method. I have a problem with assigning values to variables through reflection. I can not modify the Model1 class. Therefore the method does not want to work.
I try change Field fieldtkWI = clazz.getDeclaredField("twKI[]"); to Field fieldtkWI = clazz.getDeclaredField("twKI"); but then throws the error that can not assign to the variable "twKI" the value of double
public class Model1 {
private int LL; //
private double[] twKI;
private double[] twKS;
private double[] twINW;
private double[] twEKS;
private double[] twIMP;
private double[] KI;
private double[] KS;
private double[] INW;
private double[] EKS;
private double[] IMP;
private double[] PKB;
public Model1() {
}
public void run() {
PKB = new double[LL];
PKB[0] = (KI[0] + KS[0] + INW[0] + EKS[0]) - IMP[0];
for (int t=1; t < LL; t++) {
KI[t] = twKI[t]* KI[t-1];
KS[t] = twKS[t]* KS[t-1];
INW[t] = twINW[t]* INW[t-1];
EKS[t] = twEKS[t]* EKS[t-1];
IMP[t] = twIMP[t]* IMP[t-1];
PKB[t] = KI[t] + KS[t] + INW[t] + EKS[t] - IMP[t];
}
}
public class Controller {
private int LL;
private double[] twKI;
private double[] twKS;
private double[] twINW;
private double[] twEKS;
private double[] twIMP;
private double[] KI;
private double[] KS;
private double[] INW;
private double[] EKS;
private double[] IMP;
private double[] PKB;
private StringTokenizer st;
private String years;
Model1 model = new Model1();
Class class1;
Class<Model1> clazz = Model1.class;
public Controller(String modelName) {
try {
class1 = Class.forName("zad1.models."+modelName);
} catch (Exception e) {
e.printStackTrace();
}
}
public void readDataFrom(String fname) {
try (BufferedReader br = new BufferedReader(new FileReader(fname))) {
String text = br.readLine();
years= text;
st = new StringTokenizer(text);
LL = st.countTokens() - 1;
twKI = new double[LL];
twKS = new double[LL];
twINW = new double[LL];
twEKS = new double[LL];
twIMP = new double[LL];
KI = new double[LL];
KS = new double[LL];
INW = new double[LL];
EKS = new double[LL];
IMP = new double[LL];
PKB = new double[LL];
text = br.readLine();
String split[] = text.split("\t");
text = br.readLine();
String split2[] = text.split("\t");
text = br.readLine();
String split3[] = text.split("\t");
text = br.readLine();
String split4[] = text.split("\t");
text = br.readLine();
String split5[] = text.split("\t");
int i = 0;
while (i < LL) {
twKI[i] = Double.parseDouble(split[1]);
twKS[i] = Double.parseDouble(split2[1]);
twINW[i] = Double.parseDouble(split3[1]);
twEKS[i] = Double.parseDouble(split4[1]);
twIMP[i] = Double.parseDouble(split5[1]);
i++;
}
text = br.readLine();
String split6[] = text.split("\t");
KI[0] = Double.parseDouble(split6[1]);
text = br.readLine();
String split7[] = text.split("\t");
KS[0] = Double.parseDouble(split7[1]);
text = br.readLine();
String split8[] = text.split("\t");
INW[0] = Double.parseDouble(split8[1]);
text = br.readLine();
String split9[] = text.split("\t");
EKS[0] = Double.parseDouble(split9[1]);
text = br.readLine();
String split10[] = text.split("\t");
IMP[0] = Double.parseDouble(split10[1]);
try {
Field fieldLL = clazz.getDeclaredField("LL");
fieldLL.setAccessible(true);
fieldLL.setInt(model, LL);
Field fieldtkWI = clazz.getDeclaredField("twKI[]");
fieldtkWI.setAccessible(true);
fieldtkWI.setDouble(model, twKI[0]);
Field fieldtwKS = clazz.getDeclaredField("twKS[]");
fieldtwKS.setAccessible(true);
fieldtwKS.setDouble(model, twKS[0]);
Field fieldtwINW = clazz.getDeclaredField("twINW[]");
fieldtwINW.setAccessible(true);
fieldtwINW.setDouble(model, twINW[0]);
Field fieldtwEKS = clazz.getDeclaredField("twEKS[]");
fieldtwEKS.setAccessible(true);
fieldtwEKS.setDouble(model, twEKS[0]);
Field fieldtwIMP = clazz.getDeclaredField("twIMP[]");
fieldtwIMP.setAccessible(true);
fieldtwIMP.setDouble(model, twKS[0]);
Field fieldtKI = clazz.getDeclaredField("KI[]");
fieldtKI.setAccessible(true);
fieldtKI.setDouble(model, KI[0]);
Field fieldKS = clazz.getDeclaredField("KS[]");
fieldKS.setAccessible(true);
fieldKS.setDouble(model, KS[0]);
Field fieldINW = clazz.getDeclaredField("INW[]");
fieldINW.setAccessible(true);
fieldINW.setDouble(model, INW[0]);
Field fieldEKS = clazz.getDeclaredField("EKS[]");
fieldEKS.setAccessible(true);
fieldEKS.setDouble(model, EKS[0]);
Field fieldIMP = clazz.getDeclaredField("IMP[]");
fieldIMP.setAccessible(true);
fieldIMP.setDouble(model, IMP[0]);
/*fieldPKB = clazz.getDeclaredField("PKB");
fieldPKB.setAccessible(true);
fieldPKB.set(model, null);*/
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void runModel() {
try {
Method method = class1.getDeclaredMethod("run");
Object o = method.invoke(class1.newInstance());
//method.setAccessible(true);
int i = 1;
while(i<LL) {
Field fieldKI = clazz.getDeclaredField("KI["+i+"]");
KI[i] = Double.parseDouble(String.valueOf(fieldKI));
Field fieldKS = clazz.getDeclaredField("KS["+i+"]");
KS[i] =Double.parseDouble(String.valueOf(fieldKS));
Field fieldINW = clazz.getDeclaredField("INW["+i+"]");
INW[i] = Double.parseDouble(String.valueOf(fieldINW));
Field fieldEKS = clazz.getDeclaredField("EKS["+i+"]");
EKS[i] = Double.parseDouble((String.valueOf(fieldEKS)));
Field fieldIMP = clazz.getDeclaredField("IMP["+i+"]");
IMP[i] = Double.parseDouble(String.valueOf(fieldIMP));
i++;
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
public String getResultsAsTsv() {
return years+"\n"+"tkWI"+ " "+ Arrays.toString(twKI)+"\n"+"twKS"+" "+ Arrays.toString(twKS)+"\n"+"twINW"+" " + Arrays.toString(twINW)
+"\n"+"twEKS"+" " + Arrays.toString(twEKS)+"\n"+"twIMP"+" "+ Arrays.toString(twIMP)+"\n"+"KI"+" " + Arrays.toString(KI)+"\n"+"KS"
+" "+ Arrays.toString(KS)+"\n"+"INW"+" " + Arrays.toString(INW)+"\n"+"EKS"+" "+ Arrays.toString(EKS)+"\n"+"IMP"+" "
+Arrays.toString(IMP)+"\n"+"PKB"+" "+Arrays.toString(PKB);
}
}
This is the result now
java.lang.NoSuchFieldException: twKI[]
at java.base/java.lang.Class.getDeclaredField(Class.java:2370)
at zad1.Controller.readDataFrom(Controller.java:104)
at zad1.Main.main(Main.java:15)
java.lang.reflect.InvocationTargetException
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
at
java.base/
jdk.internal.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:62)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at zad1.Controller.runModel(Controller.java:167)
at zad1.Main.main(Main.java:16)
Caused by: java.lang.NullPointerException
at zad1.models.Model1.run(Model1.java:33)
... 6 more
LATA 2015 2016 2017 2018 2019
tkWI [1.03, 1.03, 1.03, 1.03, 1.03]
twKS [1.04, 1.04, 1.04, 1.04, 1.04]
twINW [1.12, 1.12, 1.12, 1.12, 1.12]
twEKS [1.13, 1.13, 1.13, 1.13, 1.13]
twIMP [1.14, 1.14, 1.14, 1.14, 1.14]
KI [1023752.2, 0.0, 0.0, 0.0, 0.0]
KS [315397.0, 0.0, 0.0, 0.0, 0.0]
INW [348358.0, 0.0, 0.0, 0.0, 0.0]
EKS [811108.6, 0.0, 0.0, 0.0, 0.0]
IMP [784342.4, 0.0, 0.0, 0.0, 0.0]
PKB [0.0, 0.0, 0.0, 0.0, 0.0]
It should be like this
LATA 2015 2016 2017 2018 2019
tkWI [1.03, 1.03, 1.03, 1.03, 1.03]
twKS [1.04, 1.04, 1.04, 1.04, 1.04]
twINW [1.12, 1.12, 1.12, 1.12, 1.12]
twEKS [1.13, 1.13, 1.13, 1.13, 1.13]
twIMP [1.14, 1.14, 1.14, 1.14, 1.14]
KI [1023752.2, 1074939.81, 1150185.5967, 1230698.5884690003,
1316847.4896618305]
KS [315397.0, 328012.88, 341133.3952, 354778.73100800003,
368969.88024832006]
INW [348358.0, 390160.96, 436980.2752000001, 489417.90822400013,
548148.0572108802]
EKS [811108.6, 916552.7179999999, 1035704.5713399998,
1170346.1656141996, 1322491.1671440455]
IMP [784342.4, 894150.3359999999, 1019331.3830399998,
1162037.7766655996, 1324723.0653987834]
PKB [1714273.4, 1815516.032, 1944672.4554000003, 2083203.6166496002,
2231733.528866293]
Aucun commentaire:
Enregistrer un commentaire