Всем привет, не могли бы вы мне помочь? У меня возникли проблемы с созданием слайдера изображений в моем проекте колледжа. Ошибка :исключение в потоке "AWT-eventqueue-0" java.lang.nullpointerexception.
Hello Everyone.Can you help me?I am getting problem in creating Image Slider in my college project. Error :Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.
run: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Gallery.show(Gallery.java:39) at Gallery.<init>(Gallery.java:27) at Gallery$3.run(Gallery.java:139) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756) 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:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:726) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 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)
Что я уже пробовал:
import java.awt.Image; import java.io.File; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.ImageIcon; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Admin */ public class Gallery extends javax.swing.JFrame { /** * Creates new form Gallery */ public Gallery() { initComponents(); show(position); } int position=0; public String[] takeimage() { File f = new File(getClass().getResource("/imagepath").getFile()); String[] Images=f.list(); return Images; } public void show(int index) { String[] Images=takeimage(); String img=Images[index]; ImageIcon icon=new ImageIcon(getClass().getResource("/imagepath/"+img)); Image image=icon.getImage().getScaledInstance(innerL.getWidth(), innerL.getHeight(),Image.SCALE_SMOOTH); innerL.setIcon(new ImageIcon(image)); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { innerL = new javax.swing.JLabel(); preiv = new javax.swing.JButton(); next = new javax.swing.JButton(); outerL = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); getContentPane().add(innerL, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 50, 320, 210)); preiv.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { preivMousePressed(evt); } }); getContentPane().add(preiv, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 110, 40, 40)); next.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { nextMousePressed(evt); } }); getContentPane().add(next, new org.netbeans.lib.awtextra.AbsoluteConstraints(640, 110, 50, 40)); outerL.setIcon(new javax.swing.ImageIcon("C:\\Users\\Admin\\Desktop\\images\\gallery.jpg")); // NOI18N getContentPane().add(outerL, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 770, 300)); pack(); }// </editor-fold> private void nextMousePressed(java.awt.event.MouseEvent evt) { new Thread(); try { Thread.sleep(300); } catch (InterruptedException ex) { Logger.getLogger(Gallery.class.getName()).log(Level.SEVERE, null, ex); } int p=this.innerL.getX(); if(p>=1) { Animacion.Animacion.mover_izquierda(900, 200, 1, 2, innerL); } position=position+1; if(position>=takeimage().length) { position=takeimage().length-1; } } private void preivMousePressed(java.awt.event.MouseEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Gallery.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Gallery.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Gallery.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Gallery.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Gallery().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JLabel innerL; private javax.swing.JButton next; private javax.swing.JLabel outerL; private javax.swing.JButton preiv; // End of variables declaration }