0

Basically I am just writing a socket. For some reason I keep getting this error though

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Main.ChatClient.main(ChatClient.java:143)

which is line " String server = args[0]; "

What does the args need to be to fix this issue?

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

1 Answer 1

4

Given:

String server = args[0]; 

I'd suggest you provide the server name as your first argument to your program

With

public static void main(String[] args) 

args is the array of command line args passed in.

Sign up to request clarification or add additional context in comments.

1 Comment

... and ArrayIndexOutOfBoundsException: 0 indecate that there is no 0th element in the array which means the caller of your programm does not provide a commandline paramter

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.