PageFormat变量中的nullpointerexception(nullpointerexception in a PageFormat variable)

编程入门 行业动态 更新时间:2024-10-27 18:20:23
PageFormat变量中的nullpointerexception(nullpointerexception in a PageFormat variable)

我试图打印一个包含大量内容的JPanel,它有点庞大,需要知道页数和页面格式。 但是当我试图在车道崩溃中知道这些事情并且我有一个nullpointer异常。

这是一段很短的代码:

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());

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

谢谢大家的帮助。

im trying to print a JPanel with a lot of contents and it's a bit huge and need to know the num of pages and the Page format. But when i try to know these things in a lane crash and i have a nullpointer exception.

this is a short piece of code:

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());

Im new in the wolrd of the printing and i don't know what's the fail, i researched a lot and don't find any solution.

Thanks all for the help.

最满意答案

Math.ceil(page.height/format.getImageableY()); 我的format是null

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

所以,您可以使用其中任何一个来获取PageFormat 。

Math.ceil(page.height/format.getImageableY()); I am bating format is null

The printerJob.defaultPage() function: Creates a new PageFormat instance and sets it to a default size and orientation. And printerJob.getPageFormat(PrintRequestAttributeSet attributes) calculates a PageFormat with values consistent with those supported by the current PrintService for this job (i.e., the value returned by getPrintService()) and media, printable area and orientation contained in attributes.

so, you can use either of them to get the PageFormat.

更多推荐

本文发布于:2023-07-15 20:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1118569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   PageFormat   variable   nullpointerexception

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!