0

I'm trying to add some images to those labels,but Eclipse throw me java.lang.NullPointerException and I don't know why?! Please help :}

public class LabelPanel extends JPanel {                

      public JLabel[] bills;                        

      public LabelPanel() {                  
           setLayout(new FlowLayout());                                                                   
           init();               
           labelOrder();            
      }                         


      private void init() {                 
          bills = new JLabel[4];                    
           for (int i = 0; i < bills.length; i++){                                                       
               bills[0].setIcon(newImageIcon("C:\\users\\Acer\\Documents\\images\\1.jpg"));                                 
               bills[1].setIcon(new ImageIcon("C:\\Users\\Acer\\Documents\\images\\2.jpg"));                        
               bills[2].setIcon(new ImageIcon("C:\\Users\\Acer\\Documents\\images\\5.jpg"));                        
               bills[3].setIcon(new ImageIcon("C:\\Users\\Acer\\Documents\\images\\10.jpg"));                       
               bills[4].setIcon(new ImageIcon("C:\\Users\\Acer\\Documents\\images\\20.jpg"));                   
           }                            
       }    

      private void labelOrder() {                               
           add(bills[0]);               
           add(bills[1]);               
           add(bills[2]);               
           add(bills[3]);               
           add(bills[4]);               
       }  
  }
  • 2
    You have at least two errors there. JLabel[4] elements are indexed from 0 to 3. And with bills = new JLabel[4] you are reserving space for four elements, but you haven't instantiated them. You must assign a new JLabel to each position before the loop. bills[0] = new JLabel(); – RubioRic Feb 01 '17 at 11:56
  • More info here https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – RubioRic Feb 01 '17 at 11:57
  • Also on the first line `bills[0].setIcon(`... you need to add a space between `new` and `ImageIcon` – CraigR8806 Feb 01 '17 at 11:58
  • Also, if the file name does not depend on the element index, you can remove the loop. It's useless to execute the same five sentences five times in a row in your case. – RubioRic Feb 01 '17 at 12:22

1 Answers1

2

Try following code

Sample Code

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.FlowLayout;

    public class SwingExampleDemo {  
      public static void main(String[] args) {  
          JFrame f=new JFrame();//creating instance of JFrame 
          ImageIcon imageIcon = new ImageIcon("C:\\users\\Acer\\Documents\\images\\1.jpg"); //here image path 
          JLabel jLabel = new JLabel(imageIcon);    
          f.add(jLabel);         
          f.setLayout(new FlowLayout());//using layout managers  
          f.setVisible(true);//making the frame visible  
          f.pack();
       }  
    }

Hope It will help you...

Raj Rusia
  • 728
  • 10
  • 15
  • @MiraNikol You're welcome. Since you're new here, please don't forget to mark the answer accepted which helped most in solving the problem. See also [How does accepting an answer work?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Raj Rusia Feb 01 '17 at 13:00
  • -1 for suggesting the use of `null-layout`, see [null layout is evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and the answers in [this question](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) to see why it's discouraged to use it. If you edit your code to use the [layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html), I'll retract my downvote – Frakcool Feb 01 '17 at 15:27
  • @Frakcool I edit layout manager – Raj Rusia Feb 01 '17 at 15:58
  • You're still setting bounds and not calling `pack()` – Frakcool Feb 01 '17 at 16:13
  • @Frakcool i edited all the things. – Raj Rusia Feb 01 '17 at 17:14
  • Removing downvote, but remove `setSize()`, `f.pack();` will make the frame to be of the minimum size, where all the components are visible and at their preferred size. So, you need to use `setSize()` (or better override `getPreferredSize()` or `pack()`, see [Should I avoid the use of setPreferred|Minimum|MaximumSize in Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi), the general consensus says yes. – Frakcool Feb 01 '17 at 17:17