Search code examples
javaarraysindexoutofboundsexceptionargs

ArrayIndexOutOfBoundsException when skipping command line argument


I want to know how I can make a program that takes command line arguments work without a command line argument.

It's the last else if-statement that I need help with. How can I accomplish what I'm trying to do here?

P.S I did not find an answer to this when reading the replies on the post this is a "possible duplicate" of.

Here's my code:

import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;

class LesInformasjon{
    public static void main(String[]args) throws Exception{
        Scanner fil = new Scanner(new File("informasjon.txt"));
        ArrayList<Bil> biler = new ArrayList<>();


        while(fil.hasNextLine()){
            String line = fil.nextLine();
            String ord[] = line.split(" ");
            String bilType = ord[0];
            String kjennemerke = ord[1];
            Bil bil = null; 

            //Tester typen bil, lager bil og setter inn i ArrayList
            if(bilType.equals("EL")){
                double batteriKapasitet = Double.parseDouble(ord[2]);
                bil = new Elbil(kjennemerke, bilType, batteriKapasitet);
            }else if(bilType.equals("LASTEBIL")){
                double utslipp = Double.parseDouble(ord[2]);
                double nyttevekt = Double.parseDouble(ord[3]);
                bil  = new Lastebil(kjennemerke,bilType, utslipp, nyttevekt);
            }else if(bilType.equals("PERSONBIL")){
                double utslipp = Double.parseDouble(ord[2]);
                int antGodkjenteSeter = Integer.parseInt(ord[3]);
                bil = new Personbil(kjennemerke, bilType, utslipp, antGodkjenteSeter);
            }

            biler.add(bil);
            }



            if(args[0].equals("EL")){
                for(Bil bil : biler){
                    if(bil instanceof Elbil){
                    //if(bil.bilType.equals("EL")){
                        System.out.println(bil);
                        System.out.println(" ");
                    }
                }

                //System.out.println("Print Elbiler");
            }else if(args[0].equals("FOSSIL")){
                for(Bil bil : biler){
                    if(bil instanceof Fossilbil){
                    //if(bil.bilType.equals("LASTEBIL") || bil.bilType.equals("PERSONBIL")){
                        System.out.println(bil);
                        System.out.println(" ");
                    }
                }
            }else if(args.length == 0){ //tried else if(args[0] == null as well
                for(Bil bil : biler){
                    System.out.println(bil);
                    System.out.println(" ");
                }
            }
    }
}

If you need the other classes, I can give you those. However, they're not needed to answer the question.


Solution

  • Change the order of your if statements. Right now args[1].equals() is getting checked before you check args.length == 0. So when the array is empty, the first call will throw an exception. If you check the length first, this will be solved.

    Change this structure:

    if(args[0].equals("EL")){
    
    }else if(args[0].equals("FOSSIL")){
    
    }else if(args.length == 0){
    
    }
    

    To this:

    if(args.length == 0){
    
    }else if(args[0].equals("FOSSIL")){
    
    }else if(args[0].equals("EL")){
    
    }