0

I am making an application in which one of the features the constructor requires a picture. The picture is selected using the JFileChooser obviously and then displayed on a JLabel. My problem is I do not have security privilege to access the picture. I tested to see if I do get the absolute path and if the file exists and I did get the path and true for the latter. So how do I give my app access to at least just get pictures?

My code

JFileChooser chooser = new JFileChooser();
            chooser.setFileFilter(new FileNameExtensionFilter("jpg","png"));
            int returnVal = chooser.showOpenDialog(diag);
            if(returnVal == JFileChooser.APPROVE_OPTION) {
                weaponImg = new ImageIcon(TempDialogs.class.getResource(chooser.getSelectedFile().getAbsolutePath()));
                weaponPic.setIcon(weaponImg);
                weaponPic.revalidate();
                weaponPic.repaint();

My error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ui.TempDialogs$5.mouseClicked(TempDialogs.java:171)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
BlacKnight BK
  • 105
  • 2
  • 9

1 Answers1

2

java security not allowing me ..

This has nothing to do with security, despite the stack trace mentioning 'security' in some of the lines. The real problem is at the very top of the stack trace, ..

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

And I expect it all starts with this line of code..

weaponImg = new ImageIcon(TempDialogs.class.getResource(chooser.getSelectedFile().getAbsolutePath()));

Which is both wrong and unnecessarily convoluted.

  1. Get resource is for producing an URL from resources on the application's class path, it is neither needed nor useful for files.
  2. So in this case of trying to access a file, we can use either a File object, or a String the represents a path to a file on the file system. So it could be shortened to:

    weaponImg = new ImageIcon(chooser.getSelectedFile().getAbsolutePath()); // use String 
    
  3. But as alluded to, it could also be loaded as a plain File, so this would also work:

    weaponImg = new ImageIcon(chooser.getSelectedFile()); // use File!
    
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
  • You are amazing thank you. This is how i set the icon for the app, I just copy pasted it like an idiot not knowing what I am copy pasting :) Cheers mate – BlacKnight BK May 31 '17 at 05:49
  • *"This is how i set the icon for the app.."* The question then becomes, why is the end user (with a `JFileChooser`) selecting an icon for the app.?!? An application icon would typically be expected to be 1) unchanging. 2) supplied in the Jar that the application classes come in. 3) In **that case**, we would dump the file chooser and use `getResource(..)` to load the 'embedded resource'. – Andrew Thompson May 31 '17 at 05:52
  • Andrew the pics to be selected are not icons for the app, however, I copy pasted the code that takes the icon for the app and was using it for this purpose – BlacKnight BK May 31 '17 at 06:12
  • BTW - the reason I knew there was a big problem in understanding even from reading the title was that if security was involved, Java would not even allow the app. to display a file chooser. Although there might be parts of the local file system inaccessible to a file chooser, it would simply not display the directories, rather than end up supplying a file/path that would later cause problems. It was only as I was reading the detail of the question that I worked out where the understanding had gone astray, but the title was a giveaway that there **was** a mix up. ;) – Andrew Thompson May 31 '17 at 06:24
  • Good to know, Thanks, Andrew Appreciate it :) I am kind of new to java, just finished my first course in programming and now working on an app that I would release to the public. Feel like I barely learnt much in my class due to the amount of knowledge i am getting now and issues I am facing – BlacKnight BK May 31 '17 at 06:29
  • The reason I mainly thought it was a security issue is that my professor mentioned it once in class when he told us about JFileChooser. Didn't find him in his office though so I proceeded to ask here :) Glad You answered my question – BlacKnight BK May 31 '17 at 06:33
  • *"due to the amount of knowledge i am getting now .."* LOL! Get used to it. I started programming Java in around 1998/9. If I come onto Stack Overflow **today** and don't learn something new, I feel a bit let down. With programming, we should *always* be learning. *"Glad You answered my question"* Thanks for asking it! :) – Andrew Thompson May 31 '17 at 06:35