-1

after researching a lot of time and not finding the right solution i now hope someone can help me here.

I create a Spring Boot Application which access a MySQL-Database.

If i run it with

mvn spring-boot:run

all works fine.

But if i package the application with

mvn clean package

i get a NullPointerException:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:803) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:771) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
    at de.conbord.trms.Application.main(Application.java:20) [classes!/:1.0-SNAPSHOT]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_102]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_102]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_102]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_102]
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [trms-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [trms-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [trms-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:58) [trms-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
Caused by: java.lang.NullPointerException: null
    at de.conbord.trms.controller.TokenBuilder.createToken(TokenBuilder.java:17) ~[classes!/:1.0-SNAPSHOT]
    at de.conbord.trms.api.CheckToken.createNewToken(CheckToken.java:66) ~[classes!/:1.0-SNAPSHOT]
    at de.conbord.trms.api.CheckToken.checkValidToken(CheckToken.java:41) ~[classes!/:1.0-SNAPSHOT]
    at de.conbord.trms.controller.TokenLoader.run(TokenLoader.java:41) ~[classes!/:1.0-SNAPSHOT]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
    ... 14 common frames omitted

In think the application can access the database, cause i don't get a Access Denied error, but can't get Data of it.

For completeness, here my 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>

    <groupId>de.conbord</groupId>
    <artifactId>trms</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <packaging>jar</packaging>

    <dependencies>
        <!-- Spring Boot JPA -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <!-- lombock -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.8</version>
        </dependency>

        <!-- Thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

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

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>false</optional>
        </dependency>
        <!-- Apache Commons -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>

        <!-- Spring Boot Security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!-- MySQL Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.9.4</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.1.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

CheckToken.java

@Component
public class CheckToken {

    private final ConfigurationRepository repository;
    private final TokenRepository tokenRepository;
    private static final Logger log = LoggerFactory.getLogger(Application.class);


    @Autowired
    public CheckToken(ConfigurationRepository repository, TokenRepository tokenRepository) {
        this.repository = repository;
        this.tokenRepository = tokenRepository;
    }

    public void checkValidToken() {
        Configuration configuration = this.repository.findByUserName("ADI");
        Token token = this.tokenRepository.findOne(1L);
        if (token == null) {
            token = createNewToken(configuration);
            this.tokenRepository.save(token);
        } else {
            Long tokenId = token.getId();

            Pattern pattern = Pattern.compile("(\\d+)");
            Matcher matcher = pattern.matcher(token.getValidTo());
            if (matcher.find()) {
                Long endDate = Long.parseLong(matcher.group(1));

                Date tokenEndDate = new Date(endDate);
                Date currentDate = Calendar.getInstance().getTime();

                if (currentDate.after(tokenEndDate)) {
                    TokenBuilder tokenBuilder = new TokenBuilder();
                    token = tokenBuilder.createToken(configuration);
                    token.setId(tokenId);
                    this.tokenRepository.save(token);
                }
            }
        }
    }

    private Token createNewToken(Configuration configuration) {
        TokenBuilder tokenBuilder = new TokenBuilder();
        Token token = tokenBuilder.createToken(configuration);
        token.setId(1L);
        return token;
    }
}

TokenBuilder.java

package de.conbord.trms.controller;

import de.conbord.trms.entity.Configuration;
import de.conbord.trms.entity.Token;
import org.springframework.http.HttpEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class TokenBuilder {

    public Token createToken(Configuration configuration) {
        String authString = "SIP sip_username=" + configuration.getUserName()
            + " sip_password=" + configuration.getUserPasswort()
            + " sip_database=" + configuration.getDbName()
            + " app_key=" + configuration.getAppGUID();
        String jsonObject = "{\"AppliesTo\":\"localhost:808/staffitprowebAPI/\", \"TokenType\":\"http://staffITpro.com/tokens/staffITproSecurityToken/type\"}";
        String url = configuration.getUrl() + "Token/";

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("Accept", "application/json");
        headers.add("Content-Type", "application/json");
        headers.add("Authorization", authString);

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

        HttpEntity<String> request = new HttpEntity<>(jsonObject, headers);

        return restTemplate.postForObject(url, request, Token.class);
    }
}

The Configuration Object is saved in the database.

So now i don't understand why i get this Exception if starting the application from jar.

Why I can't access the Configuration from Database within the jar?

Waysti
  • 1
  • 2
  • @Tunaki can you pls explain, why it is an exact copy of the other question? My question is, why i get a NullPointerException when i run the Application as jar. If i run it with Maven then it works. – Waysti Oct 27 '16 at 08:40
  • Where did you read it was an exact copy? Something is `null` in your application, you need to find what that is, and debug your app. It is impossible for someone to help you with the information in this question. See the linked question to find the cause and solutions of this exception. – Tunaki Oct 27 '16 at 08:44
  • Sorry, exact duplicat not copy. My fault. I know what `null` is. The Configuration Object is `null`. After starting the Application it loads the Configuration and stores it to the Configuration Object. After that it creates a new Token. And for that it needs the Configuration Object. So with `mvn spring-boot:run` i get the Data from Database but when i package it with `mvn clean package` and run it as jar `java -jar trms-1.0-SNAPHOT.jar` i don't get it from database. So maybe maven do anything in addition? – Waysti Oct 27 '16 at 08:52
  • Maven is just compiling and packaging your code the way you specified it in your POM. And it is still impossible to help you without seeing the code where the `configuration` is supposed to come from. – Tunaki Oct 27 '16 at 08:56
  • Ok, thx. Now i understand the Problem. So I added the `CheckToken.java` where the Configuration Object is loaded. I used the SpringData JPA Repository to get the Configuration by UserName. But at this point it gets no Data from the Database within the jar. But with `mvn spring-boot:run` it works. – Waysti Oct 27 '16 at 09:26

1 Answers1

0

Looks like the configuration object is null? Possibly you're not passing the same arguments when you run the jar as when maven runs it?

Daniel Scott
  • 7,418
  • 5
  • 39
  • 58