Invocationtargetexception in dynamic method

Asked

Viewed 43 times

0

I’m in a very strange situation.

I get it from my Bean the following arguments:

#{segurancaBean.callSubAcaoMethod(subAcao, moduloBean.class.name, modulo.id)}

This method is dynamic:

public void callSubAcaoMethod(Acao acao, String bean, Long id){
    try {
        Class<?> clazz = Class.forName(bean);
        Method method = clazz.getMethod(acao.getTipoAcao().getNmBean(), Long.class);
        Object obj = clazz.newInstance();
        Object ins = new Object();
        ins = method.invoke(obj, id);

    } catch (ClassNotFoundException | SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
        Logger.getLogger(SegurancaBean.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Of this method I will receive:
Name of my generic method
Name of my class for dynamic instance
ID of the object that will come from view

And below is the method that will be used:

@Override
public void btnAlterar(Long id){
    Modulo m = new Modulo();
    m = moduloServico.pesquisar(id);
}

Now comes the weird part.

Whether in this my method or put one System.out.println("alguma mensagem") or Modulo m = new Modulo(); or print on screen the ID I receive parameter works normally! Now when I go to do any operation related to instantiating an object, I have the error:

java.lang.reflect.Invocationtargetexception

The line m = moduloServico.pesquisar(id); is an example that generates this error.

In detail, I put in mine view also a direct button that accesses the method without going through the callSubAcaoMethod and works normal! I can instantiate and do whatever you want, IE, would not be problem of my service.

1 answer

0

Solved.

Being using CDI, I did the "newInstance" by this method:

Object obj = CDI.current().select(clazz).get();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.