Reputation: 187
I try to implement a REST-Service like in this tutorial: Tutorial
But I got this error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileStorageService' defined in file: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.airgnb.service.FileStorageService]: Constructor threw exception; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.airgnb.service.FileStorageService]: Constructor threw exception; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException
Service-Class:
@Service
public class FileStorageService {
private final Path fileStorageLocation;
@Autowired
public FileStorageService(FileStorageProperties fileStorageProperties) {
this.fileStorageLocation = Paths.get(fileStorageProperties.getUploadDir())
.toAbsolutePath().normalize();
try {
Files.createDirectories(this.fileStorageLocation);
} catch (Exception ex) {
throw new FileStorageServiceException("Could not create the directory where the uploaded files will be stored.", ex);
}
}
}
Configuration-Class:
@Configuration
@ConfigurationProperties(prefix = "file")
public class FileStorageProperties {
private String uploadDir;
public String getUploadDir() {
return uploadDir;
}
public void setUploadDir(String uploadDir) {
this.uploadDir = uploadDir;
}
}
The app is also annotated like this:
@SpringBootApplication
@EnableConfigurationProperties({FileStorageProperties.class})
public class TestApp implements InitializingBean {
I think the only thing which differs is that I use an application.yml instead of an application.properties, but I defined the properties similar to the tutorial, but just in yml style.
I have no idea why the FileStorageProperties
is not injected and therefore the FileStorageService
can't be created.
I tried already to annotate the app with @Import(FileStoroageProperties.class)
and also some other ways of dependency injection like field injection.
Upvotes: 0
Views: 703
Reputation: 866
I don't think FileStorageProperties
was not injected. The error would indicate that no Bean of type FileStorageProperties
was found.
You just have a NullPointerException
inside the constructor.
I think fileStorageProperties.getUploadDir()
returned null
.
Have you set the property file.uploadDir
in your application.yml or application.properties?
Upvotes: 1