Jump to content
  • Advertisement
Sign in to follow this  

Java Image problem

This topic is 2107 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I wrote an Image handler to read in images and then displace them appropriately. I cannot get it to work though.

        public BufferedImage getImage(String key){	
		for(String str : images.keySet()){
			if(str == key){
				System.out.println("Found " + key);
				return images.get(key);
		//if not found
		System.out.println("Not Found " + key);
		return images.get("error");

images is a hashmap of images with a string for the key. When I run it, I always get the error image and I checked my spelling and the .txt file, and it is reading in the images. Thanks for  any help.

Share this post

Link to post
Share on other sites


for(String str : images.keySet()){

            if(str == key){
                System.out.println("Found " + key);
                return images.get(key);



You dont need to iterate the whole hashmap...



just do it:

BufferedImage image = images.get(key);


return image == null ? images.get("error") : image;




Usually in java, if you use '==' with objects you compare if the INSTANCE of the object is the same... not the content...


to use with strings it should be  










Btw.. dont loop a hasmap... the purpose of hashmap is to get the key as fast as possible... if you iterate over it.. the hashmap loose its purpose.


(sry about my english)

Edited by krinosx

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!