0

我遇到了一个异常,它不会停止我的程序,而且似乎并没有引起任何问题,但我想知道是什么原因造成的。

在这里,我分配SText.Text"" + R;R 的是一个不断添加到getWheelState()下面的 int:

    SText ST = new SText("",Mainfont,Color.orange,10,300);
        Window.addEntity(ST);//Adds ST to a list of things to draw.
        int R = 0;
        while(true)
        {
            int C = SIn.getWheelState(); //static method that calls (Some MouseWheelEvent).getWheelRotation()
            ST.Text = ""+R; //Changing Text here
            if(R+C != R)
            {
                R += C;
            }
        }

如果有帮助,我认为 R 的值应该是无关紧要的,它是鼠标滚轮滚动的总刻度。对你积极,对你消极:

public class SInput extends MouseAdapter implements MouseListener, MouseWheelListener, KeyListener
{
    protected int MouseScrolled = 0;

    public void mouseWheelMoved(MouseWheelEvent e)
    {
        MouseScrolled += e.getWheelRotation();
    }
    public int HandleWheel()
    {
        int temp = MouseScrolled;
        MouseScrolled = 0;
        return temp;
    }
}

无论如何,这是我迄今为止追踪的方法:

public class SText extends SEntity
{
    public String Text; public Color Col; public Font Fnt;
    public SText(String text,Font f,Color cl,int a,int b)
    {
        Fnt = f;
        Text = text;
        Col = cl;
        x = a;
        y = b;
    }
    public void draw(Graphics g)
    {
            try
            {
                g.drawString(Text,x,y); // Line in question
            }
            catch(NullPointerException e)
            {
                if(Text != null && g != null)
                    throw e; //Exception gets thrown anyways
            }
    }

}

这是堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.lang.String.length(String.java:623)
at sun.font.GlyphList.setFromString(GlyphList.java:241)
at sun.java2d.pipe.GlyphListPipe.drawString(GlyphListPipe.java:71)
at sun.java2d.SunGraphics2D.drawString(SunGraphics2D.java:2829)
at SebsGameEngine.SText.draw(SText.java:29)
at SebsGameEngine.SPanel.paintComponent(SPanel.java:37)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
at javax.swing.JComponent.paint(JComponent.java:1040)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
at java.awt.Container.paint(Container.java:1967)
at java.awt.Window.paint(Window.java:3877)
at javax.swing.RepaintManager$3.run(RepaintManager.java:807)
at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1647)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

如果它有帮助,它似乎只发生在我处理鼠标滚轮输入时。

但这仍然让我感到困惑。如果不是or ,还有什么可能nullgText

4

1 回答 1

0

要了解为什么 Text 为空,您应该向我们展示您实例化 SText 类的代码段,因为您使用在从 SText 类创建对象的代码段中设置的文本和文本值来初始化 SText。请把你的代码放在这里。肯定 Text 或 g 是空的,因为我们在 SebsGameEngine.SText.draw(SText.java:29) 有它显示在 draw 方法中存在空指针。

于 2013-07-06T04:30:18.137 回答