non static method can not be referenced from a static context [duplicated]

1

I have a problem calling a method.

public class propietario{
public static void modificarCoeficiente (HttpServletRequest request, HttpServletResponse response, BaseWeb base) {
    HttpSession session = request.getSession();

    ParticipantePOJO vParticipante = (ParticipantePOJO)session.getAttribute("Participante");
    String coef = request.getParameter("Coeficiente");
    double coeficiente = Double.parseDouble(coef);

    try{
        int IdParticipante = vParticipante.getIdParticipante();
        ModificarPreciosControlador ModificarPreciosControlador = new ModificarPreciosControlador();    
        ModificarPreciosControlador.modCoeficiente(IdParticipante, coeficiente);
    }catch(Exception ex){
        base.getLoggerGarageScanner().error("Se ha producido un error");
        request.setAttribute("errorpanelcontrol", "propietariocomunidaderrorcarga");
    }
    finally{

    }
}
}

And I'm trying to call this method.

public class ModificarPreciosControlador extends BaseControlador{

    public void modCoeficiente (int pIdParticipante, double coeficiente) throws Exception{
    ListaParametrosEntrada lstParametros = new ListaParametrosEntrada();
    lstParametros.addParametroEntrada(java.sql.Types.INTEGER, pIdParticipante);
    lstParametros.addParametroEntrada(java.sql.Types.INTEGER, coeficiente);

    super.lanzarProcedimientOut("spUpdateCoeficiente", lstParametros);

    //return null;
    }

}

But I get the following error:

non static method cannot be referenced from a static context java. 

Any solution?

    
asked by urrutias 03.04.2017 в 10:15
source

1 answer

1

You must create an instance of Modify Price Control to access its non-static methods.

So, in your code, instead of doing

ModificarPreciosControlador.modCoeficiente(IdPropietario, IdComunidad, coeficiente);

you should do:

ModificarPreciosControlador controlador = new ModificarPreciosControlador();    
controlador.modCoeficiente(IdPropietario, IdComunidad, coeficiente);

If you want to keep all methods static, the easiest way to do it is to change the

 public void modCoeficiente (int pIdParticipante, double coeficiente) throws Exception

a

public static void modCoeficiente (int pIdParticipante, double coeficiente) throws Exception

And in modificarCoeficiente call the method statically:

ModificarPreciosControlador.modCoeficiente(IdParticipante, coeficiente);

*********** solution *************

ModificarPreciosControlador modContr = new ModificarPreciosControlador(base.getVariablesGlobales());// incluir esta linea de codigo en metodo modificarCoeficiente

modContr.modCoeficiente(IdParticipante, coeficiente);// y cambiar esta llamada al otro metodo por la antigua.



public  void modCoeficiente (int pIdParticipante, double coeficiente) throws Exception{}//Cambiar el tipo del metodo

And now, thank you very much

    
answered by 03.04.2017 / 10:29
source