0

I am building Java SWT application. As a part of whole process i am working on an excel file which have specific format to process things. What i want is a link on my JFrame which triggers download of a sample excel file stored in my project without local or internet dependency. I don't know how exactly i can do this.

I have tried this FileUtils.copyURLToFile but it is throwing null pointer exception. I am showing my code. Any help would be greatly appreciated.

public void actionPerformed(ActionEvent arg0) {
            log("clicked to download");
            String temp_path = System.getProperty("user.dir")+"\\resources\\EmailSheet.xlsx";
            log("path = " + temp_path);
            URL inputUrl = getClass().getResource(temp_path);
            log("URL = " +inputUrl.toString());
            File dest = new File("D:/new_file.xlsx");
            try {
                FileUtils.copyURLToFile(inputUrl, dest);
            } catch (IOException e) {
                log("Error saving sample file : " + e.getMessage().toString());
                e.printStackTrace();
            }
        }

My Console is printing this error

  Mon Oct 03 09:14:29 IST 2016 : clicked to download
  Exception in thread "AWT-EventQueue-0" Mon Oct 03 09:14:29 IST 2016 : path = \resources\EmailSheet.xlsx
  java.lang.NullPointerException
at tech.excelemail.com.TechExcelEmailApp$7.actionPerformed(TechExcelEmailApp.java:221)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
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)

I want to know if there is mistake in my code. or if i am going the wrong way. Ask me anything that if u think something missing unspecified.

Thanks in advance.

Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
Adi
  • 11
  • 4

1 Answers1

0

It's rather hard to believe that System.getProperty("user.dir") really returned an empty string, as your trace appears to suggest. Is this the real code?

In any case resources are named relative to the CLASSPATH, not the file system, so user.dir has nothing to do with it. They aren't files. And they don't use backslashes. Use forward slashes. And make sure that the resource named is present under that name in the JAR file.

user207421
  • 289,834
  • 37
  • 266
  • 440