NullPointerException from within String is confusi

2019-06-14 15:56发布

I've come across an exception that doesn't halt my program and really doesn't seem to cause any issues, but I'd like to know what causes it.

Here, I assign SText.Text to "" + R; R is an int constantly being added to getWheelState() below:

    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's value, which I assume should be irrelevent, if helpful, is the total mouse wheel scroll ticks. Positive towards you, negative away from you:

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;
    }
}

Anyways, here is the method I've traced it to so far:

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
            }
    }

}

Here's the stack trace:

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)

If it helps at all, it only seems to happen when I deal with mouse wheel input.

But it still confuses me. What else could be null if not g or Text?

1条回答
做自己的国王
2楼-- · 2019-06-14 16:42

To discover why Text is null you should show us the code segment where you instantiated SText class, because you initialize SText using text and text value set in the code segment where you make object from SText class. Please put your code here. Surely Text or g is null, because we have at SebsGameEngine.SText.draw(SText.java:29) and it shows in draw method null pointer exist.

查看更多
登录 后发表回答