问题描述
我遇到了一个异常,它不会停止我的程序,并且确实似乎不会导致任何问题,但我想知道是什么导致了它.
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.
在这里,我将 SText.Text
分配给 "" + R;
R 是一个 int,它不断被添加到下面的 getWheelState()
中:
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 的值,我认为 应该 无关紧要,如果有帮助的话,就是鼠标滚轮滚动的总刻度数.正面对你,负面远离你:
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
}
}
}
这是堆栈跟踪:
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.
但它仍然让我感到困惑.如果不是 g
或 Text
,还有什么 可以 null
?
But it still confuses me. What else could be null
if not g
or Text
?
推荐答案
要发现 Text 为 null 的原因,您应该向我们展示实例化 SText 类的代码段,因为您使用代码段中设置的文本和文本值初始化 SText从 SText 类创建对象的地方.请把你的代码放在这里.当然 Text 或 g 是空的,因为我们在 SebsGameEngine.SText.draw(SText.java:29) 中有它并且它在 draw 方法中显示空指针存在.
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.
这篇关于字符串中的 NullPointerException 令人困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!