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

StackOverflow https://stackoverflow.com/questions/19930278

  •  30-07-2022
  •  | 
  •  

Question

I am developing a simple Java Swing application and I have a stupid doubts about main() method args input paramether:

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)

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.

The problem is that when I try to execute this class I obtain the following error message in the Eclipse console:

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

Andrea

Was it helpful?

Solution

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). If you do want to call launch() in this case, do the following:

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

OTHER TIPS

Apparently you run your code without any arguments.

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

will be impossible to check, so there's your ArrayIndexOutOfBoundsException

Check if there are any parameters, in order to avoid such a errors, simply do:

if(args.length > 0)

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

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. You're not passing any arguments to the program, and the array is empty, causing the OOB.

Test that args.length > 0 before dereferencing the first element, and you'll be fine.

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.

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.

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. Otherwise write two if blocks if you want a different action for a null argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top