将图像显示到JFrame中的java.lang.NullPointerException

时间:2014-06-16 22:48:51

标签: java image swing nullpointerexception paint

我尝试做一个只显示图像的程序,一切正常,但控制台显示此警告...... 图像显示正常,但我想知道什么是控制台警告以及如何解决它 这是我的主要课程......

    public class main extends JFrame{
     Image ryu;
     imagen objRyu;
     public main(){
            super("Imagen1");
            this.setSize(500,500);
            this.setVisible(true);
            this.setResizable(true);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            objRyu = new imagen(ryu,"images/Ryu.png",100,100);
            repaint();

        }



    public static void main(String[] args) {
        new main();

    }
    public void paint(Graphics g){
        Graphics g2 = (Graphics2D)g;
         g2.setColor(Color.gray);
         g2.fillRect(0, 0, 500, 500);
         g2.drawImage(objRyu.getImagen(), 100, 50, objRyu.getAncho(), objRyu.getAlto(), null);


    }

}

我的图像类

 public class imagen {
    InputStream imgStream;
    private Image imagen;
    private int ancho;
    private int alto;

    public imagen(Image imagen, String ruta,int ancho, int alto){
        this.imagen = imagen;
        this.ancho = ancho;
        this.alto = alto;

        try{
            imgStream = imagen.class.getResourceAsStream(ruta);
            this.imagen = ImageIO.read(imgStream);
        }catch(IOException e){
            e.printStackTrace();
        }


    }

    public int getAncho() {
        return ancho;
    }

    public int getAlto() {
        return alto;
    }

    public Image getImagen() {
        return imagen;
    }

和控制台日志...

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at main.paint(main.java:34)
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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at main.paint(main.java:34)
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)

3 个答案:

答案 0 :(得分:6)

在调用paint()方法后立即调用被覆盖的setVisible(true)方法,此时对象objRyunull


一些要点:

  1. 请勿直接在JFrame上绘画,而是使用JPanel等容器,并将其添加到JFrame

  2. 而不是覆盖paint()方法使用paintComponent()的{​​{1}}方法。

    JPanel
  3. 添加完所有组件后,最后调用@Overrie public void paintComponent(Graphics g) { super.paintComponent(g); //your custom painting here }

  4. 使用JFrame.setVisible()确保EDT已正确初始化。

    了解更多

  5. 除非您修改现有逻辑,否则不要扩展任何类。

    了解更多


  6. 了解更多

答案 1 :(得分:4)

错误表明objRyu为空。只要任何Swing组件变得可见,它就会被绘制,然后再刷新。

//make this happen early
            objRyu = new imagen(ryu,"images/Ryu.png",100,100);
// this should be last
            this.setVisible(true);

答案 2 :(得分:0)

我明白了,只需在构造函数

之后更改对象调用
public class main extends JFrame{
 Image ryu;
 imagen objRyu;
 public main(){
        super("Imagen1");
        objRyu = new imagen(ryu,"images/Ryu.png",100,100);
        this.setSize(500,500);
        this.setVisible(true);
        this.setResizable(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);



    }