使用 PrinterJob 时获取 NullPointerException

Getting NullPointerException while using PrinterJob

我正在尝试通过 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,我的 OS 是 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