2017-10-25 14 views
0

基本的にはソケットを作成しています。私はこのエラーを得続ける何らかの理由 しかし、「メイン」java.lang.ArrayIndexOutOfBoundsExceptionスレッドでコマンドライン引数の理解

例外:行」の文字列である0 Main.ChatClient.mainで (ChatClient.java:143)

server = args [0]; "

この問題を解決するには、何が必要なのですか?考える

package Main; 


    import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.WindowAdapter; 
    import java.awt.event.WindowEvent; 
    import java.io.BufferedReader; 
    import java.io.IOException; 
    import java.io.InputStreamReader; 
    import java.io.OutputStream; 
    import java.net.Socket; 
    import java.util.Observable; 
    import java.util.Observer; 


// Class to manage Client chat Box. 
public class ChatClient { 

Main a = new Main(); 

/** Chat client access */ 
static class ChatAccess extends Observable { 
    private Socket socket; 
    private OutputStream outputStream; 

    @Override 
    public void notifyObservers(Object arg) { 
     super.setChanged(); 
     super.notifyObservers(arg); 
    } 

    /** Create socket, and receiving thread */ 
    public void InitSocket(String server, int port) throws IOException { 
     socket = new Socket(server, port); 
     outputStream = socket.getOutputStream(); 

     Thread receivingThread = new Thread() { 
      @Override 
      public void run() { 
       try { 
        BufferedReader reader = new BufferedReader(
          new InputStreamReader(socket.getInputStream())); 
        String line; 
        while ((line = reader.readLine()) != null) 
         notifyObservers(line); 
       } catch (IOException ex) { 
        notifyObservers(ex); 
       } 
      } 
     }; 
     receivingThread.start(); 
    } 

    private static final String CRLF = "\r\n"; // newline 

    /** Send a line of text */ 
    public void send(String text) { 
     try { 
      outputStream.write((text + CRLF).getBytes()); 
      outputStream.flush(); 
     } catch (IOException ex) { 
      notifyObservers(ex); 
     } 
    } 

    /** Close the socket */ 
    public void close() { 
     try { 
      socket.close(); 
     } catch (IOException ex) { 
      notifyObservers(ex); 
     } 
    } 
} 

/** Chat client UI */ 
static class ChatFrame extends JFrame implements Observer { 

    private JTextArea textArea; 
    private JTextField inputTextField; 
    private JButton sendButton; 
    private ChatAccess chatAccess; 

    public ChatFrame(ChatAccess chatAccess) { 
     this.chatAccess = chatAccess; 
     chatAccess.addObserver(this); 
     buildGUI(); 
    } 

    /** Builds the user interface */ 
    private void buildGUI() { 
     textArea = new JTextArea(20, 50); 
     textArea.setEditable(false); 
     textArea.setLineWrap(true); 
     add(new JScrollPane(textArea), BorderLayout.CENTER); 

     Box box = Box.createHorizontalBox(); 
     add(box, BorderLayout.SOUTH); 
     inputTextField = new JTextField(); 
     sendButton = new JButton("Send"); 
     box.add(inputTextField); 
     box.add(sendButton); 

     // Action for the inputTextField and the goButton 
     ActionListener sendListener = new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       String str = inputTextField.getText(); 
       if (str != null && str.trim().length() > 0) 
        chatAccess.send(str); 
       inputTextField.selectAll(); 
       inputTextField.requestFocus(); 
       inputTextField.setText(""); 
      } 
     }; 
     inputTextField.addActionListener(sendListener); 
     sendButton.addActionListener(sendListener); 

     this.addWindowListener(new WindowAdapter() { 
      @Override 
      public void windowClosing(WindowEvent e) { 
       chatAccess.close(); 
      } 
     }); 
    } 

    /** Updates the UI depending on the Object argument */ 
    public void update(Observable o, Object arg) { 
     final Object finalArg = arg; 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       textArea.append(finalArg.toString()); 
       textArea.append("\n"); 
      } 
     }); 
    } 
} 

public static void main(String[] args) { 
    System.out.println("troll"); 
    String server = args[0]; 
    System.out.println("reached here"); 
    int port =2222; 
    ChatAccess access = new ChatAccess(); 

    JFrame frame = new ChatFrame(access); 
    frame.setTitle("MyChatApp - connected to " + server + ":" + port); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setResizable(false); 
    frame.setVisible(true); 

    try { 
     access.InitSocket(server,port); 
    } catch (IOException ex) { 
     System.out.println("Cannot connect to " + server + ":" + port); 
     ex.printStackTrace(); 
     System.exit(0); 
    } 
} 
} 

答えて

4

String server = args[0]; 

引数を使ってプログラム

にあなたの最初の引数は、コマンドの配列であるように私には、サーバー名を提供お勧めしたいです渡された行args。

+2

...と 'ArrayIndexOutOfBoundsException:0'は、配列の0番目の要素がないことを暗示しています。これは、プログラマの呼び出し元がコマンドラインパラメータを提供しないことを意味します – k3b