При запуске приложения выскакивает куча ошибок, если определить некие параметры в одном класе для работы другого класса. Вот первый контроллер который должен использовать данные из другого класса:
package hello;
import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Version;
import com.restfb.types.Event;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.inject.Inject;
@Controller
@RequestMapping("/")
public class HelloController {
private Facebook facebook;
AccessTokenController accessTokenController;
//FacebookClient.AccessToken accessToken = new DefaultFacebookClient(Version.VERSION_2_2).obtainAppAccessToken(accessTokenController.getAppId(), accessTokenController.getAppSecret() );
FacebookClient facebookClient = new DefaultFacebookClient(accessTokenController.getAccess(), accessTokenController.getAppSecret(), Version.VERSION_2_2);
@Inject
public HelloController(Facebook facebook) {
this.facebook = facebook;
}
@RequestMapping(method= RequestMethod.GET)
public String helloFacebook(Model model) {
if (!facebook.isAuthorized()) {
return "redirect:/connect/facebook";
}
/*model.addAttribute(facebook.userOperations().getUserProfile());
PagedList<Post> homeFeed = facebook.feedOperations().getHomeFeed();
model.addAttribute("feed", homeFeed);*/
Connection<Event> events = facebookClient.fetchConnection("closerkiev/events", Event.class);
model.addAttribute("event", events);
/*model.addAttribute(facebook.groupOperations().getGroup("closerkiev"));
PagedList<Post> groupEvent = facebook.feedOperations().getFeed("closerkiev");
model.addAttribute("feed", groupEvent);*/
//model.addAttribute(facebook.eventOperations().getEvent("1635141143384681"));
/*String event = facebook.eventOperations().getEvent("1635141143384681").getDescription();
model.addAttribute("event", event);*/
/*String query = "SELECT eid, name FROM event WHERE creator=closerkiev";
List<FqlEvent> event = facebookClient.executeFqlQuery(query, FqlEvent.class);
model.addAttribute("event", event);*/
return "hello";
}
}
Вот второй класс, который генерирует данные
package hello;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Version;
public class AccessTokenController {
private String AppId = "мой апп айди";
private String AppSecret = "мой апп сикрет";
FacebookClient.AccessToken accessToken = new DefaultFacebookClient(Version.VERSION_2_2).obtainAppAccessToken(AppId, AppSecret);
private String access = accessToken.getAccessToken();
public String getAccess() {
return access;
}
public String getAppSecret() {
return AppSecret;
}
// public String getAppId() {
// return AppId;
// }
}
и класс запуска приложения:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}
После запуска выкидывает кучу эксепшенов:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController' defined in file [D:\java\FBLogin\target\classes\hello\HelloController.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [hello.HelloController]: Constructor threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:275) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1139) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) at hello.Application.main(Application.java:14) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [hello.HelloController]: Constructor threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:267) ... 22 more Caused by: java.lang.NullPointerException at hello.HelloController.(HelloController.java:29) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147) ... 24 more
По моим подозрениям во всемвиноват первый эксепшн:
Error creating bean with name 'helloController'
Как с этим бороться? Или точнее будет поставить вопрос где я туплю?