mardi 4 avril 2017

"NoSuchMethodException" error in Java

I'm trying the following code to get some parameters values from an excel sheet:

Class c = getClass();
String sheet = "params";
int colnames = 1;
int colvalues = 1;
while(!test.getCellStringValue(sheet, 1, colnames).equals("name"))
    colnames++;
while(!test.getCellStringValue(sheet, 1, colvalues).equals("value"))
    colvalues++;
int row = 2;
while(test.cellExists(sheet, row, colnames)){
    String name = test.getCellStringValue(sheet, row, colnames);
    try{
        java.lang.reflect.Field f = c.getField(name);
        java.lang.reflect.Method m;
        String methodname = "set_" + name;
        Class<?>fc = f.getType();
        if (fc.equals(double.class)){
            m = c.getDeclaredMethod(methodname, int.class);
            m.invoke (this, test.getCellNumericValue(sheet, row, colvalues));
        }else if(fc.equals(String.class)){
            m = c.getDeclaredMethod(methodname, String.class);
            m.invoke(this, test.getCellStringValue(sheet, row, colvalues));
        }else if(fc.equals(boolean.class)){
            m = c.getDeclaredMethod(methodname, boolean.class);
            m.invoke(this, test.getCellBooleanValue(sheet, row, colvalues));
        }else{
            error("This parameter type cannot be read from Excel:"+fc);
        }
    }catch (Exception e){
        error("Could not setup the parameter: "+name +" because of:\n" +e);
    }
    row++;
}

but when running it, I get the following error:

Error during model startup:
root:
Could not setup the parameter: ins1 because of:
java.lang.NoSuchMethodException: population_growth.Main.setins1(int)
java.lang.RuntimeException: root:
Could not setup the parameter: ins1 because of:
java.lang.NoSuchMethodException: population_growth.Main.setins1(int)
    at com.anylogic.engine.Engine.error(Unknown Source)
    at com.anylogic.engine.Agent.error(Unknown Source)
    at com.anylogic.engine.Utilities.error(Unknown Source)
    at population_growth.Main.onStartup(Main.java:1519)
    at com.anylogic.engine.Agent.start(Unknown Source)
    at com.anylogic.engine.Engine.start(Unknown Source)
    at com.anylogic.engine.ExperimentSimulation.a(Unknown Source)
    at com.anylogic.engine.ExperimentSimulation.run(Unknown Source)
    at population_growth.Simulation.executeShapeControlAction(Simulation.java:116)

I think the problem may bein the "set_" method, but how can I change it?





Aucun commentaire:

Enregistrer un commentaire