Issue
I am getting an error when trying to move my repository initiation from my Application class to a config class. It seems strange because the urlRepository.save call does not throw, while the urlRepository.findAll() gets a null. What am I doing wrong here?
Before (Works Fine - prints to console with the preloaded URL)
@SpringBootApplication
public class UrlShortenerApplication implements CommandLineRunner {
@Autowired
private UrlRepository urlRepository = new MongoUrlRepository();
public static void main(String[] args) {
SpringApplication.run(UrlShortenerApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
String shortlinkStub = "short.li/";
String cascadeDishPods = "https://www.amazon.com/gp/product/B07CTQ8THP/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1";
String flipFlops = "https://www.amazon.com/gp/product/B0013MWDO0/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1";
urlRepository.save(new ShortUrl(shortlinkStub + "flipflops", flipFlops));
urlRepository.findAll().forEach(shortUrl -> System.out.println("Preloaded " + shortUrl));
}
}
After (null pointer exception for repository.findAll())
ShortenerConfig.java
@Configuration
public class ShortenerConfig {
public static UrlRepository urlRepository = new MongoUrlRepository();
@Bean("urlRepo")
public UrlRepository urlRepo() {
return urlRepository;
}
}
UrlShortenerApplication.java
@SpringBootApplication
public class UrlShortenerApplication implements CommandLineRunner {
@Autowired
@Qualifier("urlRepo")
private UrlRepository urlRepository;
public static void main(String[] args) {
SpringApplication.run(UrlShortenerApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
String shortlinkStub = "short.li/";
String cascadeDishPods = "https://www.amazon.com/gp/product/B07CTQ8THP/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1";
String flipFlops = "https://www.amazon.com/gp/product/B0013MWDO0/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1";
urlRepository.save(new ShortUrl(shortlinkStub + "flipflops", flipFlops));
urlRepository.findAll().forEach(shortUrl -> System.out.println("Preloaded " + shortUrl));
}
}
That last line throws a null pointer exception and I don't know why!
Stacktrace
line 22 refers to the "run" and line 35 refers to the reponsitory.findAll()
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-06-12 10:48:25.813 ERROR 75530 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at com.example.urlshortener.UrlShortenerApplication.main(UrlShortenerApplication.java:22) ~[main/:na]
Caused by: java.lang.NullPointerException: null
at com.example.urlshortener.UrlShortenerApplication.run(UrlShortenerApplication.java:35) ~[main/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
... 5 common frames omitted
2020-06-12 10:48:25.843 INFO 75530 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Repository Interface and Class (For Both Cases)
public interface UrlRepository extends MongoRepository<ShortUrl, Long> {
public ShortUrl findByShortUrl(String shortUrl);
public List<ShortUrl> findByLongUrl(String longUrl);
}
public class MongoUrlRepository implements UrlRepository {
@Override
public ShortUrl findByShortUrl(String shortUrl) {
return null;
}
@Override
public List<ShortUrl> findByLongUrl(String longUrl) {
return null;
}
....
Solution
MongoUrlRepository supposed to be an interface, that's the way Spring could manage it's crud implementations automatically. Since you are declaring it as a class you basically overriding the builtin finder methods to just return null, that's where your NPE is coming from.
see the examples from spring.io: https://spring.io/guides/gs/accessing-data-mongodb/
Why it worked in the 1st case:
@Autowired
private UrlRepository urlRepository = new MongoUrlRepository();
You instantiate a MongoUrlRepository, however that object will be replaced during dependency injection and since you're not specifying a qualifier spring will build a managed MongoRepository bean (based on your UrlRepository interface) and ignore your MongoUrlRepository class.
Answered By - gadget
Answer Checked By - David Goodson (JavaFixing Volunteer)