Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Problem with JAVA ImageIcon


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 PsionicTransvection   Members   -  Reputation: 262

Like
0Likes
Like

Posted 08 June 2012 - 02:34 AM

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class View extends JFrame
{

    JPanel jp = new JPanel();
    JLabel jl1 = new JLabel();
    ImageIcon figure = new ImageIcon(getClass().getResource("/blank.png")); // I get the exception here
    
    public View(){
        setTitle("test");
        setSize(800, 600);
        setResizable(false);
        setVisible(true);
        setLocationRelativeTo(null);
        jl1.setIcon(figure);
        jp.add(jl1);
        add(jp);
        jl1.setBounds(0, 0, jl1.getIcon().getIconWidth(), jl1.getIcon()
            .getIconHeight());
        validate();
    }
    public static void main(String[] args){
        new View();
    }
}

for some reason I can`t fetch the image and the path and name is correct checked it several times
and the funny thing is I`m doing it by checking some old code of mine and there is working perfectly ...
This is the exception I get any Ideas ?

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:167)
at View.<init>(View.java:14)
at View.main(View.java:30)

Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1254

Like
0Likes
Like

Posted 08 June 2012 - 03:29 AM

I think the problem is that you are trying to access member functions of the View class before it is constructed. What happens if you move the assignment of figure to the constructor?

#3 PsionicTransvection   Members   -  Reputation: 262

Like
0Likes
Like

Posted 08 June 2012 - 03:35 AM

same exception different line :) in another program I have done the assignment the same way outside the constructor checked path everything still getting the NullPointerException -_-

#4 RulerOfNothing   Members   -  Reputation: 1254

Like
0Likes
Like

Posted 08 June 2012 - 03:53 AM

Alright, what happens if you remove the slash in the filename?

#5 PsionicTransvection   Members   -  Reputation: 262

Like
0Likes
Like

Posted 08 June 2012 - 04:08 AM

same thing

#6 PsionicTransvection   Members   -  Reputation: 262

Like
0Likes
Like

Posted 08 June 2012 - 10:31 AM

any other ideas ?

#7 PsionicTransvection   Members   -  Reputation: 262

Like
0Likes
Like

Posted 08 June 2012 - 11:39 AM

fixed it had to import the images through Eclipse omg

#8 fae   Members   -  Reputation: 214

Like
0Likes
Like

Posted 09 June 2012 - 03:48 PM

This is normal behavior. The getResource method of the Class delegates the call to the ClassLoader, which looks for the resource in class path. Your call basically asked for a resource called "blank.jpg" at the root of the class path (not the file system root). If you want to load resources outside class path you can use e.g. java.io.File class.

The NullPointerException you got was from the ImageIcon constructor. This happened because the getResource method returned null for a resource that it couldn't find. For more info see http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String).




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS