线程中的例外情况

时间:2014-11-23 11:28:24

标签: java nullpointerexception

StackOverflow,您好! 我无法弄清楚为什么Eclipse会让我误以为: 控制台:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at myPanel.paintComponent(Animatia.java:59)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1100(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

代码:

import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;




public class Yeah {


public static void main(String[] args) {
    myFrame frame = new myFrame();
    }
}
class myFrame extends JFrame
{
public myFrame()
{
myPanel np = new myPanel(); 
Container cont = getContentPane();
cont.add(np);
setBounds(250, 250, 250, 250);
setVisible(true);
}
}
class myPanel extends JPanel {

private int x=0,y=1555;
private Image img;
public myPanel()
{
Timer nt = new Timer(1,new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        x++;
        y--;
        repaint();

    }
});

nt.start();
try
{
img = ImageIO.read(new File("C:\\Users\\1\\Downloads\\skin.png"));
}
catch(IOException exp) {}
}
public void paintComponent(Graphics gr)
{
gr.clearRect(x-1, y-1, img.getWidth(null)+1, img.getHeight(null)+1);
gr.drawImage(img,x,y,null);
}
}

我不知道为什么我的控制台充满了那些 - 我认为这与导入有关... 请问有人请告诉我该代码应该修复什么? 附:你能否告诉我StackOverflow是否因为这些原因而有破坏者? P.P.S.我是Java的新手,所以如果我犯了愚蠢的错误,请不要评判我。

2 个答案:

答案 0 :(得分:0)

不要吞下异常!我打赌你的'img'是空的。但是你忽略了IOException。因此,如果有一个,img仍为null,后续对引用的调用 - 例如'img.height()' - 将抛出一个NPE。

答案 1 :(得分:0)

NeverMind已经,我自己修复了 - 问题在于(Public void myPanel();) 空虚是奇怪的。