Data pass dispatcherServlet

Asked

Viewed 41 times

-2

I am trying to perform data export from Log to Excel only it is presenting me the following error:

2019-03-18 10:16:16.022 ERROR 252 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/mv-fatur] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

The implementation is being done as follows:

Logacessoutil.java

@Override
@SuppressWarnings("unchecked")
public List<LogAcessoDTO> getLogAcessoExcel(String cdCliente) {

    LocalDateTime agora = LocalDateTime.now();
    DateTimeFormatter formatterData = DateTimeFormatter.ofPattern("dd/MM/uuuu");
    String dataFormatada = formatterData.format(agora);

    StringBuilder sql = new StringBuilder();
    sql.append("select * from cli");

    List<LogAcessoDTO> returnQuery = em.createNativeQuery(sql.toString(), "LogAcessoDTOMapping").getResultList();

    return (List<LogAcessoDTO>) returnQuery;
}

Globalcontroller.java

@Autowired
private LogAcessoUtil logAcessoUtil;

@Autowired
private ExportFileService<LogAcessoDTO> exportFileServiceDetalhe;

@GetMapping("/getlogacesso/excel")
    public ResponseEntity<List<LogAcessoDTO>> getLogAcessoExcel(@RequestParam String cdCliente, HttpServletResponse response) {
        try {
            List<LogAcessoDTO> dtos = logAcessoUtil.getLogAcessoExcel(cdCliente);

            exportFileServiceDetalhe.exportExcelFile(dtos, response, "LogAcesso", "", "Cod. Cliente: " + cdCliente.toString());

            return new ResponseEntity<>(HttpStatus.OK);
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException | IOException ex) {
            log.error(ex.toString());
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
  • Ikaro, you just posted a line of stacktrace, would you kindly post the full stacktrace? The more details about the problem the better, and the complete stack and one of the most important points to find the problem.

  • Please also post the other classes involved, such as the LogAcessoUtil which is probably presenting the problem. And also the way it is by injecting the properties

1 answer

1


The data you provided is not enough to be sure about the issue, but the exception launched (NullPointerException) indicates that a method is being invoked (called) by a null object or attribute.

Make sure you have (new NomeDaClasse()) or noted (with @Autowired, for example) variables/attributes logAcessoUtil and exportFileServiceDetalhe correctly.

The parameters cdCliente and response should not be null either.

  • Yes, they are annotated with @Autowired.

  • As it is being ordered via GET (in brownser) I am passing cdCliente, but Httpservletresponse I am not passing, I believe this is it. But the method that is asking for this parameter

  • Possibly is the parameter response which is null and generates the exception. The method exportExcelFile is asking for a parameter of type HttpServletResponse? Or the method getLogAcessoExcel is implementing the signing of an interface?

  • 1

    To make sure the attribute response is null, just add System.out.println(response); (debug manual) before the invocation line exportFileServiceDetalhe.exportExcelFile(...).

  • "Possibly it is the Response parameter that is null and generates the exception." - There are other methods doing the implementation this way and not generating Exception.

  • "The exportExcelFile method asks for a parameter of type Httpservletresponse?" -SIM

  • "Or is the getLogAcessoExcel method implementing an interface signature?" - This method is responsible for going into the database to bring the data and return a List that will be passed to the exportExcelFile method

  • RESPONSE variable returned: org.springframework.security.web.header.Headerwriterfilter$Headerwriterresponse@76188ab8

  • So response is not null. AND dtos, you have already checked if it is null?

  • yes, I’ve checked and it’s not null

  • I actually saw all the parameters and none is null

  • Jeez! So logAcessoUtil.getLogAcessoExcel(cdCliente) or exportFileServiceDetalhe.exportExcelFile(...) is pitching NullPointerException. Circumvent each section in which these two methods called with try { ... } catch (NullPointerException e) { System.out.println("nomeDoMetodo"); } and see what happens.

  • I found the null is coming from this property: String SAMPLE_PERSON_DATA_FILE_NAME = properties.getProperty(fileName + ".url");

  • The scolding is, I made a sysout of Properties properties and returns me several values that do not know where they were added

Show 9 more comments

Browser other questions tagged

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