1

Currently I pass a hardcoded string file location to my object method which uses the string in the .getResources() method to load an image file. I am now trying to chooses an image using a load button and pass the loaded file location as a string into the getResource() method. I am using the filename.getAbsolutePath() method to retrieve the file location then passing the filename variable into the object method however this provides me with the following error - Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException. The line of code that it points to having the error is the .getResources line where the image is loaded. I will post the code below to better understand my problem.

btnLoad.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {


                 JFileChooser fc = new JFileChooser();
                 if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
                 {
                    File loadImage = fc.getSelectedFile();
                    String filename = loadImage.getAbsolutePath();
                    filename = filename.replaceAll("\\\\", "\\\\\\\\");
                    picLocation = filename;
                    ImageSwing imageSwing = new ImageSwing(filename);
                    System.out.println(filename);
                 }
              }

The output of the file name is correct yet it still wont pass into the object.

  public class ImageSwing extends JFrame
  {
  public JLabel label;

  public ImageSwing(String S){

  super("Card Stunt");                //Window Title
  setLayout(new FlowLayout());        //lookup grid layout


  Icon flag = new ImageIcon(getClass().getResource(S));          
  label = new JLabel(flag);
  label.setToolTipText(S);
  setSize(1350, 800);
  //setMinimumSize(new Dimension(1200, 760));

  }//main
 }
Blip
  • 3,061
  • 5
  • 22
  • 50
TechCowboy
  • 51
  • 8
  • is the file under your class environment? because you are calling getClass().getResource(S) this will retrieve file begining with classpath + filename – sgpalit Apr 27 '15 at 10:35
  • Can you put the full exception (including stack trace)? Have you tried stepping through with a debugger and looking to see just what is null? – Tim B Apr 27 '15 at 10:40
  • Another tip for using string filenames is to always us forward slashes to separate directories in the path. Java will sort it out. – redge Apr 27 '15 at 10:40
  • what is the output of the line `System.out.println(filename);`? – Blip Apr 27 '15 at 10:41
  • Without knowing your directory structure it's hard to help. See [the tutorial](https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource) and also [this](http://stackoverflow.com/questions/22733881/java-swing-unable-to-load-image-using-getresource), [this](http://stackoverflow.com/questions/2343187/loading-resources-using-getclass-getresource) and [this](http://stackoverflow.com/questions/17002906/trying-to-load-image-using-imageio-readclass-getresourceurl-but-getresource). – user1803551 Apr 27 '15 at 11:05
  • 2
    Having an absolute filename, why not just invoke the constructor of ImageIcon that takes a String as parameter (instead of URL)? – fgwe84 Apr 27 '15 at 11:07

1 Answers1

1

It seems like you create an absolute filename with loadImage.getAbsolutePath(), but then you try to use this as a class path resource with new ImageIcon(getClass().getResource(S)).

Instead, you should just pass the absolute filename, as a string, to ImageIcon:

Icon flag = new ImageIcon(S);

Also, don't forget to add the label to the frame...

getContentPane().add(label);

Also, I'm not on Windows right now, but I don't think filename.replaceAll("\\\\", "\\\\\\\\"); is necessary.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • Icon flag = new ImageIcon(S);, please why Icon from ImageIcon, then you have to test for instanceof, becase if I'm remember correctly not all ImageIcons can to be Icon, rest is correct, plus one – mKorbel Apr 27 '15 at 16:37
  • @mKorbel Not sure what you mean, I just copied that part from your code. But since [`ImageIcon` implements `Icon`](http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html) no casting is needed. – tobias_k Apr 27 '15 at 18:09
  • What *I think* @mKorbel wants is to use `ImageIcon flag = new ImageIcon(S)` instead. Then you don't have to use `if (flag instanceof ImageIcon)` to test if the `Icon` is an `ImageIcon`. – user1803551 Apr 28 '15 at 14:37