0

I am learning Spring and have been trying to autowire reference of an interface with its only concrete implementation.

However, when I try to call a method using the interface reference, I get a Null Pointer Exception.

Exception in thread "main" java.lang.NullPointerException
at personal.nb.behaviours.WhiteWalker.walk(WhiteWalker.java:28)
at personal.nb.springboottutorial.SpringBootTutorialApplication.main(SpringBootTutorialApplication.java:19)

My code is as follows:

Walkable.java

package personal.nb.behaviours;

public interface Walkable {
    void walk();
}

WalkableImplementation.java

package personal.nb.behaviours;

import org.springframework.stereotype.Component;

@Component
public class WalkableImplementation implements Walkable {

    public WalkableImplementation() {
        System.out.println("Default no argument constructor for WalkableImplementation called.");
    }

    @Override
    public void walk() {
        System.out.println("I can walk.");
    }
}

WhiteWalker.java

package personal.nb.behaviours;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WhiteWalker {

    @Autowired
    WalkableImplementation walkable;

    public WhiteWalker() {
        System.out.println("Default no argument constructor for WhiteWalker called.");
        System.out.printf("HashCode:%d%n", hashCode());
    }

    public WhiteWalker(WalkableImplementation walkable) {
        this.walkable = walkable;
        System.out.println("Constructor with walkable argument called.");
    }

    public void setWalkable(WalkableImplementation walkable) {
        this.walkable = walkable;
        System.out.println("Walkable property set.");
    }

    public void walk() {
        walkable.walk();
    }
}

SpringBootTutorialApplication

package personal.nb.springboottutorial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import personal.nb.behaviours.WhiteWalker;

import java.util.Arrays;

@ComponentScan("personal.nb.behaviours")
@SpringBootApplication
public class SpringBootTutorialApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringBootTutorialApplication.class, args);

        WhiteWalker ww = new WhiteWalker();
        ww.walk();
    }

}

pom.xml

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>personal.nb</groupId>
    <artifactId>spring-boot-tutorial</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-tutorial</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Output :

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2019-05-08 12:53:27.211  INFO 29203 --- [           main] p.n.s.SpringBootTutorialApplication      : Starting SpringBootTutorialApplication on apps-MacBook-Pro.local with PID 29203 (/Users/nbatale/IdeaProjects/spring-boot-tutorial/target/classes started by nbatale in /Users/nbatale/IdeaProjects/spring-boot-tutorial)
2019-05-08 12:53:27.214  INFO 29203 --- [           main] p.n.s.SpringBootTutorialApplication      : No active profile set, falling back to default profiles: default
2019-05-08 12:53:28.015  INFO 29203 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2019-05-08 12:53:28.041  INFO 29203 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-05-08 12:53:28.041  INFO 29203 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-05-08 12:53:28.122  INFO 29203 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-05-08 12:53:28.122  INFO 29203 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 844 ms
Default no argument constructor for WalkableImplementation called.
Default no argument constructor for WhiteWalker called.
HashCode:1959708563
2019-05-08 12:53:28.324  INFO 29203 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-05-08 12:53:28.500  INFO 29203 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2019-05-08 12:53:28.504  INFO 29203 --- [           main] p.n.s.SpringBootTutorialApplication      : Started SpringBootTutorialApplication in 16.625 seconds (JVM running for 17.203)
Default no argument constructor for WhiteWalker called.
HashCode:37400149
Exception in thread "main" java.lang.NullPointerException
    at personal.nb.behaviours.WhiteWalker.walk(WhiteWalker.java:28)
    at personal.nb.springboottutorial.SpringBootTutorialApplication.main(SpringBootTutorialApplication.java:19)
Nikit Batale
  • 1,790
  • 2
  • 15
  • 20
  • you need to get instance of WhiteWalker from ApplicationContext instead of WhiteWalker ww = new WhiteWalker(): applicationContext.getBean(WhiteWalker.class) – Sergey Bzhezitskiy May 08 '19 at 07:45
  • You create a WhiteWalker and rely upon a field that has not necessarily been set... – Michael May 08 '19 at 07:50

2 Answers2

2

You are circumventing the whole dependency injection stuff when you create the WhiteWalker manually in your main method. When you create a WhiteWalker with its default constructor the field walkable will be null. And when you call a method on a null object you get a NullPointerException

What you really want is something like this:

@SpringBootApplication
public class SpringBootTutorialApplication implements CommandLineRunner {

    @Autowired
    private WhiteWalker whiteWalker;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootTutorialApplication.class, args);
    }

    public void run(String... args) {
        whiteWalker.walk();
    }

}

This way you get the WhiteWalker instance which is managed by Spring. Spring will automatically set the walkable field and you won't get the NPE.

stevecross
  • 5,588
  • 7
  • 47
  • 85
-2

The reason for you NPE is that WhiteWalker created not by spring, in that case its not a spring bean and @Autowired annotation is ignored.

The right way is to autowire the WhiteWalker:

package personal.nb.springboottutorial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import personal.nb.behaviours.WhiteWalker;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;

@ComponentScan("personal.nb.behaviours")
@SpringBootApplication
public class SpringBootTutorialApplication {
    @Autowired
    WhiteWalker ww;

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringBootTutorialApplication.class, args);

        ww.walk();
    }

}
Pavel
  • 1,627
  • 15
  • 12