java - OPeration失败

时间:2012-12-21 21:39:28

标签: java

我有下面的代码,但是当我运行它时,我会看到一个消息框,显示OPeration失败,为什么这样做以及可运行的解决方案是什么,代码应该只允许用户从txt文件

代码:

import java.io.*;
import javax.swing.JOptionPane;
import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;

public class printimg
{
    public void printimg() throws FileNotFoundException, PrintException, InterruptedException {
    String filename = ("item.text"); // THIS IS THE TEXT FILE TO PRINT
    try{
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8; //  FILE IS .txt TYPE
    PrintService printService[] =
            PrintServiceLookup.lookupPrintServices(flavor, pras);
    PrintService defaultService =
            PrintServiceLookup.lookupDefaultPrintService();
    PrintService service = ServiceUI.printDialog(null, 200, 200,
            printService, defaultService, flavor, pras);
    if (service != null) {
        DocPrintJob job = service.createPrintJob();
        FileInputStream fis = new FileInputStream(filename);
        DocAttributeSet das = new HashDocAttributeSet();
        Doc doc = new SimpleDoc(fis, flavor, das);
        job.print(doc, pras);

    }
    }
    catch(Exception a){
        JOptionPane.showMessageDialog(null, "OPeration Failed");
    }
}
}

现在收到此错误:

java.lang.IllegalArgumentException: services must be non-null and non-empty
    at javax.print.ServiceUI.printDialog(ServiceUI.java:167)
    at printimg.printimg(printimg.java:20)
    at __SHELL8.run(__SHELL8.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:725)

1 个答案:

答案 0 :(得分:1)

您正在看到该消息,因为代码的try块中的某些内容正在抛出异常。在catch块内的语句上设置断点,开始调试,并查看究竟抛出了什么异常。

这会告诉你什么代码破坏了。从那里,你应该能够弄清楚如何解决它。