提问人:anie 提问时间:5/7/2021 最后编辑:user207421anie 更新时间:5/7/2021 访问量:788
线程“main”java.lang.ArrayIndexOutOfBoundsException 中的异常:索引 0 超出长度 0 的边界,具有 RMI
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 with RMI
问:
我有三个java文件,一个是RMI服务器和RMI客户端和接口文件,如下所示: 服务器:
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
public class RmiServer extends
java.rmi.server.UnicastRemoteObject implements ReceiveMessageInterface{
String address;
Registry registry;
public void receiveMessage(String x) throws RemoteException{
System.out.println(x);
}
public RmiServer() throws RemoteException{
try{
address = (InetAddress.getLocalHost()).toString();
}
catch(Exception e){
System.out.println("can't get inet address.");
}
int port=3233;
System.out.println("this address=" + address + ",port=" + port);
try{
registry = LocateRegistry.createRegistry(port);
registry.rebind("rmiServer", this);
}
catch(RemoteException e){
throw e;
}
}
static public void main(String args[]){
try{
RmiServer s = new RmiServer();
}
catch (Exception e){
e.printStackTrace();
System.exit(1);
}
}
}
客户:
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;
public class RmiClient{
static public void main(String args[]){
ReceiveMessageInterface rmiServer;
Registry registry;
String serverAddress=args[0];
String serverPort=args[1];
String text=args[2];
System.out.println
("sending " + text + " to " +serverAddress + ":" + serverPort);
try{
registry=LocateRegistry.getRegistry
(serverAddress,(new Integer(serverPort)).intValue());
rmiServer=(ReceiveMessageInterface)(registry.lookup("rmiServer"));
// call the remote method
rmiServer.receiveMessage(text);
}
catch(RemoteException e){
e.printStackTrace();
}
catch(NotBoundException e){
e.printStackTrace();
}
}
}
接口:
import java.rmi.*;
public interface ReceiveMessageInterface extends Remote{
void receiveMessage(String x) throws RemoteException;
}
所以基本上,当我运行服务器时,它会给我的笔记本电脑地址和正在运行的端口,这运行得很好 但是,问题是当我在运行服务器后运行客户端时,它不断抛出此错误:
线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常: 索引 0 超出长度 0 的边界
哪个客户端文件在哪一行:
String serverAddress=args[0];
答:
0赞
Bishnu Das
5/7/2021
#1
由于没有传递给 main 方法的参数值,这就是它抛出错误的原因。
我相信您没有将命令行参数分配给 main 方法。 如果使用任何 IDE 来运行项目,请搜索如何在项目执行期间传递参数。
在这里,您可以找到如何配置 Netbeans IDE 的参数 - Netbeans 如何在 Java 中设置命令行参数
如果您使用的是任何其他 IDE,请进行类似的搜索。
否则,只需通过命令提示符运行即可向程序提供命令行参数。
评论
0赞
Stultuske
5/7/2021
如前所述,OP 使用 netbeans,并且不知道什么是命令行参数。这意味着仅仅在 netbeans 中填充它们是一个坏主意,因为一旦他尝试在 netbeans 之外执行它,问题就会再次出现
1赞
Lahiru Wijesekara
5/7/2021
#2
如果你看一下“static public void main(String args[])”方法,String args[] 是一个标准约定。当您以您使用的方式调用程序时,您应该在命令行中将值传递给此数组。
异常的原因是,您的数组中没有 args[0],因为您没有传递值。请确保在调用程序时通过命令行传递三个值,因为您已访问程序中的 args[0]、args[1] 和 args[2]。
例:
public class X{
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}
如何运行
java X a ab abc
预期成果
一个
血型
美国广播公司
评论
0赞
Lahiru Wijesekara
5/7/2021
参考说明将帮助您理清problem.@Stultuske
0赞
Stultuske
5/7/2021
由于他通过在 NetBeans 中右键单击来运行代码,因此这并不能真正解决 IMO 问题。
评论