I keep getting a nullpointer exception when trying to invoke a method on an autowired object and I don't know why. I am doing everything by the book. (spring in action in this case). I know that it is included in the application context correctly because I can see a print appearing whenever a singleton instance of the injectable object is created. Here is the code and the pom.xml (its a Netbeans maven spring boot project). Please help!
main class
package com.example;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class BasicApplication {
public static void main(String[] args) {
SpringApplication.run(BasicApplication.class, args);
new UsingAutoWired();
}
}
configuration file (with unused imports I know) /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.example;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
/**
*
* @author maurice
*/
@Configuration
public class ComponentScanConfig {
@Bean
public testclass testclass(){
return new testclass();
}
}
the test class that gets injected
package com.example;
import org.springframework.stereotype.Component;
/**
*
* @author maurice
*/
@Component
public class testclass {
public void hoi(){
System.out.println(" ----------moiiii");
}
public testclass(){
System.out.println(" ----------------hoiii");
}
}
the POM file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>basic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>basic</name>
<description>Basic project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
usingAutoWired class
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
/**
*
* @author maurice
*/
public class UsingAutoWired {
@Autowired
testclass testclass;
public UsingAutoWired(){
testclass.hoi();
}
}
As you can see this is as simple as it can get and yet I get an error on line 26 of the basic application class. Can anyone please tell me why?
Thank you
EDIT: I've changed the example and removed the static variable, it still gives me a nullpointer exception!