gpt4 book ai didi

java - PageFormat 变量中的 nullpointerException

转载 作者:行者123 更新时间:2023-12-01 18:42:29 25 4
gpt4 key购买 nike

我正在尝试打印一个包含大量内容的 JPanel,它有点大,需要知道页数和页面格式。但是当我尝试在车道碰撞中了解这些事情时,我遇到了空指针异常。

这是一小段代码:

private Component componentToBePrinted;
private PageFormat format;
private int numPages;

componentToBePrinted = this.PanelMatricula;
Dimension page = this.componentToBePrinted.getPreferredSize();

numPages = (int) Math.ceil(page.height/format.getImageableY());// Nullpointerexception
print(componentToBePrinted.getGraphics(), format, this.getNumberOfPages());

我是打印界的新手,我不知道失败是什么,我进行了很多研究,但没有找到任何解决方案。

感谢大家的帮助。

最佳答案

Math.ceil(page.height/format.getImageableY()); 我正在打击 formatnull

printerJob.defaultPage()功能:创建一个新的 PageFormat 实例并将其设置为默认大小和方向。和printerJob.getPageFormat(PrintRequestAttributeSet attributes)计算 PageFormat,其值与该作业的当前 PrintService 支持的值(即 getPrintService() 返回的值)以及属性中包含的介质、可打印区域和方向一致。

因此,您可以使用它们中的任何一个来获取PageFormat

关于java - PageFormat 变量中的 nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19364907/

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