【问题标题】:Problem showing a JOptionPane in an HTTP server在 HTTP 服务器中显示 JOptionPane 时出现问题
【发布时间】:2019-08-30 10:33:15
【问题描述】:

我正在用 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)

我已经尝试从 paysafe.java 文件中删除线程并简单地给出一个server.run(); 但随后页面无限加载并且没有显示错误。

有人知道我是怎么解决这个问题的吗?

代码:

PaySafe.java

        package pt.paysafe;

    import java.io.IOException;
    import java.net.ServerSocket;
    import javax.swing.JOptionPane;
    public class PaySafe {
        static final int PORT = 8080;

        private static void servidorHttp(){
            try{
                ServerSocket serverConnect = new ServerSocket(PORT);

                while(true){
                    Servidor server = new Servidor(serverConnect.accept());
                    Thread thread = new Thread(server);
                    thread.start();  
                }
            } catch (IOException ex){
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }

        public static void main(String[] args){
            servidorHttp();
        }
    }

Servidor.java (Servidor = 服务器):

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

    }

【问题讨论】:

  • pt.paysafe.Server 类是什么??
  • stacktrace 清楚地表明您的问题出在使用 StringTokenizer 的解析中。因此它与 JOptionPane 完全无关。
  • 只有当我把 JOptionPane 删除时才会出现错误,一切正常,但我需要它!
  • pt.paysafe.Server 是负责控制与 Web 服务器相关的所有事情的类
  • 看来String input = in.readLine(); 正在返回一个null 值。所以 String Tokenizer 因异常而失败

标签: java swing nullpointerexception joptionpane


【解决方案1】:

我发现了问题,把条件去掉就行了

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

结果:

Result

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    相关资源
    最近更新 更多