尝试在表之间传输数据并使用Swing显示数据时,为什么会出现NullPointerException?

时间:2019-06-12 17:40:02

标签: nullpointerexception jframe jtable graphics2d

在我正在使用的程序中,我正在使用Swing在简单的打印预览中呈现表格。我选择通过将第一个JFrame中的JTable中的值传递到第二个JTable中,然后将JTable呈现为图像并将其绘制到Graphics2D画布上来执行此操作。在尝试使用drawImage(image,x,y,observer)绘制表格(Image)之前,我不会收到任何错误。使用不同的drawImage方法会产生相同的问题。我最终收到此错误消息(已从中排除了与自己的代码无关的行):

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at printxpert.PrintComponent.createImage(PrintComponent.java:77)
        at printxpert.PrintComponent.drawPage(PrintComponent.java:51)
        at printxpert.PrintComponent.paintComponent(PrintComponent.java:31)

以下是此错误所引用的代码:

public class PrintComponent extends JComponent implements Printable {
private static final Dimension PREFERRED_SIZE = new Dimension(300,300);
private Graphics2D g2D;

private Object tableData[][];
private String[] columns;
private JTable table;
private TablePanel getTable;
private Image tableImage;


public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    drawPage(g2);
}

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    if (page >= 1)
        return Printable.NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.translate(pf.getImageableX(), pf.getImageableY());
    g2.draw(new Rectangle2D.Double(0, 0, pf.getImageableWidth(), pf.getImageableHeight()));

    drawPage(g2);
    return Printable.PAGE_EXISTS;
}

/* This method draws the page on the screen and on the printer graphics context.
@param g2 - the graphics context */
public void drawPage(Graphics2D g2) {
    FontRenderContext context = g2.getFontRenderContext();
    Font font = new Font("Serif", Font.PLAIN, 72);
    GeneralPath clipShape = new GeneralPath();
    tableImage = createImage(table);


    TextLayout layout = new TextLayout(">:3", font, context);
    AffineTransform transform = AffineTransform.getTranslateInstance(200, 100);
    Shape outline = layout.getOutline(transform);
    clipShape.append(outline, false);

    g2.draw(clipShape);
    g2.clip(clipShape);

    g2.drawImage(tableImage, 0, 0, null);

    final int NLINES = 50;
    Point2D p = new Point2D.Double(0,0);

    for (int i = 0; i < NLINES; i++) {
        double x = (2 * getWidth() * i) / NLINES;
        double y = (2* getHeight() * (NLINES -1 - i)) / NLINES;
        Point2D q = new Point2D.Double(x, y);
        g2.draw(new Line2D.Double(p, q));
    }
}

    // Draws image of a table from table data
public Image createImage(JTable table) {
    tableData = getTable.getTableData(); // gets jTable1 data from TableFrame
    String[] columns = {"A", "B", "C", "D"};
    table = new JTable(tableData, columns);
    JTableHeader tableHeaderComp = table.getTableHeader();
    int totalWidth = tableHeaderComp.getWidth() + table.getWidth();
    int totalHeight = tableHeaderComp.getHeight() + table.getHeight();
    tableImage = new BufferedImage(totalWidth, totalHeight,
    BufferedImage.TYPE_INT_RGB);
    Graphics2D g2D = (Graphics2D) tableImage.getGraphics();
    tableHeaderComp.paint(g2D);
    g2D.translate(0, tableHeaderComp.getHeight());
    table.paint(g2D);
    return tableImage;
}

public Dimension getPreferredSize() {
    return PREFERRED_SIZE;
}
}

我了解到,由于错误的变量声明/实例化而引发了Null指针异常,但是我无法真正解决问题。我遇到问题的程序是专有的,包含数千行代码,因此我显然不会在此处发布该程序,但是我 do 拥有一个公共存储库,其中包含所有“简化的”该项目可供任何愿意查看我的代码并伸出援手的人使用。

谢谢!

编辑: Here's the repository,我完全忘了链接。

0 个答案:

没有答案
相关问题