java - Netbeans 中的基本 Maven iText 程序

标签 java maven nullpointerexception itext7

我需要从数据库中的一些数据生成一个 Pdf 文档,我遇到了 iTEXT 7。我刚刚在 Netbeans 上启动了我的第一个 Maven 项目(从基本的 hello world 开始),这是输出:

Building mavenproject2 1.0-SNAPSHOT

Exception in thread "main" java.lang.NullPointerException
 at com.mycompany.mavenproject2.class2.main(class2.java:18) BUILD FAILURE [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch. Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

代码:

package com.mycompany.mavenproject2;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.*;

public class class2 {
    public static void main(String args[]) throws FileNotFoundException 
    {
        File file = new File("hello.pdf");
        file.getParentFile().mkdirs();
        PdfWriter writer = new PdfWriter("hello.pdf");
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf);
        document.add(new Paragraph("Hello World!"));
        document.close();
    }
}

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>com.mycompany</groupId>
    <artifactId>mavenproject2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <repositories>
        <repository>
            <id>itext</id>
            <name>iText Repository - releases</name>
            <url>https://repo.itextsupport.com/releases</url>
        </repository>
    </repositories>

    <dependencies>

        <!-- add all iText Core modules -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext7-core</artifactId>
            <version>7.0.3</version>
            <type>pom</type>
        </dependency>

        <!-- pdfSweep -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>cleanup</artifactId>
            <version>1.0.2</version>
        </dependency>

        <!-- pdfCalligraph -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>typography</artifactId>
            <version>1.0.2</version>
        </dependency>

        <!-- pdfInvoice -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>zugferd</artifactId>
            <version>1.0.1</version>
        </dependency>

        <!-- pdfHTML -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>1.0.0</version>
        </dependency>

        <!-- pdfXFA -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>pdfxfa</artifactId>
            <version>1.0.1</version>
        </dependency>

        <!-- iText 7 License Key Library -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-licensekey</artifactId>
            <version>2.0.4</version>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

最佳答案

这是我用于简单项目的 POM 文件:

<repositories>
    <repository>
        <id>itext-releases</id>
        <name>iText Repository - releases</name>
        <url>https://repo.itextsupport.com/releases</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>     

...

<dependencies>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-licensekey</artifactId>
        <version>2.0.4</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.0.4</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.0.4</version>
    </dependency>

    ...

</dependencies>

正如评论中所建议的,它包括版本(许可证 key 所需)以及对许可证 key 存储库的依赖关系。

关于java - Netbeans 中的基本 Maven iText 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45622178/

相关文章:

java - Ggts 内存不足错误

java - Spring Boot内容类型 'multipart/form-data;boundary=--------------------------#;charset=UTF-8'不支持

java - Maven: versions:update-parent 后跟 install 安装旧版本

java - Maven、Tomcat、java.lang.ArrayIndexOutOfBoundsException : 10961

java - 如何使所有包对 Hibernate 可见?

java - 我收到 nullpointerException 请帮助 :)

java - 在android中获取图片文件夹的文件夹大小时出错

java - 可用空间正则表达式选项 (Pattern.COMMENTS) 未按预期工作

java - 在 jface/swt-eclipse 插件开发中单击树查看器中的标签时,如何跳转到编辑器中的特定行

java - JSP——在表单过程中提供 HTTP 500