-1

I don't know what I am doing wrong as I am new in Spring. I have the following method in the following class.

@Component
public class FileInfo {
    
    @Autowired
    ProjectConfiguration configuration;
    
    public FileInfo() {
        System.out.println("Whatever: " + configuration.getWhatever());
    }
}

The methos is already highlighted in the IDE saying that it comes with a NullPointerException warning.

And this is the class I am autowiring. Notice that I am using the @Data lombok.

@Data
@Configuration
public class ProjectConfiguration implements Serializable {

    private String whatever = "Whatever";

    private static volatile ProjectConfiguration instance = null;

    @Bean
    public static ProjectConfiguration getInstance() {
            if (instance == null) {
                synchronized(ProjectConfiguration.class) {
                    if (instance == null) {
                        instance = new ProjectConfiguration();
                    }
                }
            }
            return instance;
    }
}

This is not working. I'm trying to make this ProjectConfiguration a class to store the project settings, which can be recovered, and can be changed. How can I make this work? Why isn't autowiring working? Is the problem maybe in the constructor?

Sotirios Delimanolis
  • 263,859
  • 56
  • 671
  • 702
  • The linked post contains the likely answer. If you disagree, edit your question to explain why you think it doesn't. That post is a canonical resource for the problem you're facing. You won't get a simpler (nor "better") answer. – Sotirios Delimanolis May 30 '22 at 21:50
  • Oscar, I'm asking you to clarify your post if you don't think the linked question's answers also answer your question here. There was nothing derogatory in the comment, it was explaining how this site works. Please see [here](https://meta.stackoverflow.com/questions/252252/this-question-already-has-answers-here-but-it-does-not-what-can-i-do-when-i). – Sotirios Delimanolis Jun 04 '22 at 12:40

0 Answers0