0

I’m trying to get a file drag implemented into my program. I’m trying to pass the variable StringFile from the class FileDragDemo and use it further in my program but I’m having a some trouble with it executing the code where it calls the other class prematurely. What we want to do for the program to call class PdfEasyManager once we drag a file into the listbox on the GUI. However, the way we have it now does not wait for the file to be dragged and is causing problems.

I get the following errors:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.io.File.(Unknown Source) at PdfEasyManager.main(PdfEasyManager.java:15) at FileDragDemo$1.run(FileDragDemo.java:49) at java.awt.event.InvocationEvent.dispatch(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.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)

Code of the runable class:

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

@SuppressWarnings("serial")
public class FileDragDemo extends JPanel {

//static FileListTransferHandler objectFileListTransferHandler = new     FileListTransferHandler ();

public static String StringFile; 
public JList list = new JList();


public FileDragDemo() {
  list.setDragEnabled(true);
  list.setTransferHandler(new FileListTransferHandler(list));

  add(new JScrollPane(list));

}

public static void createAndShowGui() {
  FileDragDemo mainPanel = new FileDragDemo();

  JFrame frame = new JFrame("FileDragDemo");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(mainPanel);
  frame.pack();
  frame.setLocationByPlatform(true);
  frame.setVisible(true);

}

public static void main(String[] args) throws Exception {

   //String filestring ="";
   //data = new List;

  SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        createAndShowGui();
        /*StringFile = FileListTransferHandler.StringFile;
        PdfEasyManager PdfEasyManagerObject = new PdfEasyManager ();
        try {
            PdfEasyManagerObject.main(args);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
     }
  });
}
}

@SuppressWarnings("serial")
class FileListTransferHandler extends TransferHandler {
public JList list;
//public static FileDragDemo.file;
//public File file;
//public List data;
public static String StringFile;

public FileListTransferHandler(JList list) {
  this.list = list;
}

public int getSourceActions(JComponent c) {
  return COPY_OR_MOVE;
}

public boolean canImport(TransferSupport ts) {
  return ts.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
}

public boolean importData(TransferSupport ts) {
  try {
     @SuppressWarnings("rawtypes")

     List data = (List) ts.getTransferable().getTransferData(
           DataFlavor.javaFileListFlavor);
     if (data.size() < 1) {
        return false;
     }// close if

     DefaultListModel listModel = new DefaultListModel();

     for (Object item : data) {

          File file = (File) item;
         //file1 = item;
        listModel.addElement(file);
        System.out.println ("%%%%%%%%%%%file... " + file);
        StringFile = file.toString();
        System.out.println ("%%%%%%%%%%%string... " + StringFile);
     } // close for
     //String filestring = file.toString();

     list.setModel(listModel);
     return true;

  }// close try  
  catch (UnsupportedFlavorException e) {
     return false;
  } catch (IOException e) {
     return false;
  }

  //PdfEasyManager PdfEasyManagerObject = new PdfEasyManager ();
  //try {
        //PdfEasyManagerObject.main(args);
    //} catch (IOException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
    //}
 }
 }

Class that is follows the previous class

import java.io.File;
import java.io.IOException;


public class PdfEasyManager {
static FileDragDemo objectFileDragDemo = new FileDragDemo ();
public static String StringFile = objectFileDragDemo.StringFile;
//static FileListTransferHandler objectFileDragDemo = new 
FileListTransferHandler ();
public static File file;  

public static void main(String[] args) throws IOException {

    System.out.println ("StringFile is: " + StringFile);
    file = new File (StringFile);

    EasySearch easysearchobject = new EasySearch();
    System.out.println(easysearchobject.ToText()); //For some reason this needs to be there in order for it to work..?

    searchPdftext searchPdftextobject = new searchPdftext();
                try {
                    searchPdftextobject.main(null);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


 }

}    
Moe
  • 57
  • 5
  • 1
    could you paste here these lines : `PdfEasyManager.java:15` and at `FileDragDemo.java:49` – Naman Jan 24 '16 at 04:50
  • Sure! 15: `file = new File (StringFile);` 49 is commented out.. `/*StringFile = FileListTransferHandler.StringFile;` Not sure whats up with that.. – Moe Jan 24 '16 at 05:05
  • You never initialize `StringFile` inside of `FileDragDemo`. – Makoto Jan 24 '16 at 05:38

1 Answers1

0

You might want to assign some value to StringFile in your FileDragDemo class. The code is passing a null value to the file in PdfEasyManager class.

public static String StringFile = objectFileDragDemo.StringFile; //This is null
public static File file;  
public static void main(String[] args) throws IOException {

    System.out.println ("StringFile is: " + StringFile);
    file = new File (StringFile); //Exception here

OR at least initialise with somedefault value.[just for example public static String StringFile = "/Users/xyz/desktop";]

Naman
  • 27,789
  • 26
  • 218
  • 353