Results 1 to 12 of 12

Thread: Help please?

  1. #1
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Unhappy Help please?

    Hello, I hope I can explain this enough so that you guys know what my problem is...

    I want to know how to include an image in a java application, and, I typed up what I thought should go there, and eclipse told me there were no errors, but when I ran it, it told me there's an error on line 18;

    (by the way, there is one other .java, but there's no errors in it, this is the one with the error)
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Nick{
    	public static void main(String[] args){
    		DisplayMode displayMode = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
    		Nick n = new Nick();
    		n.run(displayMode);
    	}
    	
    	private Screen screen;
    	private Image bg;
    	private Animation a;
    	
    	
    	public void loadPics(){
    		
    		bg = new ImageIcon(getClass().getResource("back.jpg")).getImage();
    		Image face1 = new
                   [COLOR="Red"] //This is where I'm getting the error:[/COLOR]
    ImageIcon(getClass().getResource("face1.png")).getImage();
    		Image face2 = new ImageIcon(getClass().getResource("face2.png")).getImage();
    		a = new Animation();
    		a.addScene(face1, 250);
    		a.addScene(face2, 250);
    	}
    	
    	//main engine to run
    	public void run(DisplayMode dm){
    		screen = new Screen();
    		try{
    			screen.setFullScreen(dm, new JFrame());
    			loadPics();
    			movieLoop();
    		}finally{
    			screen.restoreScreen();
    		}
    	}
    	
    	//main movie loop
    	public void movieLoop(){
    		long startingTime = System.currentTimeMillis();
    		long cumTime = startingTime;
    		
    		while(cumTime - startingTime <5000){
    			long timePassed = System.currentTimeMillis() -cumTime;
    			cumTime += timePassed;
    			a.update(timePassed);
    			
    			Graphics g = screen.getFullScreenWindow().getGraphics();
    			draw(g);
    			g.dispose();
    			
    			try{
    				Thread.sleep(20);
    			}catch(Exception ex){}
    		}
    	}
    	
    	//draw method
    	public void draw(Graphics g){
    		g.drawImage(bg,0,0,null);
    		g.drawImage(a.getImage(),0,0,null);
    	}
    }
    and when I run that code, eclipse gives me this error:
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:167)
    at Nick.loadPics(Nick.java:18)
    at Nick.run(Nick.java:31)
    at Nick.main(Nick.java:8)



    And I DO have those 3 pictures in my src file.
    Last edited by nickburris; 01-23-2011 at 05:13 AM.

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

  3. #3
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    Yes, all 3 of those images are in the same directory as the class files

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

    Default

    Are the image file names spelled correctly? Capitalized correctly? Are you using an IDE? If so, can you post a screen snip of the images files in relation to the class files in your IDE? Have you tried to debug this -- create a very simple program whose only goal is to see if it can create an image from a file?

  5. #5
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default


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

    Default

    Thanks for the image post. And can you see the image file names in the Eclipse package explorer? Also, is your code in packages? Also, why create ImageIcons if you're not going to use them? Why not read in a BufferedImage directly using ImageIO.read(...)? Sorry for all the questions, but these types of problems can be hard to debug sometimes, at least for me.

    Luck!

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by nickburris View Post
    Yes, all 3 of those images are in the same directory as the class files

    Perhaps I'm missing something, but you seem to have the images where the source files are, not the class files.

  8. #8
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    Thanks for the image post. And can you see the image file names in the Eclipse package explorer? Also, is your code in packages? Also, why create ImageIcons if you're not going to use them? [COLOR="rgb(152, 251, 152)"]Why not read in a BufferedImage directly using ImageIO.read(...)?[/COLOR]

    Actually.. recently I closed the eclipse package explorer and can't find out how to open it again..

    What does that mean?

    [COLOR="rgb(255, 140, 0)"]I do use them, they're called in Animation.java [/COLOR]

    [COLOR="rgb(152, 251, 152)"]Sorry.. not quite sure what that means either[/COLOR]

    I'm kind of new to Java, I'm still learning

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

    Default

    Quote Originally Posted by pbrockway2 View Post
    Perhaps I'm missing something, but you seem to have the images where the source files are, not the class files.
    It could be an Eclipse thing for if he added the images through Eclipse via drag and drip, it will actually place the images in both the class and the source package locations. But you are right, we'd best not assume a thing until this is solved.

  10. #10
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    All I did was click "new" in Eclipse, then selected Class...

    And I've used images before in applications, and placed them in this same place

  11. #11
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    Oh! I feel kind of dumb by making this error, but I took the images from the .../src file and put them into the .../bin file with all of my classes. I thought that I put them in the src file last time.. anyways, thanks for the help guys, my bad!

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

Tags for this Thread

Posting Permissions

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