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

Thread: getResource()-error

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question getResource()-error

    My code compiled without errors, but when I modified three pics in photoshop and turned them to gif (from jpg) the program wont compile since thereīs some error with the getResource(). How to fix? Canīt change back either.


  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: getResource()-error

    thereīs some error with the getResource()
    What error and where in what code? We can only guess without the full text of the error and code accompanying said error

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: getResource()-error

    Ok, thought the error may come from some rule like that the getResource() loads stuff or something.

    Error:
    fileName=pink.gif
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at Pointer.<init>(Pointer.java:18)
    at Clock.<init>(Clock.java:26)
    at Main.<init>(Main.java:7)
    at Main.main(Main.java:17)
    Hereīs the code:
    Main:
    import javax.swing.JFrame;

    public class Main extends JFrame {

    public Main(){
    add(new Clock());
    setTitle("Klocka");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(400,400);
    setVisible(true);
    setLocationRelativeTo(null);
    setResizable(false);
    }

    public static void main(String[] args){
    new Main();
    }
    }
    Clock:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;

    import javax.swing.ImageIcon;
    import javax.swing.JPanel;


    public class Clock extends JPanel {




    private Image back;

    public Pointer pPink;
    public Pointer pWeird;
    public Pointer pRose;




    public Clock(){
    pPink = new Pointer(3600,"pink.gif");
    pWeird = new Pointer(60,"weird.gif");
    pRose = new Pointer(1,"rose.gif");

    }

    public void paint(Graphics g) {
    if (pPink.img != null){
    System.out.println("got through if statement pPink");
    g.drawImage(pPink.getImg(), 200, 200, null);
    }

    if (pWeird.img != null){
    System.out.println("got through if statement pWeird");
    g.drawImage(pWeird.getImg(), 250, 200, null);
    }

    if (pRose.img != null){
    System.out.println("got through if statement pRose");
    g.drawImage(pRose.getImg(), 300, 200, null);
    }




    ImageIcon i = new ImageIcon(this.getClass().getResource("wheel.png") );
    back = i.getImage();
    g.drawImage(back,50,50, null);

    }




    }
    Pointer:

    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.ImageIcon;


    public class Pointer {

    public int speed;
    public String fileName;
    public Image img;

    public Pointer(int speed, String fileName) {
    this.speed = speed;
    this.fileName = fileName;
    System.out.println("fileName=" + this.fileName);
    ImageIcon ii = new ImageIcon(getClass().getResource(this.fileName));
    img = ii.getImage();

    }


    public Image getImg(){
    System.out.println("args=" + fileName);
    return img;

    }




    }

  4. #4
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: getResource()-error

    As I mentioned earlier: the code worked before changing the jpgīs to gifīs (I changed all .jpg names in the code to .gif also).

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: getResource()-error

    main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at Pointer.<init>(Pointer.java:18)
    Look at the code at line 18.
    Does the getResource method return a null?
    Does the file referenced in the fileName variable exist?

  6. #6
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: getResource()-error

    The getResource() method returns the fileName
    And yes, the file exists.

    I thinks itīs quite weird since all I did was to modify the pics from jpg to gif, (deleted the jpgs and inserted the gifs to my drawable folder, and turned every jpg in the code to gif).

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: getResource()-error

    What is causing the NullPointerException? What variable or value is null?

    The getResource() method returns the fileName
    The code shows that the fileName variable being passed to the getResource() method.
    Your statement does not make sense.
    Read the API doc for the getResource method to see what it returns.

Similar Threads

  1. More getResource and Eclipse question
    By cl2606 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 8th, 2011, 06:39 PM