-1

I am creating a project in JAVA, where I start a WEB server on the client machine, and I need to load a page in addition to showing the page content in the browser call a JOptionPane, but is giving the following error:

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)

I already tried removing the thread from the paysafe.java file and simply giving a server.run(); But then the page is infinitely loading and shows no error.

Does anyone know how I solve this?

Code:

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 = server) :

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

    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
tomas
  • 339
  • 1
  • 2
  • 14
  • What is the class pt.paysafe.Server ?? – Kris Aug 30 '19 at 10:56
  • 1
    The stacktrace clearly shows that your problem is located in the parsing using the StringTokenizer. Therefore it has absolutely nothing to to with JOptionPane. – Robert Aug 30 '19 at 10:59
  • The error only appears when I put JOptionPane if I remove it everything works normally, but I need it! – tomas Aug 30 '19 at 11:01
  • pt.paysafe.Server is the class responsible for controlling everything that happens related to the web server – tomas Aug 30 '19 at 11:02
  • 1
    it seems `String input = in.readLine();` is returning a `null` value. so String Tokenizer is failing with the exception – Sanjeev Aug 30 '19 at 11:12
  • Take a loom at https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – keuleJ Aug 30 '19 at 11:19

1 Answers1

0

I found the problem, just remove the condition

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

Result:

Result

tomas
  • 339
  • 1
  • 2
  • 14