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 6 of 6

Thread: png problem continued

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default png problem continued

    this is the error i get when i try to run the application.Any Suggestions? I appreciate all help thank you!

    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import java.net.*;
    import java.awt.geom.*;

    public class DrawImage extends JFrame
    {
    private Image image;

    public static void main(String[] args)
    {
    new DrawImage();
    }

    public DrawImage()
    {
    super("DrawImage");
    setSize(600,600);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Toolkit tk = Toolkit.getDefaultToolkit();
    image=tk.getImage(getURL("castle.png"));
    }

    private URL getURL(String filename)
    {
    URL url=null;
    try
    {
    url=this.getClass().getResource(filename);
    }

    catch(NullPointerException e)
    {
    }


    return url;
    }
    public void paint(Graphics g)
    {
    Graphics2D g2d = (Graphics2D)g;

    g2d.setColor(Color.BLACK);
    g2d.fillRect(0,0,getSize().width,getSize().height) ;

    g2d.drawImage(image,0,40,this);
    }
    }










    Uncaught error fetching image:
    java.lang.NullPointerException
    at sun.awt.image.URLImageSource.getConnection(URLImag eSource.java:115)
    at sun.awt.image.URLImageSource.getDecoder(URLImageSo urce.java:125)
    at sun.awt.image.InputStreamImageSource.doFetch(Input StreamImageSource.java:263)
    at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher. java:205)
    at sun.awt.image.ImageFetcher.run(ImageFetcher.java:1 69)


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: png problem continued

    a) That is not the full stack trace b) Please use the code tags c) As I mentioned in another post of yours, do something with exceptions, don't just catch them and then ignore them. At the very least print out the stack trace
    try
    {
    url=this.getClass().getResource(filename);
    }
     
    catch(NullPointerException e)
    {
        e.printStackTrace();
    }

    And last but not least - this topic already exists. Why did you start a new one with the same question? Please read the forum rules.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: png problem continued

    i started a new one because i didnt have the errors on there and i wanted people to see the code and the errors. The code is written out of a book and should work perfectly fine it is word for word. And i dont think the book was published with errors. thats why im confused why it doesnt work. As for the exceptions I do catch them and do something with them but in this case it wasnt necessary because i was copying it from the book Java SE 6 Game Programming. I appreciate the help but can you give me an answer that i can help me figure it out?

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: png problem continued

    I tried the stackTrace() the errors that i have up in the code are the only thing the IDE shows me as errors

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: png problem continued

    My money is on castle.png not existing. Check the location and exact name of the file to make sure it is correct. If all else fails, try using the absolute path of the file.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    NewAtJava (March 31st, 2012)

  7. #6
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: png problem continued

    thanks man thats what i think too

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  2. Continued from Grass problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 19th, 2010, 07:26 PM
  3. Application Continued (problem)
    By Riston in forum AWT / Java Swing
    Replies: 6
    Last Post: November 26th, 2009, 03:33 PM