gpt4 book ai didi

java - 使用 PrinterJob 时获取 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 11:06:06 26 4
gpt4 key购买 nike

我正在尝试通过 JavaFX 应用程序打印收据,但每当我尝试打印时都会收到 NullPointerException

import javafx.print.PrinterJob;
// More Imports

public class PrintBasket
{
public PrintBasket()
{
VBox vboxPages = new VBox();
// Some Code
printAction();
}

public void printAction()
{
PrinterJob printerJob = PrinterJob.createPrinterJob();

// Some Code

if(printerJob.printPage(vboxPages)) { // Getting exception at this line
printerJob.endJob();
}
}
}

我得到的异常如下:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at Dialogs.PrintBasket.printAction(PrintBasket.java:638)
at Dialogs.PrintBasket.<init>(PrintBasket.java:214)
at Tabs.PrintUtil.<init>(PrintUtil.java:75)

我的 Java 是版本 8,我的操作系统是 Ubuntu 14.04.01

最佳答案

From the JavaDocs for PrinterJob.createPrinterJob "If there are no printers available, this will return null. Some platforms may provide a pseudo printer, which creates a document. These will be enumerated here so long as the platform also enumerates them as if they are printers"

正如 @MadProgrammer 在上面的评论中指出的那样,我没有安装任何打印机,这就是我收到 NullPointerException 的原因。所以,我继续安装 cups-pdf

我安装了 cups-pdf 如下 -

sudo apt-get install cups-pdf

然后创建了一个 PDF 打印机,我可以使用它来打印收据(来源:Way to Create PDF Printer in Ubuntu)。现在,我在打印时没有收到任何 NullPointerException

关于java - 使用 PrinterJob 时获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29406241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com