Pergunta

i develop a little playframework project and use jpa in a inner block like this:

private void sendMailToTheRegulator(final Machine mc, String rea) {
    List<LogProject> pros = null;
    try {
        pros = JPA.withTransaction(new Function0<List<LogProject>>() {
            @Override
            public List<LogProject> apply() throws Throwable {
                List<ServerModel> ss = JPA.em()
                        .createQuery("from ServerModel where machineId=")
                        .setParameter(1, mc.getId()).getResultList();
...
}

the mc does not exist in apply() so i cannot access its id. when i execute the codes it throws that the mc.getId() is null.
what should i do to fix it?

Foi útil?

Solução

mc can be safely used in the anonymous inner class instance of Function0 that you have created. The null error for mc.getId() will only come if mc is null. If getId() returns null then null will be passed as value to the setParameter method. But if mc is null then you are actually trying to call a method getId on a null object which is not correct.

So if yo getting null in this code, check at the place from where you are calling sendMailToTheRegulator method. Most probably the mc parameter being passed in null.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top