3
I have the following error, when accessing a dao class of a test class:
java.lang.Nullpointerexception
This is the controller method:
@Post("/consultar_lancamento/{codLancamento}")
public int consultarLancamento(int codLancamento) {
try {
Lancamento lancamento = lancamentoDao.carregaPorId(codLancamento);
result.use(json()).withoutRoot().from(lancamento).serialize();
contaLinhasDoLancamento(lancamento);
} catch (Exception err) {
Lancamento lancamento = new Lancamento();
lancamento.setDescricaoLancamento(null);
result.use(json()).withoutRoot().from(lancamento).serialize();
return 0;
}
return codLancamento;
}
This is the test class method:
@Test
public void testConsultarLancamento() {
EditarLancamentoController instanciaEditarLancamentoController = new EditarLancamentoController();
int resultadoDaConsultaUm = instanciaEditarLancamentoController.consultarLancamento(4);
int resultadoDaConsultaNulo = instanciaEditarLancamentoController.consultarLancamento(0);
assertEquals(4, resultadoDaConsultaUm);
assertEquals(0, resultadoDaConsultaNulo);
}
Note: I am using Dependency Injection. The error occurs at the time of accessing the DAO method, I checked the design to see if it was the injection, and found no configuration problems.
Error description:
Testsuite: br.com.uprise.controller.EditarLancamentoControllerTest
Tests run: 3, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1,125 sec
Testcase: testConsultarLancamento(br.com.uprise.controller.EditarLancamentoControllerTest): Caused an ERROR
null
java.lang.NullPointerException
at br.com.uprise.controller.EditarLancamentoController.consultarLancamento(EditarLancamentoController.java:106)
at br.com.uprise.controller.EditarLancamentoControllerTest.testConsultarLancamento(EditarLancamentoControllerTest.java:52)
Test br.com.uprise.controller.EditarLancamentoControllerTest FAILED
Adds the full error stack.
– user28595
@diegofm, added the description
– Ivan Alves
Where do you use codLankmentReceived within query Shouldn’t be codLanking? I think the problem is this...
– Bruno Bermann
Correct variable, but nothing has changed.
– Ivan Alves
Are you using spring for dependency injection? Do you use JPA in your daos? If you use jpa, the connection in the database is in the container or persistence.xml?
– Giuliana Bezerra
@Giulianabezerra I’m using the container peak and use JPA in the daos. my connection is in persistence
– Ivan Alves
Actually, it’s the variable
lancamentoDao
is null? You initialized this variable before calling the methodconsultarLancamento
by testing?– Marcos Tanaka