我正在使用Netbeans IDE,如何解决;java.lang.ArrayIndexOutOfBoundsException“;?
代码: 错误: 线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常异常:1 位于com.server.Client.main(Client.java:47)`我正在使用Netbeans IDE,如何解决;java.lang.ArrayIndexOutOfBoundsException“;?,java,netbeans,Java,Netbeans,代码: 错误: 线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常异常:1 位于com.server.Client.main(Client.java:47)` 运行程序时,您需要向它传递(至少)2个参数。 您的问题是这个args[1]不存在,这是因为您仅在运行程序时传递1个参数。因此,您正在访问元素args[1],但是args数组只包含1个元素(args[0]),而不是2个(args[0]和args[1]) 启动java应用程序时没有传递任
运行程序时,您需要向它传递(至少)2个参数。
您的问题是这个
args[1]
不存在,这是因为您仅在运行程序时传递1个参数。因此,您正在访问元素args[1]
,但是args
数组只包含1个元素(args[0]
),而不是2个(args[0]
和args[1]
) 启动java应用程序时没有传递任何参数。@Eng.Fouad似乎OP传递了1个参数。@peter.petrov您是对的。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.server;
import java.awt.*;
import java.io.*;
import java.net.Socket;
public class Client {
private final ObjectOutputStream out;
private final ObjectInputStream in;
private final Robot robot;
public Client(String serverMachine,String clientname)
throws IOException,AWTException {
Socket socket = new Socket("servermachine",port);
robot = new Robot();
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(
new BufferedInputStream(socket.getInputStream()));
out.writeObject(clientname);
out.flush();
}
public void run() throws ClassNotFoundException{
try{
while(true){
RobotAction action = (RobotAction) in.readObject();
Object result = action.execute(robot);
if(result !=null)
{
out.writeObject(result);
out.flush();
out.reset();
}
}
}
catch(IOException ex){
System.out.println("Connection closed");
}
}
public static void main(String[] args) throws Exception{
Client client = new Client(args[0], args[1]);
client.run();
}
}