2014-01-29 43 views
0

我正在使用Java中的簡單服務器,該服務器應具有跨計算機傳輸文件的功能。我在Protocol.class的第77行獲得NullPointerException。這裏是堆棧:將文件發送到客戶端時發生Tricky NullPointerException

java.lang.NullPointerException 
     at Protocol.processInput(Protocol.java:77) 
     at Server.main(Server.java:41) 

爲什麼會發生這種情況?第77行沒有null引用!

Client.java:

import java.io.BufferedReader; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.ArrayList; 

import javax.swing.JFileChooser; 
import javax.swing.JOptionPane; 
import javax.swing.filechooser.FileNameExtensionFilter; 


public class Client { 

    private static boolean filein = false; 
    private static ArrayList<String> fln = new ArrayList<>(); 

    public static void main(String[] args) throws IOException { 

     if (args.length != 2) { 
      System.err.println(
       "Usage: java Client <host name> <port number>"); 
      System.exit(1); 
     } 

     String hostName = args[0]; 
     int portNumber = Integer.parseInt(args[1]); 

     try (
      Socket kkSocket = new Socket(hostName, portNumber); 
      PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); 
      BufferedReader in = new BufferedReader(
       new InputStreamReader(kkSocket.getInputStream())); 
     ) { 
      BufferedReader stdIn = 
       new BufferedReader(new InputStreamReader(System.in)); 
      String fromServer; 
      String fromUser = null; 

      while ((fromServer = in.readLine()) != null) { 
       System.out.println("Server: " + fromServer); 
       if (fromServer.equals("@file")) { filein = true; 
       fromUser = "";} 
       else if(fromServer.equals("@[email protected]")) { 

        filein = false; 
        JFileChooser chooser = new JFileChooser(); 
        int returnVal = chooser.showSaveDialog(null); 
        if(returnVal == JFileChooser.APPROVE_OPTION) { 
          String fname = chooser.getSelectedFile().getAbsolutePath(); 
          File f = new File(fname); 
          f.createNewFile(); 
          PrintWriter p = new PrintWriter(f); 
          for(int i = 0; i < fln.size(); i++) { 

           p.println(fln.get(i)); 

          } 

          p.close(); 
          JOptionPane.showMessageDialog(null, "File saved!"); 
        } 


       } 
       else if (filein == true) { 

        fln.add(fromServer); 
        System.out.println(fln.get(fln.size() - 1)); 

       } 
       if (fromServer.equals("Bye.")) 
        break; 

       if (!filein) fromUser = stdIn.readLine(); 
       else if (filein) fromUser = "@contintueFileRun"; 
       if (fromUser != null) { 
        System.out.println("Client: " + fromUser); 
        out.println(fromUser); 
       } 
      } 
     } catch (UnknownHostException e) { 
      System.err.println("Don't know about host " + hostName); 
      System.exit(1); 
     } catch (IOException e) { 
      System.err.println("Couldn't get I/O for the connection to " + 
       hostName); 
      System.exit(1); 
     } 
    } 

} 

Server.java:

import java.io.*; 
import java.net.*; 

import static java.lang.System.out; 

/** 
* Title: FTP Server 
* @author Galen Nare 
* @version 1.0 
*/ 

public class Server { 
    public static void main(String[] args) throws IOException { 

     out.println("Starting server!"); 

     if (args.length != 1) { 
      System.err.println("Usage: java Server <port number>"); 
      System.exit(1); 
     } 

     int portNumber = Integer.parseInt(args[0]); 

     try ( 
      ServerSocket serverSocket = new ServerSocket(portNumber); 
      Socket clientSocket = serverSocket.accept(); 
      PrintWriter out = 
       new PrintWriter(clientSocket.getOutputStream(), true); 
      BufferedReader in = new BufferedReader(
       new InputStreamReader(clientSocket.getInputStream())); 
     ) { 

      String inputLine, outputLine; 

      // Initiate conversation with client 
      Protocol kkp = new Protocol(); 
      outputLine = kkp.processInput(""); 
      out.println(outputLine); 

      while ((inputLine = in.readLine()) != null) { 
       outputLine = kkp.processInput(inputLine); 
       out.println(outputLine); 
       if (outputLine.equals("Bye.")) 
        break; 
      } 
     } catch (IOException e) { 
      System.out.println("Exception caught when trying to listen on port " 
       + portNumber + " or listening for a connection"); 
      System.out.println(e.getMessage()); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

最後,Protocol.java:

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Protocol { 

    enum ServerState { 

     STARTING, 
     WAITING 

    } 

    ArrayList<String> lns; 

    boolean fileout = false; 
    int i = 0; 

     private ServerState state = ServerState.STARTING; 

     public String processInput(String theInput) throws Exception { 
      String theOutput = ""; 

      if (state == ServerState.STARTING) { 
       theOutput = "Hello, Client!"; 
       state = ServerState.WAITING; 
      } 

      if (!theInput.equals("")) { 
       if(theInput.length() > 10 && theInput.startsWith("e")) { 
       if (theInput.substring(0,11).equalsIgnoreCase("executecmd ")) { 
       theOutput = theInput.substring(11); 
       System.out.println(theOutput); 
       try { 
        @SuppressWarnings("unused") 

        Process child = Runtime.getRuntime().exec(theInput.substring(11)); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       theOutput = "Executed " + theInput.substring(11) + "."; 
       } 
      } else if (theInput.equalsIgnoreCase("stop")) { 

       theOutput = "Stopping Server!"; 
       System.exit(0); 

      } else if (theInput.equalsIgnoreCase("executecmd")) { 

       theOutput = "Usage: executecmd <command [-options]>"; 


      } else if (theInput.equalsIgnoreCase("getfile")) { 

       theOutput = "Usage: getfile <file>"; 

      } else if(theInput.length() > 7 && theInput.startsWith("g")) { 
       System.out.println("in"); 
       if (theInput.substring(0,8).equalsIgnoreCase("getfile ")) { 
       theOutput = theInput.substring(8); 
       File f = new File(theInput.substring(8)); 
       Scanner scan = new Scanner(f); 
       ArrayList<String> lns = new ArrayList<>(); 
       while(scan.hasNext()) { 
        lns.add(scan.nextLine()); 
       } 
       for (int i=0; i < lns.size(); i++) { 
        System.out.println(lns.get(i)); 
       } 
       scan.close(); 
       lns.add("@[email protected]"); 
       theOutput = "@file"; 
       fileout = true; 
       } 
      } else if (fileout && i < lns.size()) { 

        theOutput = lns.get(i); 
        i++; 

      } else if (fileout && i == lns.size()) { 

       i = 0; 
       fileout = false; 

      } else { 


       theOutput = "That is not a command!"; 

      } 


      } 
      System.out.print(theOutput); 
      return theOutput; 
    } 
} 

提前感謝!

+1

什麼是第77行? – SLaks

+0

@SLaks第77行如下:'} else if(fileout && i

+1

so ...那些是唯一可以爲空的,因此必須是一個爲空? – eis

回答

4

你永遠不會初始化lnsProtocol,所以它總是一個null參考。您可以只改變聲明閃避:

private List<String> lns = new ArrayList<String>(); 

(我做了它的私人,改變類型List只是出於習慣...)

你也應該考慮給它一個更具可讀性的名稱 - 是否代表行?如果是這樣,請將其稱爲lines! (下一步,考慮一下爲什麼你自己無法診斷出這個問題,你是否在調試器中完成了這個步驟?爲什麼你認爲第77行沒有空引用?你採取了哪些診斷步驟?增加額外的日誌等?重要的是使用這樣的錯誤作爲學習體驗,以使未來的問題更加易於理解。)

+0

我在調試器中完成了這一步。我不是一個非常新的Java程序員。 –

相關問題