I'm making a web app for a course similar to a social hub, I'm using Java with Spring, and the Spring social module. The point of the app is to have a place where you can browse the feed from your facebook and twitter accounts. So currently the app allows to create an account, sign in, sign out and browse the feeds. I'm not using spring social for sign in per se, since the app works with accounts that are specific to the app, but after sign in the user can have the option link his/her facebook or twitter account to the app. The app successfully connects with facebook and twitter, and retrieves the feeds without problem. I base my project in the spring social quickstart that can be found in github Spring social quickstart project. The problem itself is that when the user goes to the feed page without having linked the twitter or facebook account to the app it throws a NullPointerException. This is the code for the method where exception occurs
@RequestMapping(value={"/", "feed"}, method = RequestMethod.GET)
public String llenarFeed(Principal principal, Model model){
boolean emptyFeed = true;
if(facebook != null){
if( facebook.isAuthorized()){
model.addAttribute("fbFeed", facebook.feedOperations().getHomeFeed());
model.addAttribute("perfil", facebook.userOperations().getUserProfile());
emptyFeed = false;
}
}
if (twitter != null) {
model.addAttribute("timeline", twitter.timelineOperations().getHomeTimeline());
emptyFeed = false;
}
model.addAttribute("usuario", usuarioService.loadUsuarioByUsername(principal.getName()));
return "feed";
}
The problem occurs in this line if( facebook.isAuthorized()) So I know that facebook is not null, but calling the isAuthorized causes the NullPointerException, the question is how can I solve it?
@Bean @Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES) public Facebook facebook(ConnectionRepository repository) { Connection<Facebook> connection = repository.findPrimaryConnection(Facebook.class); return connection != null ? connection.getApi() : null; }
– cvalentina