2013-07-05 47 views
0

我遇到了一個異常,不會暫停我的程序,真的不似乎導致任何問題,但我想知道是什麼原因導致它。從內部字符串NullPointerException異常令人困惑

在這裏,我分配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) 

如果它有幫助,它似乎只在我處理鼠標滾輪輸入時發生。

但它仍然讓我困惑。還有什麼可能null如果不是gText

+0

g裏面的東西? –

+5

您正在捕捉異常並拋出一個新的異常,從而丟失了原始堆棧跟蹤。更好地將其改爲'拋出e;' – leonbloy

+2

你不應該捕獲'NullPointerException',這是一個不好的做法,在你的情況沒有理由。 – LaurentG

回答

0

要發現爲什麼文本爲空,您應該向我們展示實例化SText類的代碼段,因爲您使用在SText類中創建對象的代碼段中設置的文本和文本值初始化SText。請把你的代碼放在這裏。當然Text或者g爲null,因爲我們在SebsGameEngine.SText.draw(SText.java:29)有它,並且它在draw方法中顯示null指針存在。

+0

謝謝,我會將其添加到我的問題中。 – Sebastian

+0

好的。現在我確定g是空的。請告訴我你是如何初始化g的。 – zari

+0

親愛的塞巴斯蒂安,請告訴我爲什麼你使用,如果在catch塊?它有什麼好處?如果Text!= null且g!= null,那麼這個異常永遠不會被拋出。 – zari