Java“ AWT-EventQueue-0” java.lang.NullPointerException

时间:2018-07-16 18:08:30

标签: java swing awt bufferedimage javax.imageio

我正在尝试从目录加载图像,并在单击其中一个按钮时显示它们。不幸的是,我仍然收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at picsFrom.dir.SSGui.NextActionPerformed(SSGui.java:95)
    at picsFrom.dir.SSGui.access$200(SSGui.java:11)
    at picsFrom.dir.SSGui$3.actionPerformed(SSGui.java:60)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6539)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6304)
    at java.awt.Container.processEvent(Container.java:2239)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2297)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
    at java.awt.Container.dispatchEventImpl(Container.java:2283)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Main.java

package picsFrom.dir;

import java.awt.*;


public class Main {
    public static void main(String args[]) {
        image i = new image();
        i.ListFiles();
        String[] cmb = i.combined;
        System.out.println(i.ListFiles().length);
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SSGui().setVisible(true);
            }
        });
    }
}

image.java

package picsFrom.dir;


import java.io.File;
import java.io.FilenameFilter;

public class image {
    private String[] namesPNG, namesJPG, namesGIF;
    public String[] combined;
    public String[] ListFiles() {
        File dir = new File("C:\\Users\\John\\Pictures\\");
        namesPNG = dir.list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".png");
            }
        });
        namesJPG = dir.list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".jpg");
            }
        });
        namesGIF = dir.list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".gif");
            }
        });
        combined = new String[namesPNG.length + namesJPG.length + namesGIF.length];
        int combLenght = namesPNG.length + namesJPG.length;
        System.arraycopy(namesPNG, 0, combined, 0, namesPNG.length);
        System.arraycopy(namesJPG, 0, combined, namesPNG.length, namesJPG.length);
        System.arraycopy(namesGIF, 0, combined, combLenght, namesGIF.length);
        return combined;
    }

}

SSGui.java

package picsFrom.dir;


import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SSGui extends javax.swing.JFrame {


    public SSGui() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    private void initComponents() {

        iP = new javax.swing.JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
                System.out.println(img);
            }
            /*public void setImage(BufferedImage img) {
                this.img = img;
                repaint();
            }*/
        };
        Prev = new javax.swing.JButton();
        Next = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout iPLayout = new javax.swing.GroupLayout(iP);
        iP.setLayout(iPLayout);
        iPLayout.setHorizontalGroup(
                iPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGap(0, 0, Short.MAX_VALUE)
        );
        iPLayout.setVerticalGroup(
                iPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGap(0, 450, Short.MAX_VALUE)
        );

        Prev.setText("Prev");
        Prev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PrevActionPerformed(evt);
            }
        });

        Next.setText("Next");
        Next.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NextActionPerformed(evt);
            }
        });


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(iP, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(Prev, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Next, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(iP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(Prev, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                                        .addComponent(Next, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(0, 0, 0))
        );

        Next.getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>

    private void NextActionPerformed(java.awt.event.ActionEvent evt) {
        int cL = i.ListFiles().length;
        try {
            for (int i = cL; cL > -1; i--) {
                img = ImageIO.read(new File("C:\\Users\\John\\Pictures\\" + cmb[i]));
            }

        } catch (IOException ex) {}
        //iP.setImage(img);
    }

    private void PrevActionPerformed(java.awt.event.ActionEvent evt) {
        int cL = i.ListFiles().length;
        try {
            for (int i = 0; i < cL; i++) {
               img = ImageIO.read(new File("C:\\Users\\John\\Pictures\\" + cmb[i]));

            }

        } catch (IOException ex) {}
        //iP.drawImage(img);
    }

    // Variables declaration - do not modify
    private javax.swing.JButton Next;
    private javax.swing.JButton Prev;
    //private JLabel image;
    image i = new image();
    //imgPanel iP = new imgPanel();
    private BufferedImage img;
    String[] cmb = i.combined;
    private javax.swing.JPanel iP;



}

有人可以告诉我我做错了什么吗?我不知道该如何解决。 我是新手,使用的是IntelliJ Idea,但是UI来自NetBeans。 预先感谢!

0 个答案:

没有答案