Java 在HTTP服务器中显示JOptionPane时出现问题

Java 在HTTP服务器中显示JOptionPane时出现问题,java,swing,nullpointerexception,joptionpane,Java,Swing,Nullpointerexception,Joptionpane,我正在用JAVA创建一个项目,在该项目中,我在客户端计算机上启动了一个WEB服务器,除了在浏览器调用中显示页面内容外,我还需要加载一个页面,调用一个JOptionPane,但出现以下错误: Exception in thread "Thread-5" java.lang.NullPointerException at java.util.StringTokenizer. <init> (StringTokenizer.java:199) at java.util.StringTok

我正在用JAVA创建一个项目,在该项目中,我在客户端计算机上启动了一个WEB服务器,除了在浏览器调用中显示页面内容外,我还需要加载一个页面,调用一个
JOptionPane
,但出现以下错误:

Exception in thread "Thread-5" java.lang.NullPointerException at 
java.util.StringTokenizer. <init> (StringTokenizer.java:199) at
java.util.StringTokenizer. <init> (StringTokenizer.java:236) at
pt.paysafe.Server.run (Server.java:44) at java.lang.Thread.run
(Thread.java:748)
Servidor.java(Servidor=server):


我发现了问题,只需删除条件

//int showed = 0;
//if(showed == 0){
JOptionPane.showMessageDialog(null, "Sucesso!", "OK", JOptionPane.INFORMATION_MESSAGE);
//showed = 1;
//}
结果:


什么是pt.paysafe.Server类??stacktrace清楚地显示您的问题在于使用StringTokenizer进行解析。因此,它与JOptionPane完全没有关系。只有当我将JOptionPane移除时,才会出现错误。一切正常,但我需要它!pt.paysafe.Server是负责控制与web服务器相关的所有事件的类。它似乎
String input=in.readLine()正在返回一个
null
值。所以字符串标记器失败,出现异常
package pt.paysafe;

    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.Date;
    import java.util.StringTokenizer;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JOptionPane;

    public class Servidor implements Runnable{

        static final File WebRoot = new File(".");
        static final String DefaultPage = "index.html";
        static final String FileNotFound = "404.html";
        static final String MethodNotSupported = "notSupported.html";

        private Socket connect;

        public Servidor(Socket c) {
            connect = c;
        }

        @Override
        public void run() {
            BufferedReader in = null;
            PrintWriter out = null;
            BufferedOutputStream dataOut = null;
            String fileRequested = null;

            try{
                in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
                out = new PrintWriter(connect.getOutputStream());
                dataOut = new BufferedOutputStream(connect.getOutputStream());
                String input = in.readLine();
                StringTokenizer parse = new StringTokenizer(input);
                String method = parse.nextToken().toUpperCase();
                fileRequested = parse.nextToken().toLowerCase();
                if(!method.equals("GET") && !method.equals("HEAD")){
                    File file = new File(WebRoot, MethodNotSupported);
                    int fileLength = (int) file.length();
                    String contentMimeType = "text/html";
                    byte[] fileData = readFileData(file, fileLength);
                    out.println("HTTP/1.1 501 Not Implemented");
                    out.println("Server: Servidor Plugin PaySafe");
                    out.println("Date: " + new Date());
                    out.println("Content-type: " + contentMimeType);
                    out.println("Content-length: " + fileLength);
                    out.println();
                    out.flush();
                    dataOut.write(fileData, 0, fileLength);
                    dataOut.flush();
                }else{
                    if(fileRequested.endsWith("/")){
                        fileRequested += DefaultPage;
                    }
                    File file = new File(WebRoot, fileRequested);
                    int fileLength = (int) file.length();
                    String contentmt = getContentType(fileRequested);

                    if(method.equals("GET")){
                        int showed = 0;
                        if(showed == 0){
                            JOptionPane.showMessageDialog(null, "Sucesso!", "OK", JOptionPane.INFORMATION_MESSAGE);
                            showed = 1;
                        }
                        byte[] fileData = readFileData(file, fileLength);
                        out.println("HTTP/1.1 200 OK");
                        out.println("Server: Servidor Plugin PaySafe");
                        out.println("Date: " + new Date());
                        out.println("Content-type: " + contentmt);
                        out.println("Content-length: " + fileLength);
                        out.println();
                        out.flush();
                        dataOut.write(fileData, 0, fileLength);
                        dataOut.flush();
                    }
                }
            }catch (FileNotFoundException fnfe){
                try{
                    fileNotFound(out, dataOut, fileRequested);
                }catch(IOException ioe){
                    JOptionPane.showMessageDialog(null, ioe.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
            } finally {
                try {
                    in.close();
                    out.close();
                    dataOut.close();
                    connect.close();
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, e.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
                }
            }
        }

        private byte[] readFileData(File file, int fileLength) throws IOException {
            FileInputStream fileIn = null;
            byte[] fileData = new byte[fileLength];
            try{
                fileIn = new FileInputStream(file);
                fileIn.read(fileData);
            }finally{
                if(fileIn != null){
                    fileIn.close();
                }
            }
            return fileData;
        }

        private String getContentType(String fileRequested) {
            if(fileRequested.endsWith(".htm") || fileRequested.endsWith(".html")){
                return "text/html";
            }else if(fileRequested.endsWith(".ico")){
                return "image/ico";
            }else if(fileRequested.endsWith(".png")){
                return "image/png";
            }else{
                return "text/plain";    
            }
        }

        private void fileNotFound(PrintWriter out, OutputStream dataOut, String fileRequested) throws IOException {
            File file = new File(WebRoot, FileNotFound);
            int fileLenght = (int) file.length();
            String content = "text/html";
            byte[] fileData = readFileData(file, fileLenght);
            out.println("HTTP/1.1 404 Not Found");
            out.println("Server: Servidor Plugin PaySafe");
            out.println("Date: " + new Date());
            out.println("Content-type: " + content);
            out.println("Content-length: " + fileLenght);
            out.println();
            out.flush();
            dataOut.write(fileData, 0, fileLenght);
            dataOut.flush();
        }

    }
//int showed = 0;
//if(showed == 0){
JOptionPane.showMessageDialog(null, "Sucesso!", "OK", JOptionPane.INFORMATION_MESSAGE);
//showed = 1;
//}