Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Value is null [duplicate]

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

like image 519
DxAntonxD Avatar asked Dec 14 '25 17:12

DxAntonxD


1 Answers

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;
    }
}
like image 131
Plog Avatar answered Dec 16 '25 10:12

Plog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!