10 Replies - 5734 Views - Last Post: 06 October 2011 - 09:06 PM Rate Topic: -----

#1 jokerman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-October 11

Loading Images In Eclipse

Posted 06 October 2011 - 07:34 PM

I'm running into some problems importing images in Eclipse. I'm trying to load some images and then store them in a Vector. Here is my code:
private void loadSprites() {
	BufferedImage sub = null;
	try { 
		sub = ImageIO.read(new File("sub.gif")); 
	}
	catch (IOException e) {	
		System.out.println("Image load failed");
	}
		sprites.add(sub);
}

The program compiles, but when I run it, I get a NullPointerException when I try to access the BufferedImage stored in the vector. I have all the images stored in the src folder for the project in Eclipse. I tried running the program using on my school's UNIX server and there were no issues. Any help would be greatly appreciated. I can post the rest of my code if that would help.

Is This A Good Question/Topic? 0
  • +

Replies To: Loading Images In Eclipse

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Loading Images In Eclipse

Posted 06 October 2011 - 07:42 PM

first your code should be

private void loadSprites() {
	try { 
		BufferedImage sub = ImageIO.read(new File("sub.gif")); 
		sprites.add(sub);  // load only if you can read it :)/>
	}
	catch (IOException e) {	
		System.out.println("Image load failed");
	}
}


and try
private void loadSprites() {
	try { 
                File file = new File(new File("sub.gif"));
                System.out.println("Is the file there ? " + file.exists());
		BufferedImage sub = ImageIO.read(file); 
		sprites.add(sub);  // load only if you can read it :)/>
	}
	catch (IOException e) {	
		System.out.println("Image load failed");
	}
}


Welcome to come back with the output
Was This Post Helpful? 0
  • +
  • -

#3 jokerman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-October 11

Re: Loading Images In Eclipse

Posted 06 October 2011 - 07:59 PM

Here is the code I tried
		private void loadSprites() {			
			try { 
				File file = new File("sub.gif");
				System.out.println("Is the file there ? " + file.exists());
				BufferedImage sub = ImageIO.read(file);
				sprites.add(sub);
			}
			catch (IOException e) {	
				System.out.println("Image load failed");
			}
		}

Here is the console output:

Is the file there ? false
Image load failed

The file "sub.gif" is stored in the src folder of the project.

This post has been edited by jokerman: 06 October 2011 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Loading Images In Eclipse

Posted 06 October 2011 - 08:06 PM

So what else do you want ?
Did you configured your Eclipse project for different folders for the source .java and binaries .class ?
If so, your Image file should go into your .class folder
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Loading Images In Eclipse

Posted 06 October 2011 - 08:09 PM

if it is inside of the source folder inside the project have you tried adding the file path of "src\\sub.gif" If it isn't in the same directory, folder level, you have to add a path if it is above or the subfolder's name then the file name. That is how it is in JCreator I am not sure about Eclipse
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Loading Images In Eclipse

Posted 06 October 2011 - 08:21 PM

View PostFuzzyness, on 06 October 2011 - 11:09 PM, said:

if it is inside of the source folder inside the project have you tried adding the file path of "src\\sub.gif" If it isn't in the same directory, folder level, you have to add a path if it is above or the subfolder's name then the file name. That is how it is in JCreator I am not sure about Eclipse

Not in Eclipse. It makes 2 different folders. The file access to the default directory points to the .class folder so just to move the .jpg, .png there :)
Was This Post Helpful? 1
  • +
  • -

#7 jokerman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-October 11

Re: Loading Images In Eclipse

Posted 06 October 2011 - 08:26 PM

How exactly do I move the image from the src folder to the .class folder? I'm very new to Eclipse and I can't find the .class folder in the Project Explorer. Thanks for all of the help.
Was This Post Helpful? 0
  • +
  • -

#8 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,529
  • Joined: 05-May 05

Re: Loading Images In Eclipse

Posted 06 October 2011 - 08:40 PM

I use Eclipse and I put my image/txt files in the folder above the src folder and I only need the relative path.

This post has been edited by blackcompe: 06 October 2011 - 08:41 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Loading Images In Eclipse

Posted 06 October 2011 - 08:49 PM

View Postjokerman, on 06 October 2011 - 11:26 PM, said:

How exactly do I move the image from the src folder to the .class folder? I'm very new to Eclipse and I can't find the .class folder in the Project Explorer. Thanks for all of the help.

I used Eclipse for more than 10 years
Never ask to have /SRC and /CLASS ??? to be in different folders, never seen a reason for that :)

Just use WindowsExplorer and browse from your Project folder you will find where Eclipse put your .class. Move your Image there
Was This Post Helpful? 0
  • +
  • -

#10 jokerman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-October 11

Re: Loading Images In Eclipse

Posted 06 October 2011 - 09:02 PM

I got the image to load with with it stored in the src folder using
private void loadSprites() {			
			try { 
				File file = new File("src/sub.gif");
				System.out.println("Is the file there ? " + file.exists());
				BufferedImage sub = ImageIO.read(file);
				sprites.add(sub);
			}
			catch (IOException e) {	
				System.out.println("Image load failed");
			}
		}


I know this because the output to the console is:

Is the file there ? true

And and my catch statement doesn't print "Image load failed". Does this mean that something is wrong the image file if I'm still getting the NullPointerPointerException?
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Loading Images In Eclipse

Posted 06 October 2011 - 09:06 PM

View Postjokerman, on 06 October 2011 - 11:26 PM, said:

How exactly do I move the image from the src folder to the .class folder? I'm very new to Eclipse and I can't find the .class folder in the Project Explorer. Thanks for all of the help.

Nothing to do with Eclipse
basic Windows operation of moving files drom a directory to another
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1