Java version 11. The code doesnt show any errors. but run time show error Failed to execute CommandLineRunner
InputStream inputStream = TypeReference.class.getResourceAsStream("json/countrylist.json");
I add JSON file correctly resource directory. but this line retun null. How do I fix it.
import com.example.countryCRUD.model.Country;
import com.example.countryCRUD.repo.CountryRepository;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Configuration
public class StudentConfigaration {
@Bean
CommandLineRunner commandLineRunner(CountryRepository countryRepository){
return args->{
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<Country>> typeReference = new TypeReference<List<Country>>(){};
InputStream inputStream = TypeReference.class.getResourceAsStream("json/countrylist.json");
try {
List<Country> countryList = mapper.readValue(inputStream,typeReference);
countryRepository.saveAll(countryList);
System.out.println("Users Saved!");
} catch (IOException e){
System.out.println("Unable to save users: " + e.getMessage());
}
};
}
}```
```Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-08-12 20:51:39.466 ERROR 17112 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:794) ~[spring-boot-2.5.3.jar:2.5.3]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:775) ~[spring-boot-2.5.3.jar:2.5.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:345) ~[spring-boot-2.5.3.jar:2.5.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.3.jar:2.5.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.3.jar:2.5.3]
at com.example.countryCRUD.CountryCrudApplication.main(CountryCrudApplication.java:19) ~[classes/:na]
Caused by: java.lang.IllegalArgumentException: argument "src" is null
at com.fasterxml.jackson.databind.ObjectMapper._assertNotNull(ObjectMapper.java:4757) ~[jackson-databind-2.12.4.jar:2.12.4]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3592) ~[jackson-databind-2.12.4.jar:2.12.4]
at com.example.countryCRUD.StudentConfigaration.lambda$commandLineRunner$0(StudentConfigaration.java:27) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:791) ~[spring-boot-2.5.3.jar:2.5.3]
... 5 common frames omitted```
Country class
```package com.example.countryCRUD.model;
import javax.persistence.*;
import java.io.Serializable;
@Embeddable
@Entity
public class Country implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false, updatable = false)
private Long id;
private String name;
public Country() {
}
public Country(Long id, String name) {
this.id = id;
this.name = name;
}
public Country(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Country{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Country.json
[{"name":"Afghanistan"},{"name":"Åland Islands"},{"name":"Albania"},{"name":"Algeria"},{"name":"American Samoa"}]