0

I am trying to create a button navigation to display images in a jlabel.But when i run the program i get a nullpointer exception.I tried alot but but i cannot disslove the problem.Here is the code of the slideshow gallery

public class gallery extends javax.swing.JFrame {
    public gallery() {
        initComponents();
         showimage(position);
    }
    int position=0;
public String[] takeimage()
{
    File f=new File(getClass().getResource("/gallery").getFile());
    String[] Imageslist=f.list();
    return Imageslist;
}
public void showimage(int index)
{
    String[] Imageslist=takeimage();
    String img=Imageslist[index];
    ImageIcon icon=new ImageIcon(getClass().getResource("/gallery/"+img));
    Image image=icon.getImage().getScaledInstance(mainscreen.getWidth(), mainscreen.getHeight(), Image.SCALE_SMOOTH);
    mainscreen.setIcon(new ImageIcon(image));
}
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.mainscreen.getX();
        
        if(p>-1)
        {
          Animacion.Animacion.mover_izquierda(900, 200, 1, 2, mainscreen);
        }
        position=position+1;
        if(position>=takeimage().length)
        {
            position=takeimage().length-1;
        }
        showimage(position);
    }                              
private void previousMousePressed(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        new Thread();
        try {
            Thread.sleep(300);
        } catch (InterruptedException ex) {
            Logger.getLogger(gallery.class.getName()).log(Level.SEVERE, null, ex);
        }
        int p=this.mainscreen.getX();
        
        if(p>-1)
        {
          Animacion.Animacion.mover_izquierda(900, 200, 1, 2, mainscreen);
        }
          position=position-1;
        if(position<0)
        {
            position=0;
        }
        showimage(position);
    }      

This is the error i am facing everytime i run the program

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at college_management.gallery.showimage(gallery.java:79)
    at college_management.gallery.<init>(gallery.java:25)
    at college_management.gallery$3.run(gallery.java:158)
    
    
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – maloomeister May 06 '21 at 09:55
  • Is your source code missing something? I cannot find the reference of `initComponents()`, `mainscreen`, `Animacion` – Mr. Brickowski May 07 '21 at 02:41
  • mainscreen is the name of jlabel in which i want to display images. – Vaibhav Tomar May 07 '21 at 05:25

0 Answers0