目前我想用点阵打印机打印一份报告,使用的是:https://blog.jocki.me/simple-escp/
下面是我的项目目录:

这是我用来测试它的GUI Jframe
public class Index extends javax.swing.JFrame {
public Index() throws IOException, URISyntaxException {
initComponents();
Template template = new JsonTemplate(Thread.currentThread().
getContextClassLoader().getResource("com/app/tests/template.json").toURI()
);
}
...
}让我们通过以下方式运行它:
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
try {
new Index().setVisible(true);
} catch (IOException | URISyntaxException ex) {
Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
}
});
}我收到错误:如何访问这些template.json?
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.app.views.Index.<init>(Index.java:31)发布于 2021-02-05 16:07:44
此外,您的文件/资源应该存在于test/resources下。
文件夹结构应如下所示:
-src --测试-资源-您的自定义文件夹- template.json
然后尝试:
AnyTestClass.class.getClassLoader().getResource("your-custom-folder/template.json")
or try the below:
AnyTestClass.class.getClassLoader().getResourceAsStream("your-custom-folder/template.json")如果它是一个不是测试类的类,那么您的资源应该在src/main/resources/your-custom-folder/template.json下
Then its Index.class.getClassLoader().getResource()https://stackoverflow.com/questions/66059527
复制相似问题