Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么这个简单的spring引导应用程序会出现null指针异常?_Java_Spring Boot_Dependency Injection_Maven 3 - Fatal编程技术网

Java 为什么这个简单的spring引导应用程序会出现null指针异常?

Java 为什么这个简单的spring引导应用程序会出现null指针异常?,java,spring-boot,dependency-injection,maven-3,Java,Spring Boot,Dependency Injection,Maven 3,在尝试调用自动连线对象上的方法时,我不断收到nullpointer异常,我不知道为什么。我做任何事都是按部就班的。(在这种情况下,弹簧起作用)。我知道它正确地包含在应用程序上下文中,因为每当创建可注入对象的单例实例时,我都可以看到打印。下面是代码和pom.xml(它是一个Netbeans maven spring引导项目)。请帮忙 主类 package com.example; import java.util.List; import org.springframework.beans.fa

在尝试调用自动连线对象上的方法时,我不断收到nullpointer异常,我不知道为什么。我做任何事都是按部就班的。(在这种情况下,弹簧起作用)。我知道它正确地包含在应用程序上下文中,因为每当创建可注入对象的单例实例时,我都可以看到打印。下面是代码和pom.xml(它是一个Netbeans maven spring引导项目)。请帮忙

主类

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();
    }
}
配置文件(我知道有未使用的导入) /* *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具|模板 *然后在编辑器中打开模板。 */ 包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();
  }

}
被注入的测试类

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");
    }
}
POM文件

<?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>
正如您所看到的,这是非常简单的,但是我在基本应用程序类的第26行发现了一个错误。谁能告诉我为什么

多谢各位


编辑:我已经更改了示例并删除了静态变量,它仍然会给我一个nullpointer异常

不能自动关联静态变量。有关更多信息:

编辑:编辑后,您可以访问bean,如下所示:

@SpringBootApplication
public class BasicApplication implements ApplicationContextAware {

    private static ApplicationContext ac;

    public static void main(String[] args) {
        SpringApplication.run(BasicApplication.class, args);
        testclass bean = ac.getBean(testclass.class);
        bean.hoi();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ac = applicationContext;
    }
}
在静态上下文中获得Spring的ApplicationContext,然后获得Bean,它将被自动连接

new UsingAutoWired();
从你的主课和

@Autowired
    testclass testclass;
从您的UsingAutoWired类将无法一起工作。
您需要从Spring容器中获取UsingAutoWired的实例,testclass将被注入

您是否自动连接静态变量?正如Raphael Amoedo所建议的,您不能在静态变量上使用自动连接。该对象需要通过Spring容器实例化到带有@autowired注释的对象成员,并执行此任务。是的,我是,但这应该不是问题。我这样做是因为您不能从静态上下文(在本例中是从main方法)引用实例变量。对于自动连接的实例变量,我也遇到过同样的问题。所以这不是问题的原因请看编辑过的示例,我已经更改了示例,使其不再包含静态变量。。当它调用该方法时,我仍然会得到一个nullpionter异常。为什么?这也不是netbeans的问题,我已经用intelliJ运行了这个项目,它给出了与您给出的链接中回答的完全相同的nullpointer exceptionAlready,所以您应该投票以重复方式关闭。我已经编辑了这个示例并包含了另一个类,我不再自动连接一个静态变量,但仍然会得到一个空指针,除非您仍在尝试自动连接
静态测试类testclass。。。如果将UsingAutoWired类设置为
@Component
,请从testclass中删除
static
,并在ComponentScanConfig上自动连接,而不是
新建UsingAutoWired()
(因为这样它将永远不会自动连接)。。。那就行了。在您的示例中,您可能需要将autowire ComponentScanConfig设置为抱歉,我已从Autowired中删除了static,但问题仍然存在。。autowire ComponentScanConfig也是什么意思?现在检查我的编辑是的,我发现这是问题的原因。使用新操作符后,Spring将不再知道该实例,并且无法在该实例中注入任何对象。
@Autowired
    testclass testclass;