Results 1 to 6 of 6
  1. #1
    rvd1ofakind is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default ImageIcon problem

    Basiclly for now my goal is a drop down list which shows pictures depending on what is selected.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class GUI extends JFrame{
    	private JLabel ProfilePicture;
    	private JComboBox HeroNames;
    	private static String[] Heroes = {"Alchemist", "Ancient_Apparition", "Anti-Mage", "Axe", "Beastmaster", 
    			"Bloodseeker", "Bounty_Hunter", "Broodmother", "Chen", "Clinkz", "Clockwerk", "Crystal_Maiden", 
    			"Dark_Seer", "Dazzle", "Death_Prophet", "Doom_Bringer", "Dragon_Knight", "Drow_Ranger", "Earthshaker", 
    			"Enchantress", "Enigma", "Faceless_Void", "Huskar", "Invoker", "Jakiro", "Juggernaut", "Kunkka",
    			"Leshrac", "Lich", "Lifestealer", "Lina", "Lion", "Mirana", "Morphling", "Nature's_Prophet", "Necrolyte", 
    			"Night_Stalker", "Omniknight", "Outworld_Destroyer", "Puck", "Pudge", "Queen_Of_Pain", "Razor", "Riki",
    			"Sand_King", "Shadow_Fiend", "Shadow_Shaman", "Silencer", "Skeleton_King", "Slardar", "Sniper", "Spectre", 
    			"Spirit_Breaker", "Storm_Spirit", "Sven", "Tidehunter", "Tiny", "Tinker", "Ursa", "Vengeful_Spirit",
    			"Venomancer", "Viper", "Warlock", "Weaver", "Windrunner", "Witch_Doctor", "Zeus"};
    	private Icon[] pics;
    	public GUI(){
    	super("Dota 2 Helper");
    	for(int i = 0; i < 67 ; i++){
    		pics[i] = new ImageIcon(getClass().getResource(Heroes[i]+".png")); 
    	}
    	setLayout(new FlowLayout());
    	HeroNames = new JComboBox(Heroes);
    	HeroNames.addItemListener(
    			new ItemListener(){
    				public void itemStateChanged(ItemEvent event){
    					if(event.getStateChange()==ItemEvent.SELECTED)
    						ProfilePicture.setIcon(pics[HeroNames.getSelectedIndex()]);
    				}
    			}
    			);
    	add(HeroNames);
    	ProfilePicture = new JLabel(pics[0]);
    	add(ProfilePicture);
    	}
    }
    Errors at:
    pics[i] = new ImageIcon(getClass().getResource(Heroes[i]+".png"));
    ProfilePicture = new JLabel(pics[0]);
    add(ProfilePicture);

    These might be related though

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ImageIcon problem

    You mention "Errors at:..." but neglect to post the error message. That kind of is important information.

  3. #3
    rvd1ofakind is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: ImageIcon problem

    Exception in thread "main" java.lang.NullPointerException
    at GUI.<init>(GUI.java:59)
    at Main.main(Main.java:5)
    main 5 is calling the class

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ImageIcon problem

    You will need to instantiate the pics array before using it. i.e.,

    Java Code:
    private Icon[] pics = new Icon[someNumberGoesHere];

  5. #5
    rvd1ofakind is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: ImageIcon problem

    *cries tears of joy*
    Thank you so much, good sir!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. BufferedImage to ImageIcon
    By han003 in forum New To Java
    Replies: 2
    Last Post: 03-25-2011, 11:13 AM
  2. Need help with ImageIcon and int
    By proceeded in forum New To Java
    Replies: 9
    Last Post: 01-23-2011, 10:10 PM
  3. ImageIcon Transparency
    By Lingerz in forum New To Java
    Replies: 2
    Last Post: 06-04-2010, 02:53 PM
  4. ImageIcon HELP!
    By ben1989 in forum New To Java
    Replies: 1
    Last Post: 05-05-2010, 08:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •