Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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 使用批注时的NullPointerException行为_Java_Javafx_Annotations_Java 8_Javafx 8 - Fatal编程技术网

Java 使用批注时的NullPointerException行为

Java 使用批注时的NullPointerException行为,java,javafx,annotations,java-8,javafx-8,Java,Javafx,Annotations,Java 8,Javafx 8,编辑(显示实际问题,而不是示例代码注释代码) 我使用注释创建了自己在Javafx 8中加载fxml文件的方法,而不是使用这个博客的fx:controller标记 我提出了这个处理Javafxfxml文件的想法 @Target(value = {ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface FXMLConfig { public

编辑(显示实际问题,而不是示例代码注释代码)

我使用
注释创建了自己在Javafx 8中加载
fxml
文件的方法,而不是使用这个博客的
fx:controller
标记

我提出了这个处理Javafx
fxml
文件的想法

@Target(value = {ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface FXMLConfig {

    public String path();

}
因此,如果我有一个Javafx控制器
SampleController
,它看起来是这样的

@FXMLConfig(path = "/path/to/SampleController.fxml")
public class SampleController implements Initializable { ... }
为了加载GUI,我使用了一个名为
JavafxWindow

public class JavafxWindow {

    private Stage stage;
    private Scene scene;
    private FXMLLoader loader;

    public void setup(Object controller) throws IOException {
        FXMLConfig config = controller.getClass().getAnnotation(FXMLConfig.class);
        this.loader = new FXMLLoader(controller.getClass().getClassLoader().getResource(config.path()));
        this.loader.setController(controller);
        Parent parent = (Parent) loader.load();
        this.scene = new Scene(parent);
        this.stage = new Stage();
    }

    public void show() {
        this.stage.show();
    }

}
现在,
java.lang.NullPointerException
位于

FXMLConfig config = controller.getClass().getAnnotation(FXMLConfig.class);
this.loader = new FXMLLoader(controller.getClass().getClassLoader().getResource(config.path()));
我测试了代码,它工作得很好,但是在生产过程中出现了NPE行为,这对用户来说是一个非常恼人的bug<代码>异常
在大量使用时在
getAnnotation()
config.path()
处抛出。但是生产代码是使用
OSGi
apachefelix4.6.0
多个
捆绑包部署的。我还在其他部分上使用
Reflection
,但没有抛出
NPE

您能给我一个实现,它带有一个级别的检查来删除异常的
java.lang.NullPointerException
行为吗

  • java版本“1.8.0_45”
  • Java(TM)SE运行时环境(build 1.8.0_45-b14)
  • Java HotSpot(TM)64位服务器虚拟机(构建25.45-b02,混合模式)
  • ApacheFelix4.6.0
  • Ubuntu 14.04/Windows 7/Windows 8/Windows 8.1

感谢社区的积极响应。

这里是一个最小的完整可验证示例。我还没有见过这种类型的NPE,所以我将以我创建自定义注释供参考的方式在这里发布。欢迎进一步提问

自定义注释类(接口)

使用自定义注释初始化(无需任何导入):

驱动程序类来测试以上内容


您是否已经创建了一个简短的程序,每次运行它时都会在这一行上抛出一个NPE?(换句话说,你能创建一个吗?)引发NPE的唯一可能的原因是当类没有注释时…
System.out.println(objectClass.getClass().getAnnotation(CustomAnnotation.class).toString)
--末尾的
toString
在语法上无效。你能发布真正失败的代码吗?当你似乎已经编辑了文章的代码时,很难给出建议。很抱歉,我会尽可能简单地编辑我的代码possible@Arjay我复制粘贴了你的代码并运行了它:它按预期打印了
/path/to/metadata
,我没有得到任何NPE。你能给出一个完整的抛出NPE的例子吗?如果不能重现你的问题,就很难帮助你,因为据我所知,没有问题!也许你只需要重新构建你的项目…我已经编写/使用自定义注释有一段时间了,但我没有看到任何这样的NPE…我很快检查了一下,我发现我总是用
@Inherited@Documented@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)为我的自定义注释接口添加注释
-我不确定是否总是需要这四个,但至少我没有看到任何NPE,也许这就是原因?
import java.lang.annotation.*;
@Inherited
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)

@interface BasicBenefits {
    String bId() default "B-101";
    String bName() default "General Class A Employee";
}
@BasicBenefits(bId="B-400", bName="General Plus Class A Employee")
public class Employee {
    String eId;
    String eName;
    public Employee(String eId, String eName){
        this.eId = eId;
        this.eName = eName;
    }

    public void getEmployeeDetails(){
        System.out.println("Employee ID: "+eId);
        System.out.println("Employee Name: "+eName);
    }
}
import java.lang.annotation.Annotation;
public class TestCustomAnnotationBasicBenefits {
    public static void main(String[] args) throws Exception{
        Employee emp = new Employee("E-100", "user3320018");
        emp.getEmployeeDetails();
        Class reflectedClass = emp.getClass();
        Annotation hopeBenefitAnn = reflectedClass.getAnnotation(BasicBenefits.class);
        BasicBenefits bBenefits = (BasicBenefits)hopeBenefitAnn;
        System.out.println("Benefit ID: "+bBenefits.bId());
        System.out.println("Benefit Name: "+bBenefits.bName());
    }
}