3

Like the Title says, my @Value doesn't passes a value

I've written the following in my application.properties:

project:
  image:
    path= "C:\\Users\\...."

I've got a Class Configuration

@Component
public class Configuration{
    @Value("${project.image.path}")
    public static String ImagePath;
}

In my Other class called Image I want to convert the String path to a File, so that I can work with this value later:

public class Image{
    static File Directory= new File (Configuration.ImagePath);
}


The Problem is, when I use my programm and one of the Methods is used, where the variable Directory is used(which are all written in the Image class), I get a NullPointerException: null and strangely when I refresh the site after the error occurs one time, the error now says NoClassDefFoundError: Could not initialize class com.Project.Image

DxAntonxD
  • 55
  • 1
  • 6

1 Answers1

8

You can't use @Value on static properties. Either remove the static keyword or make a non static setter for your static variable:

@Component
public class Configuration {

    private static String imagePath;

    @Value("${project.image.path}")
    public void setImagePath(String value) {
        this.imagePath = value;
    }
}
Plog
  • 9,164
  • 5
  • 41
  • 66