Home > Back-end >  java read data from json and save it in ms sql database - error argument "src" is null
java read data from json and save it in ms sql database - error argument "src" is null

Time:10-07

I want to read data from json file and save it to ms sql data base This is my application.java class. The error is in this file on the line before for loop

@SpringBootApplication
public class Covid19Application {

    public static void main(String[] args) {
        SpringApplication.run(Covid19Application.class, args);
    }

    @Bean
    CommandLineRunner runner(SaveDataFromJsonToDBService saveDataFromJsonToDBService) {
        return args -> {
            ObjectMapper mapper = new ObjectMapper();
            TypeReference<List<CovidStatistics>> typeReference = new TypeReference<List<CovidStatistics>>(){};
            InputStream inputStream = TypeReference.class.getResourceAsStream("/CovidStatistics.json");
            try {
                List<CovidStatistics> covidStatistics = mapper.readValue(inputStream,typeReference);
                for (CovidStatistics cs : covidStatistics) {
                    saveDataFromJsonToDBService.save(cs);
                }
                //saveDataFromJsonToDBService.save(covidStatistics);
                System.out.println("Users Saved!");
            } catch (IOException e){
                System.out.println("Unable to save users: "   e.getMessage());
            }
        };
    }

}

And this is the error. I dont know where is the problem, I try to google it, but I dont found a solution

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:774) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:755) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.4.jar:2.7.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.4.jar:2.7.4]
    at com.example.Covid19.Covid19Application.main(Covid19Application.java:18) ~[classes/:na]
Caused by: java.lang.IllegalArgumentException: argument "src" is null
    at com.fasterxml.jackson.databind.ObjectMapper._assertNotNull(ObjectMapper.java:4737) ~[jackson-databind-2.11.4.jar:2.11.4]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3512) ~[jackson-databind-2.11.4.jar:2.11.4]
    at com.example.Covid19.Covid19Application.lambda$runner$0(Covid19Application.java:28) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:771) ~[spring-boot-2.7.4.jar:2.7.4]
    ... 5 common frames omitted

2022-10-06 20:42:51.780  INFO 30652 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2022-10-06 20:42:51.782  INFO 30652 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-10-06 20:42:51.788  INFO 30652 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Process finished with exit code 1

CodePudding user response:

As you want to read from the classpath root you must use, It seems the inputstream is null so its not finding the file.

InputStream inputStream = TypeReference.class.getClassLoader().getResourceAsStream("/CovidStatistics.json");

TypeReference.class.getResourceAsStream will look in the directory where the class file is.

CodePudding user response:

First wrap your InputStream into a try with resources. Next print all errors with catching exception. Also make sure that the file you are referring to can be found.

  CommandLineRunner runner(SaveDataFromJsonToDBService saveDataFromJsonToDBService) {
      return args -> {
          ObjectMapper mapper = new ObjectMapper();
          TypeReference<List<CovidStatistics>> typeReference = new TypeReference<List<CovidStatistics>>(){};
          try(InputStream inputStream = TypeReference.class.getResourceAsStream("/CovidStatistics.json")) {
              List<CovidStatistics> covidStatistics = mapper.readValue(inputStream,typeReference);
              for (CovidStatistics cs : covidStatistics) {
                  saveDataFromJsonToDBService.save(cs);
              }
              //saveDataFromJsonToDBService.save(covidStatistics);
              System.out.println("Users Saved!");
          } catch (Exception e){
              System.out.println("Unable to save users: "   e.getMessage());
          }
      };
  }```
  • Related