Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: images, layers, labels and panels.. something's wrong somewhere, please help!

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    Denmark
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default images, layers, labels and panels.. something's wrong somewhere, please help!

    I made a class called imgObj, so that I could add custom attributes to an Image, such as name, position and layer (front, back, ect.)

    Before further explaination, I'll just post the code:

    public class Window extends JFrame {
     
    	private ArrayList<imgObj> allImages;
    	private JLabel[] labels;
    	private JPanel[] panels;	//<-- contains the labels.
    	private JLayeredPane aLPane = new JLayeredPane() ; //<-- contains the panel.
     
    	public Window(){
    		super("title");
    		setLayout(new BorderLayout());
    		add(aLPane, BorderLayout.CENTER);
    		aLPane.setBounds(0,0,800,600);
     
    		LoadImages();
    		labels = new JLabel[allImages.size()];
    		panels = new JPanel[allImages.size()];
     
    		//loop through allImages, and give all images a label, and adds the label to the window.
    		int counter = 0;
    		for(imgObj i : allImages){
    				labels[counter] = new JLabel(i.getImg());
    				labels[counter].setOpaque(true);
    				labels[counter].setBackground(Color.BLUE);
    				labels[counter].setBounds(i.getPosx(), i.getPosy(), i.getImg().getIconWidth(),i.getImg().getIconHeight());
    				panels[counter].add(labels[counter]);
    				panels[counter].setOpaque(true);
    				panels[counter].setBounds(0, 0, 800, 600);
    				panels[counter].setBackground(Color.BLACK);
    				System.out.printf("%s was added to window, as pos (%d ; %d)\n", i.getName(), i.getPosx(), i.getPosy());
    				counter++;
    				aLPane.add(panels[counter], new Integer(i.getLayer()), 0);
    		}
     
    	}
    }


    My problem is: when I run the code, I get a NullPointerException at the "panels[counter].add(labels[counter]);".
    When looking at the variables in the debug-window (i am using Eclipse), I can see that both my imgObj icons are loaded correctly, and it seems that nothing is wrong the first time the enhanced for-loop is running (yet the console does not print " ... was added to window at pos..."), but the second time, the i.getImg() in "labels[counter] = new JLabel(i.getImg());" is null.

    Any help will (of course) be appriciated, and I will gladly elaporate anything which I have (tried to) explained in this first post..


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,354
    Thanks
    182
    Thanked 832 Times in 775 Posts
    Blog Entries
    5

    Default Re: images, layers, labels and panels.. something's wrong somewhere, please help!

    When you debug, look at the value of panel[counter]....an array of JPanel's is created, but the members of that array must each be instantiated.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Denmark
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: images, layers, labels and panels.. something's wrong somewhere, please help!

    Yep, that was it, #2!
    Thank you!

Similar Threads

  1. Matching Pairs game - something's not right?
    By sambar89 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 9th, 2011, 02:54 PM
  2. adding or removing panels to panels
    By luisp88 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 1st, 2011, 05:37 PM
  3. Java Not Removing Panels?
    By Pantheon8 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 17th, 2011, 08:54 PM
  4. JTabbedPane with two Panels in the same tab?
    By cadarn in forum AWT / Java Swing
    Replies: 1
    Last Post: August 3rd, 2011, 11:48 AM
  5. Help with Layouts and Panels
    By Zookey in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 29th, 2011, 06:48 PM

Tags for this Thread