0

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?

Sebastian
  • 108
  • 1
  • 9
  • 5
    You are catching the exception and throwing a new one, loosing the original stack trace. Better change that to `throw e;` – leonbloy Jul 05 '13 at 19:06
  • 2
    You should not catch `NullPointerException`, this is a bad practice and there is no reason in your case. – LaurentG Jul 05 '13 at 19:08
  • What are the datatypes of `x` and `y`? – pamphlet Jul 05 '13 at 19:09
  • 7
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jul 05 '13 at 19:10
  • Thanks, leonbloy, I'll update the stack trace – Sebastian Jul 05 '13 at 19:10
  • 1
    It looks like the `String` `Text` that you are trying to draw is `null` . Would be much better if you show your more code. – Vishal K Jul 05 '13 at 19:13
  • if Text was null, it wouldn't re-throw the exception... – Sebastian Jul 05 '13 at 19:13
  • 2
    @VishalK It's not a `String` that is null. The NPE is within `String.length`. – Tom Hawtin - tackline Jul 05 '13 at 19:14
  • @LaurentG I'm aware of this, it is just temporary until I can find the problem – Sebastian Jul 05 '13 at 19:15
  • That's an odd exception - it's from within `String.length`. Going to be JVM version dependent. Presumably unpacking from a byte array representation. I suggest getting the latest version of your JRE. – Tom Hawtin - tackline Jul 05 '13 at 19:16
  • Actually I guess a `String` implementation that uses a full `char[]` without offset and length fields. Latest versions do that. Where did you get `Text` from? – Tom Hawtin - tackline Jul 05 '13 at 19:18
  • Text is public and can be changed at anytime from outside the package. Not sure if it would make a difference, but it might. – Sebastian Jul 05 '13 at 19:21
  • @TomHawtin-tackline: But `String.length` source code is `public int length() { return count; }` . How NPE is possible within this method as`count` is a primitive?. – Vishal K Jul 05 '13 at 19:30
  • 1
    When you encounter high weirdness, set traps for it. Try forcing Text to something sane and seeing if the NPE goes away. Try logging Text. Try logging every other access to Text to see if you have a race condition. – Chris Stratton Jul 05 '13 at 19:40
  • @VishalK It varies between versions. Version in front of me is `return value.length;`. / What appears to be important is how the instance of `String` assigned to `Text` was constructed. – Tom Hawtin - tackline Jul 05 '13 at 19:51
  • 3
    My guess is that the String in question was created using the package-private constructor `String(char[], boolean)`. That's the only way I've found to get `null` into the field `value`. I'm using JDK 1.7.0_25 and I get the same line number in `String.length`. However, I cannot say how this constructor was called. If you can attach the JDK source and perhaps drop a breakpoint in this constructor, it would be interesting to see where it gets called from. – Luke Woodward Jul 05 '13 at 20:15
  • 1
    The string gets created and modified as such: `GUIEl.Text = ""+R;` (R is an `int`) – Sebastian Jul 05 '13 at 20:44

1 Answers1

0

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.

zari
  • 1,709
  • 1
  • 12
  • 18
  • Thanks, I'll add that to my question. – Sebastian Jul 06 '13 at 05:15
  • Ok. Now I'm sure that g is null. Please tell me how you initialize g. – zari Jul 06 '13 at 10:32
  • Dear Sebastian, please tell me why did you use if in the catch block? What's its benefit? If Text!= null and g!=null, then the exception is never thrown. – zari Jul 06 '13 at 10:35
  • That's what confuses me: It _does_ get thrown. – Sebastian Jul 06 '13 at 16:48
  • I get `g` from overriding `paintComponent(Graphics g)` in `Jpanel`, from which I call `draw(g)` for every `Entity` that gets added. I simply add Spanel to a Jframe and let it take care of g. – Sebastian Jul 06 '13 at 16:51