在执行 getTemplate("report.ftl") 时得到 java.lang.NullPointerException
Got java.lang.NullPointerException while do getTemplate("report.ftl")
我在 Eclipse 中使用 freemarker 2.3.23 来生成报告。
但是在尝试 getTemplate 时得到了 nullpointerException。但是模板文件 "report.ftl" 在那里。不确定我做错了什么。
下面是代码:
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
try {
cfg.setDirectoryForTemplateLoading(new File("C:\Users\zzng"));
} catch (IOException e) {
e.printStackTrace();
cfg=null;
return false;
}
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
rptTemplate=cfg.getTemplate("report.ftl");
Exception in thread "main" java.lang.NullPointerException at
com.barclaycardus.cd.freemarker.Reports.getTemplate(Reports.java:63)
at UnitTest.Test.testConfiguration(Test.java:11) at
UnitTest.Test.main(Test.java:20)
上面的这段代码有效。
它对我不起作用的原因是因为我将它们放入两个单独的方法中并声明了两次,这导致 cfg 成为不同的变量。
我在 Eclipse 中使用 freemarker 2.3.23 来生成报告。 但是在尝试 getTemplate 时得到了 nullpointerException。但是模板文件 "report.ftl" 在那里。不确定我做错了什么。 下面是代码:
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
try {
cfg.setDirectoryForTemplateLoading(new File("C:\Users\zzng"));
} catch (IOException e) {
e.printStackTrace();
cfg=null;
return false;
}
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
rptTemplate=cfg.getTemplate("report.ftl");
Exception in thread "main" java.lang.NullPointerException at com.barclaycardus.cd.freemarker.Reports.getTemplate(Reports.java:63) at UnitTest.Test.testConfiguration(Test.java:11) at UnitTest.Test.main(Test.java:20)
上面的这段代码有效。 它对我不起作用的原因是因为我将它们放入两个单独的方法中并声明了两次,这导致 cfg 成为不同的变量。