0

The application.properties file is already created in src/resources folder ; it contains :

ARTICLE_XML = D:\\Projets\\Oxalys\\XML\\Exemple_export_Articles_.xml
FOURNISSEUR_XML = D:\\Projets\\Oxalys\\XML\\Exemple_export_Fournisseurs.xml
FACTURE_XML = D:\\Projets\\Oxalys\\XML\\Exemple_export_Factures.xml
COMMANDE_XML = D:\\Projets\\Oxalys\\XML\\Exemple_export_Commandes.xml
RECEPTION_XML = D:\\Projets\\Oxalys\\XML\\Exemple_export_Réceptions.xml

ARTICLE_CSV = D:\\Projets\\Oxalys\\CSV\\
FOURNISSEUR_CSV = D:\\Projets\\Oxalys\\CSV\\
FACTURE_CSV = D:\\Projets\\Oxalys\\CSV\\
COMMANDE_CSV = D:\\Projets\\Oxalys\\CSV\\
RECEPTION_CSV = D:\\Projets\\Oxalys\\CSV\\

Inside a class I reference a property :

public class Article {

    @Autowired
    private static Environment env;
    static File xml = new File(env.getProperty("ARTICLE_XML")); // it is the line 31 in the console error
    static File out = new File(env.getProperty("ARTICLE_CSV") + "out_article.csv");

    public static synchronized void process() throws IOException, InterruptedException {

        Thread th = new Thread() {
            @Override
            public void run() {
                try {
                       .....

After compiling and making maven package , when I run the app then I get a console error :

Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.axian.oxalys.main.App.main(App.java:22)
Caused by: java.lang.NullPointerException
    at com.axian.oxalys.classes.Article.<clinit>(Article.java:31)
    ... 1 more

So what is wrong ?

pheromix
  • 18,213
  • 29
  • 88
  • 158
  • 1
    Possible duplicate of [Spring Boot - Environment @Autowired throws NullPointerException](https://stackoverflow.com/questions/19454289/spring-boot-environment-autowired-throws-nullpointerexception) – Ori Marko Jan 14 '19 at 11:28
  • is your Atricle spring managed? A Bean, Component,... – Dirk Deyne Jan 14 '19 at 11:31
  • no it is not managed . – pheromix Jan 14 '19 at 11:33
  • and cannot be static anyways... – Dirk Deyne Jan 14 '19 at 11:44
  • but I want to reference a property inside the declaration `static File xml = new File(...` , so it should be static ! So how to do ? – pheromix Jan 14 '19 at 11:49
  • 1
    Then you can refer to this question in stead: [Can you use `@Autowired` with static fields](https://stackoverflow.com/questions/1018797/can-you-use-autowired-with-static-fields). As you can see, it's possible, but certainly not recommended. Perhaps you should share what you're trying to do, so we can suggest a more recommended way than having static fields. – g00glen00b Jan 14 '19 at 12:01

0 Answers0