简体   繁体   English

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

[英]java.lang.NullPointerException on show image into JFrame

I tried to do a program that just show an image, everything goes right but the console shows this warning... The image shows fine but i wanna know what is the console warning and how to solve it Here my main class... 我试图做一个只显示图像的程序,一切正常,但控制台显示此警告...图像显示正常,但我想知道控制台警告是什么以及如何解决它,这是我的主要课程...

    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);


    }

}

my imagen class 我的图像课

 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;
    }

and the console log... 和控制台日志...

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)

The overridden paint() method is called immediately after calling setVisible(true) method and at that point object objRyu is null . 调用setVisible(true)方法后,将立即调用重写的paint()方法,此时对象objRyunull


Some Points: 一些要点:

  1. Don't directly paint on JFrame instead use a container such as JPanel and add it in JFrame . 不要直接在JFrame绘制,而是使用诸如JPanel的容器并将其添加到JFrame

  2. Instead of overriding paint() method use paintComponent() method for JPanel . 代替重写paint() paintComponent()方法,对JPanel使用paintComponent()方法。

     @Overrie public void paintComponent(Graphics g) { super.paintComponent(g); //your custom painting here } 
  3. Call JFrame.setVisible() in the end after adding all the component. 添加所有组件后,最后调用JFrame.setVisible()

  4. Use SwingUtilities.invokeLater() to make sure that EDT is initialized properly. 使用SwingUtilities.invokeLater()确保EDT正确初始化。

    Read more 阅读更多

  5. Don't extend any class until and unless you are modifying the existing logic. 除非修改现有逻辑,否则不要扩展任何类。

    Read more 阅读更多


Read more 阅读更多

The error indicates that objRyu is null. 该错误表明objRyu为空。 As soon as any Swing component becomes visible, it is painted and later refreshed again. 一旦看到任何Swing组件,就将其绘制并稍后再次刷新。

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

I got it , just change the object call after the constructor 我知道了,只需在构造函数之后更改对象调用

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);



    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM