Java AWT-eventQueue线程抛出的未知空指针异常

时间:2014-02-21 21:45:34

标签: java swing nullpointerexception jframe awt

这只发生在大约1/10的运行中,我不确定为什么会发生这种情况,例外并没有给我一个错误,我知道

这是我的扩展JFrame的类

private StyledDocument debug;
    private JScrollPane debugSP;
    private StyledDocument chat;
    private JScrollPane chatSP;

    public DapBotGUI()
    {
        super("JFrame");
        setSize(600, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(null);

        JTextPane debug = new JTextPane();

        debugSP = new JScrollPane(debug);
        debugSP.setBounds(0, 0, this.getWidth() - 20, this.getHeight() / 2 - 20);
        debugSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        debugSP.setAutoscrolls(true);

        JTextPane chat = new JTextPane();

        chatSP = new JScrollPane(chat);
        chatSP.setBounds(0, this.getHeight() / 2 - 20, this.getWidth() - 20, this.getHeight() / 2 - 20);
        chatSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        chatSP.setAutoscrolls(true);

        this.getContentPane().add(debugSP);
        this.getContentPane().add(chatSP);
        this.setVisible(true);

        this.debug = debug.getStyledDocument();
        this.chat = chat.getStyledDocument();
    }

    public void debug(String message)
    {
        debug(message, Color.BLACK);
    }

    public synchronized void debug(String message, Color color)
    {
        SimpleAttributeSet formatter = new SimpleAttributeSet();
        StyleConstants.setForeground(formatter, color);
        StyleConstants.setBackground(formatter, Color.WHITE);

        try
        {
            debug.insertString(debug.getLength(), message + System.lineSeparator(), formatter);

            JScrollBar vertical = debugSP.getVerticalScrollBar();
            vertical.setValue(vertical.getMaximum());
        }
        catch (BadLocationException e)
        {
            e.printStackTrace();
        }
    }

    public void chatMessage(String message)
    {
        chatMessage(message, Color.BLACK);
    }

    public synchronized void chatMessage(String message, Color color)
    {
        SimpleAttributeSet formatter = new SimpleAttributeSet();
        StyleConstants.setForeground(formatter, color);
        StyleConstants.setBackground(formatter, Color.WHITE);
        try
        {
            chat.insertString(chat.getLength(), message + System.lineSeparator(), formatter);

            JScrollBar vertical = chatSP.getVerticalScrollBar();
            vertical.setValue(vertical.getMaximum());
        }
        catch (BadLocationException e)
        {
            e.printStackTrace();
        }
    }

然后这是错误,但从我可以看到,我的代码没有直接问题导致此错误(我可以看到)。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.text.FlowView$FlowStrategy.layoutRow(Unknown Source)
    at javax.swing.text.FlowView$FlowStrategy.layout(Unknown Source)
    at javax.swing.text.FlowView.layout(Unknown Source)
    at javax.swing.text.BoxView.setSize(Unknown Source)
    at javax.swing.text.BoxView.updateChildSizes(Unknown Source)
    at javax.swing.text.BoxView.setSpanOnAxis(Unknown Source)
    at javax.swing.text.BoxView.layout(Unknown Source)
    at javax.swing.text.BoxView.setSize(Unknown Source)
    at javax.swing.plaf.basic.BasicTextUI$RootView.setSize(Unknown Source)
    at javax.swing.plaf.basic.BasicTextUI$RootView.paint(Unknown Source)
    at javax.swing.plaf.basic.BasicTextUI.paintSafely(Unknown Source)
    at javax.swing.plaf.basic.BasicTextUI.paint(Unknown Source)
    at javax.swing.plaf.basic.BasicTextUI.update(Unknown Source)
    at javax.swing.JComponent.paintComponent(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JViewport.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent._paintImmediately(Unknown Source)
    at javax.swing.JComponent.paintImmediately(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$1100(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

0 个答案:

没有答案