Search code examples
javaspring-bootintellij-idea

Arrays.asList Cannot access class in same package


I'm following a Spring Boot tutorial with 2 simple classes. This is the controller class:

package org.example.springbootquickstart.topic;

import java.util.Arrays;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TopicController {

   @RequestMapping( "/topics" )
   public List<Topic> getAllTopics() {
      return Arrays.asList(
            new Topic("123123123", "Spring Framework", "Spring Framework Description"),
            new Topic("java", "Core Java", "Core Java Description"),
            new Topic("javascript", "JavaScript", "JavaScript Description"));
   }
}

This is the Topic class:

package org.example.springbootquickstart.topic;

public class Topic {

   private String id;
   private String name;
   private String description;

   public Topic() {

   }

   public Topic( String id, String name, String description ) {
      super();
      this.id = id;
      this.name = name;
      this.description = description;
   }

// getters and setters ...

}

The main class is in the package org.example.springbootquickstart. Now, this code runs without any issues, I can see the JSON response when opening the route:

enter image description here

The issue is that IntelliJ shows the following error message and I can't figure out why this is the case: Cannot access org.example.springbootquickstart.topic.Topic

enter image description here


Solution

  • It turned out to be an issue with where I saved the project on the system. By mistake, I saved it in the folder C:\Users\USERNAME\IdeaProjects\.idea instead of C:\Users\USERNAME\IdeaProjects\

    After moving the project there are no more issues and automatically rebuilding works as well.