Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误:java.lang.ArrayIndexOutofBounds异常:1,如何处理_Java_Indexoutofboundsexception - Fatal编程技术网

错误:java.lang.ArrayIndexOutofBounds异常:1,如何处理

错误:java.lang.ArrayIndexOutofBounds异常:1,如何处理,java,indexoutofboundsexception,Java,Indexoutofboundsexception,如何处理发出此消息的代码中的错误 java.lang.ArrayIndexOutOfBoundsException:1 为了使用udp套接字将数据包从客户端发送到服务器 public class DatagramServer { private final static int PACKETSIZE = 100 ; public static void main( String args[] ) { // Check the arguments if( args.length

如何处理发出此消息的代码中的错误 java.lang.ArrayIndexOutOfBoundsException:1

为了使用udp套接字将数据包从客户端发送到服务器

    public class DatagramServer
{
 private final static int PACKETSIZE = 100 ;

public static void main( String args[] )
{
  // Check the arguments
  if( args.length != 0 )
  {
     System.out.println( "usage: DatagramServer port" ) ;
     return ;
  }

  try
  {
     // Convert the argument to ensure that is it valid
     int port = Integer.parseInt( args[1] ) ;

     // Construct the socket
     DatagramSocket socket = new DatagramSocket( port ) ;

     System.out.println( "The server is ready..." ) ;



  }
   }

数组元素从索引0开始,而不是
1

int port = Integer.parseInt( args[0] ) ;  // first argument is args[0]

if
语句更改为检查是否等于,即

if(args.length==0)

或者使用
1
代替
0
,即

if(args.length!=1)


当前代码意味着只有当
args
的长度为
0
时,才能到达
Integer.parseInt()
语句,因此通过寻址
args[0]
可以尝试索引空数组的第一个位置

谢谢你的帮助,但是它一直给我同样的问题谢谢,它工作了,但是我需要它转到try语句,但是它保留在IF语句中如果它在两种情况下都保留在'IF``语句中,那么你就不能通过这个论点。如何调用