0

I am getting a java.lang.RuntimeException:

java.util.MissingFormatArgumentException: Format specifier '%s' error in Java below is my stacktrace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transformToListClass': Invocation of init method failed; nested exception is java.lang.RuntimeException: java.util.MissingFormatArgumentException: Format specifier '%s'

Caused by: java.lang.RuntimeException: java.util.MissingFormatArgumentException: Format specifier '%s'

Caused by: java.util.MissingFormatArgumentException: Format specifier '%s'

I am passing the below JSON file:

{
"EcrionIntegration": {
  "HelloWord": "Hello World"
}
}

To the following method:

@PostConstruct
public ResponseEntity<InputStreamResource> tranform() {
    try {

        JSONParser parser = new JSONParser();
//Use JSONObject for simple JSON and JSONArray for array of JSON.
        JSONObject data = (JSONObject) parser.parse(new FileReader("C:\\Liver\\code\\HELLO_WORLD.json"));//path to the JSON file.

        String payLoad = data.toString();


        ImageDescriptor descriptor = ecrionService.generateImage(payLoad, "HELLO_WORLD");

        log.info(String.format("%s generateImage Result: [%s] ", descriptor.getFileName(), descriptor.getFileUrl()));
        System.out.print(descriptor.getFileName() + " " + descriptor.getFileUrl());

        String fileName = "ecrionlList.PDF";
        log.info(String.format("file name:", fileName));
        System.out.print("file name:" + fileName);
        descriptor.setFileName(fileName);

        InputStreamResource streamResource = new InputStreamResource(descriptor.getInputStream());
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(MediaType.APPLICATION_PDF_VALUE))
                .body(streamResource);

    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

}

Which is in turn passed to the following method and send to the server:

public ImageDescriptor generateImage(String payLoad, String templateName) {
    try {
        ImageDescriptor descriptor = new ImageDescriptor();

        String myEcrionUrl = "http://localhost:8013/v1/ecrion";
      String ecrionURL = myEcrionUrl.concat(Constant.F_SLASH).concat(templateName);



        log.info("payload" + payLoad);

        ResponseEntity<Resource> responseEntity = restTemplate.exchange(
                ecrionURL,
                HttpMethod.POST,
                ncbiService.getStringHttpEntityWithPayload(payLoad),
                Resource.class);
      log.info(String.format("%s generateImage Result: [%s] ", responseEntity.getBody().getInputStream()));
        descriptor.setInputStream(Objects.requireNonNull(responseEntity.getBody()).getInputStream());

        convert(responseEntity.getBody().getInputStream(), "sherrr.pdf");

        log.info("file is:"+ convert(responseEntity.getBody().getInputStream(), "sherrr.pdf"));

        return descriptor;
    } catch (IOException e) {
        e.printStackTrace();
        log.error(" generate image failed " + e.getMessage());
        throw new RuntimeException(e);
    }




 }
san
  • 263
  • 2
  • 7
  • 14

2 Answers2

2
log.info(String.format("%s generateImage Result: [%s] ", responseEntity.getBody().getInputStream()));

This format has two %s, but you only passed one argument.

Deadbeef
  • 1,499
  • 8
  • 20
  • Thanks....so should it be log.info(String.format("%s generateImage Result ", responseEntity.getBody().getInputStream())); or log.info(String.format("generateImage Result: [%s] ", responseEntity.getBody().getInputStream())); ? – san Aug 21 '20 at 05:07
  • Also what does %s mean, I am working on a code done by someone else :) and where am I passing just one argument? – san Aug 21 '20 at 05:34
  • whatever format you want to use. `%s` indicates where the string representation of the corresponding argument you pass is. If for example the argument is null, the message is either `null generateImage Result` or `generateImage Result: [null]` – Deadbeef Aug 22 '20 at 05:16
  • @san, If you think my answer has helped you, you can accept my answer as the best answer. – Deadbeef Aug 31 '20 at 09:01
0

As you can see by documentation String.format accepts Format string and vararg or parameters. If you look at the formatter string documentation you can see that after % sign there are a bunch of parameters which can be used to format the string. Particularly for the %s it's written the following

's', 'S'    general If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString().

So the error you were getting was because you were telling formatter string that 2 parameters will be formatted and you passed only one of it

log.info(String.format("%s generateImage Result: [%s] ", responseEntity.getBody().getInputStream()));

So you can just remove the first %s :

log.info(String.format("generateImage Result: [%s] ", responseEntity.getBody().getInputStream()));
Hakob Hakobyan
  • 1,111
  • 8
  • 15