title: How to fix org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'name'?
body: <p><strong>I tried to make a ManyToMany relationship in spring boot with JPA.</strong></p>
  
  <blockquote>
    <p>Post.java:</p>
  </blockquote>
  
  <pre><code>@Entity
  @Table(name = "post")
  public class Post {
  
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Id
      @Column(name = "postId")
      private Integer postId;
      @Column(name = "name")
      private String name;
      @ManyToMany
      @JoinTable(name="postTag", joinColumns = { @JoinColumn(name = "postId") }, inverseJoinColumns = { @JoinColumn(name = "tagId") })
      private List&lt;Tag&gt; tags = new ArrayList&lt;Tag&gt;();
  
      public Post() {
      }
  
      public Integer getPostId() {
          return postId;
      }
  
      public void setPostId(Integer id) {
          this.postId = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public List&lt;Tag&gt; getTags() {
          return tags;
      }
  
      public void setTags(List&lt;Tag&gt; tags) {
          this.tags = tags;
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>Tag.java:</p>
  </blockquote>
  
  <pre><code> @Entity
  @Table(name = "Tag")
  public class Tag {
  
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Id
      @Column(name = "tagId")
      private Integer tagId;
      @Column(name = "name")
      private String name;
      @ManyToMany(cascade=CascadeType.ALL, mappedBy = "tags")
      private List&lt;Post&gt; posts = new ArrayList&lt;Post&gt;();
  
      public Tag() {
          count = 1;
      }
  
      public Integer getTagId() {
          return tagId;
      }
  
      public void setTagId(Integer id) {
          this.tagId = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public List&lt;Post&gt; getPosts() {
          return posts;
      }
  
      public void setPosts(List&lt;Post&gt; posts) {
          this.posts = posts;
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>PostService.java:</p>
  </blockquote>
  
  <pre><code>@Service
  public class PostService {
  
      private PostRepository postRepository;
  
      @Autowired
      public void setPostRepository(PostRepository postRepository) {
          this.postRepository = postRepository;
      }   
  
      public Post getPostById(Integer id) {
          return postRepository.findByPostId(id);
      }
  
      @PostConstruct
      public void create() {
          Tag tag1 = new Tag();
          tag1.setName("tag1");
  
          Post post = new Post();
          post.setName("post");
          post.getTags().add(tag1);
          postRepository.save(post);
  
          Post dbPost = postRepository.findByPostId(post.getPostId());
          System.out.println(post.getName() + " -&gt; " + post.getTags() + "  |  " 
          + dbPost.getName() + " -&gt; " + dbPost.getTags());
  
      }
  }
  </code></pre>
  
  <blockquote>
    <p>PostRepository.java:</p>
  </blockquote>
  
  <pre><code>public interface PostRepository extends CrudRepository&lt;Post, Integer&gt; {
  
      List&lt;Post&gt; findAll();
  
      Post findByPostId(Integer id);
  
  }
  </code></pre>
  
  <blockquote>
    <p>Full errors on console:</p>
  </blockquote>
  
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homeController': Unsatisfied dependency expressed through method 'setTagService' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tagService': Invocation of init method failed; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:723) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
      at com.hunor.FirstProjectApplication.main(FirstProjectApplication.java:10) [classes/:na]
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tagService': Invocation of init method failed; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:715) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      ... 19 common frames omitted
  Caused by: java.lang.NullPointerException: null
      at com.hunor.service.TagService.create(TagService.java:48) ~[classes/:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_231]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_231]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_231]
      at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_231]
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      ... 31 common frames omitted
  </code></pre>
  
  <blockquote>
    <p>Without the relationship (removed the <code>List&lt;Tag&gt; tags</code> and the <code>List&lt;Post&gt; posts</code> and related parts) works fully fine, no problems with the mySQL database, and with beans. But with the relationship i got the errors above.</p>
  </blockquote>