简体   繁体   English

java.lang.ArrayIndexOutOfBoundsException:0,当我尝试访问args参数的第一个参数时,为什么?

[英]java.lang.ArrayIndexOutOfBoundsException: 0 when I try to access to the first argument of args parameters, why?

I am developing a simple Java Swing application and I have a stupid doubts about main() method args input paramether: 我正在开发一个简单的Java Swing应用程序,并对main()方法的args输入参数感到愚蠢:

I have the following code: 我有以下代码:

package com.test.login;

import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JFrame;

import org.jdesktop.application.SingleFrameApplication;

public class MainWindows extends SingleFrameApplication {

    private static final int FIXED_WIDTH = 880;
    private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 440);

    // First execute the LoginFrame class to open the login windows:
    public static void main(String[] args) {
        System.out.println("Inside: MainWindows() ---> main()");

        if(!(args[0].equals("loggedIn"))){
            launch(LoginFrame.class, args);
        }

    }

    @Override
    protected void startup() {
        // TODO Auto-generated method stub

        System.out.println("Inside MainWindows ---> startup()");


        JFrame mainFrame = this.getMainFrame();         // main JFrame that represents the Windows
        mainFrame.setTitle("My Appliction MainFrame");

        mainFrame.setPreferredSize(INITAL_SIZE);
        mainFrame.setResizable(false);

        show(mainFrame);

    }

}

The main() method take the classic args[] array parameters (that is an array of String) main()方法采用经典的args []数组参数(即String的数组)

I would that if the first element in this array IS NOT the String loggedIn it launch the LoginFrame.class otherwise do nothint and the startUp() method that render a JFrame windows will automatically called. 我想,如果这个数组中的第一个元素是不是字符串的loggedIn它推出LoginFrame.class否则做nothint和使得一个JFrame窗口会自动调用启动()方法。

The problem is that when I try to execute this class I obtain the following error message in the Eclipse console: 问题是,当我尝试执行此类时,我在Eclipse控制台中获得以下错误消息:

Inside: MainWindows() ---> main()
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at com.test.login.MainWindows.main(MainWindows.java:19)

Why? 为什么? Where is the problem? 问题出在哪儿? How cal I resolve? 我如何解决?

Tnx TNX

Andrea 安德里亚

You aren't running this with any command line parameters, are you? 您没有使用任何命令行参数来运行它,是吗? Verify that you actually have a first parameter before you try performing checks on it. 在尝试对其进行检查之前,请验证您是否确实具有第一个参数。 For example: 例如:

if(args.length > 0 && !args[0].equals("loggedIn")){

The above implies you don't want to call launch() if there are no parameters (your question is a bit vague on this, but this would be a strict interpretation). 上面的内容暗示了如果没有参数,您不想调用launch() (您的问题对此有点含糊,但这是严格的解释)。 If you do want to call launch() in this case, do the following: 如果确实要在这种情况下调用launch() ,请执行以下操作:

if(!(args.length > 0 && args[0].equals("loggedIn"))){

Here is the problem 这是问题所在

 if(!(args[0].equals("loggedIn"))) 

You need to pass the command line argument. 您需要传递命令行参数。

or 要么

you can do like this 你可以这样

 if(args.length>0&&!("loggedIn".equals(args[0]))){
            launch(LoginFrame.class, args);
 }

Apparently you run your code without any arguments. 显然,您运行的代码没有任何参数。

args[0].equals("loggedIn"))

will be impossible to check, so there's your ArrayIndexOutOfBoundsException 将无法检查,因此存在您的ArrayIndexOutOfBoundsException

Check if there are any parameters, in order to avoid such a errors, simply do: 检查是否有任何参数,为避免此类错误,只需执行以下操作:

if(args.length > 0)

In C/C++ where the main() construct is "borrowed from", the first argument is always the program itself (and thus you're sure of one element in the array), but this is not true in Java - and this is your problem. main()构造是“借来”的C / C ++中,第一个参数始终是程序本身(因此您可以确定数组中有一个元素),但是在Java中不是这样-这是你的问题。 You're not passing any arguments to the program, and the array is empty, causing the OOB. 您没有将任何参数传递给程序,并且该数组为空,从而导致OOB。

Test that args.length > 0 before dereferencing the first element, and you'll be fine. 在取消引用第一个元素之前测试args.length > 0就可以了。

Cheers, 干杯,

Remember that arrays of length 0 do not have anything in them, therefore there is no index 0. You need to test for length first before testing for the value of a particular index. 请记住,长度为0的数组中没有任何内容,因此没有索引0。在测试特定索引的值之前,需要先测试长度。

String[] args = null;

if(args.length > 0) {  // <- NullPointerException: args is null.
       .....
}

args = new String[0];
if("nope".equals(args[0])){ // <- IndexOutOfBoundsException: 'args' does not have 
                            //                               anything at index 0
    ....
}

args = new String[3];
if("nope".equals(args[0])){ // <- Good to go, but...
           .....
}
if(args != null && args.length > 0){ // <- Safest bet
    if("nope".equals(args[0])){ .. } // Now check
}

Looks like your args array is empty, hence you're passing a null to your main method. 看起来您的args数组为空,因此您要将null传递给main方法。

Instead of checking 而不是检查

if(!(args[0].equals("loggedIn")))

First do: 首先要做的是:

if(args==null || !(args[0].equals("loggedIn")))

So you can see if the argument was null (in that case the second condition won't be calculated) and execute the same login action. 因此,您可以查看参数是否为null(在这种情况下将不计算第二个条件),并执行相同的登录操作。 Otherwise write two if blocks if you want a different action for a null argument. 否则,如果要对null参数执行不同的操作,请编写两个if块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM