1

in this program i wanted to load images in array of image in netbeans. the program works if i read a single picture. however when i used a loop to load the pictures, i get error exactly at "labelx.seticon(imageicon)". as you see i tested if the icons are null and got the message they are not null. any help will be apprciated. the error i got an error in main thread nullpointer exceptions. thanks

public class Image_array2 {

    /**
     * @param args the command line arguments
     */
   public Image_array2()
    {
        photo();
    }
    public static void main(String[] args) {
        new Image_array2();
        int g=5;
        Image [] arrayimages = new Image[49];
        System.out.println("gamal" + g);

        // TODO code application logic here
    }
    public void photo()
    {
        //try{
        ImageIcon myicon;
        ImageIcon[] icons = new ImageIcon[3];
        JFrame frame = new JFrame();
        frame.setSize(900,900);
        JLabel[] labelx = new JLabel[3];
        JLabel mylabel = null;
        Image image;

         for(int i = 0 ; i<3;i++)
         {
             // image= new ImageIcon(this.getClass().getResource("src/image_array2/mycards/image"+i+".jpg")).getImage();
         ////  Image img = new ImageIcon(this.getClass().getResource("src/image_array2/mycards/image22.jpg")).getImage();
           //if(img == null)
              // System.out.println(" it is nothting");
          // else
               //System.out.println("it is okay");

         icons[i] = new ImageIcon("src/image_array2/mycards/image"+i+".jpg");
         myicon = icons[i];
         if(myicon == null)
               System.out.println(" it is nothting");
           else
               System.out.println("it is okay");
         labelx[i].setIcon(icons[i]);
         //label[0].setIcon(icons[i]);
        // mylabel.setIcon(myicon);
        // frame.add(label[i]); }
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
      //  } catch(IOException e) {}
}
    }
}

screen shot of the program

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Gamal Last
  • 27
  • 1
  • 4

2 Answers2

1
labelx[i].setIcon(icons[i]);

labelx[i] is probably null when calling this function

Martin
  • 652
  • 9
  • 27
  • Photos are there. I posted a link to screen shot under my post to stackoerflow.com – Gamal Last Oct 09 '18 at 00:51
  • @GamalLast images are there but object at index i in labelx is null. You have just initialized array of labelx but all objects are null – Martin Oct 09 '18 at 12:16
0

labelx[i] is null. Usually you get a null pointer exception when you try to call a method on a null object.

candino
  • 1,297
  • 3
  • 11
  • 13