1
votes

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?

1
how have you instantiated the Facebook object? i.e. private Facebook facebook; @Inject public llenarFeed(Facebook facebook) { this.facebook = facebook; }smoggers
@smoggers In my controller where that method belongs I have an attribute annotated with @Inject called facebook, the Bean is initialized in a SocialConfig class that's annotated with @Configuration and implements SocialConfigurer and the method for the bean is @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
Like I said I have no problem connecting to facebook or twitter and retrieving the home feed, the problem is how to know if the user hasn't connected with each of them since isAuthorized() throws NullPointerException for facebook and twitter.cvalentina
What version are you using?The1Fitz
@The1Fitz For spring social core, web and config 1.1.2, spring social facebook is version 2.0.1, for other spring packages I use version 4.0.3cvalentina

1 Answers

1
votes

I solved with try-catch. When catched an error, it redirects to connection page.

@RequestMapping(value={"/", "feed"}, method = RequestMethod.GET)
public String llenarFeed(Principal principal, Model model){
    boolean emptyFeed = true;
    try{
       if(facebook != null){
            if( facebook.isAuthorized()){
                model.addAttribute("fbFeed", facebook.feedOperations().getHomeFeed());
                model.addAttribute("perfil", facebook.userOperations().getUserProfile());
                emptyFeed = false;
            }
      }
  }
  catch{
     return "redirect:/connect/facebook";
}
    if (twitter  != null) {
        model.addAttribute("timeline", twitter.timelineOperations().getHomeTimeline());
        emptyFeed = false;
    }
    model.addAttribute("usuario", usuarioService.loadUsuarioByUsername(principal.getName()));
    return "feed";
}