过早地打电话给另一个班级

时间:2016-01-24 04:13:19

标签: java string class

我正在尝试将文件拖放到我的程序中。我正在尝试从类StringFile传递变量FileDragDemo并在我的程序中进一步使用它但是我在执行代码时遇到了一些麻烦,因为它过早地调用了另一个类。一旦我们将文件拖到GUI上的PdfEasyManager,我们想要让程序调用类listbox。但是,我们现在的方式不会等待文件被拖动并导致问题。

我收到以下错误:

  

线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException     在java.io.File。(未知来源)at   PdfEasyManager.main(PdfEasyManager.java:15)at   FileDragDemo $ 1.run(FileDragDemo.java:49)at   java.awt.event.InvocationEvent.dispatch(未知来源)at   java.awt.EventQueue.dispatchEventImpl(未知来源)at   java.awt.EventQueue.access $ 500(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(未知   来自)java.awt.EventQueue.dispatchEvent(未知来源)at   java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知   来自java.awt.EventDispatchThread.pumpEvents(未知来源)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at   java.awt.EventDispatchThread.run(未知来源)

可运行类的代码:

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();
    //}
 }
 }

上一课的课程

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();
        }


 }

}    

1 个答案:

答案 0 :(得分:0)

您可能希望为StringFile课程中的FileDragDemo分配一些值。代码将空值传递给file类中的PdfEasyManager

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

或至少初始化一些default值。[仅举例public static String StringFile = "/Users/xyz/desktop";]