-4

This is part of a bigger project but i am having a problem with something fairly basic. I keep getting an array out of bounds exception. Can you advise why?

public class Arrayif {

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, ParseException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException, InterruptedException {

    String[] strarray = new String[0];

    String liveBoA = "test";

    if (strarray[0].isEmpty()) {
        strarray[0] = liveBoA;
        System.out.println("hello");
    } else if (strarray[0].contains(liveBoA)) {

        System.out.println("bellow");

    }
}

}

This doesnt work either:

public class Arrayif {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, ParseException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException, InterruptedException {

        String[] strarray = new String[1];

        String liveBoA = "test";

        if (strarray[0].isEmpty()) {
            strarray[0] = liveBoA;
            System.out.println("hello");
        } else if (strarray[0].contains(liveBoA)) {

            System.out.println("bellow");

        }
    }
}
Ingram
  • 654
  • 2
  • 7
  • 29
  • Your array has length zero. It has space for no elements. That means `[0]` is outside the array's bounds. – khelwood Apr 27 '19 at 10:26

1 Answers1

0

String[] strarray = new String[0]; will create empty array.

You need change to String[] strarray = new String[1];

or add strarray.length > 0 to if condition if (strarray.length > 0 && strarray[0].isEmpty()) to prevent array out of bounds exception

Update: it throw null pointer exception if you did init array.

String[] strarray = new String[1];
strarray[0] = "Your string";

If you dont want to init at first time, you should check null before use it before isEmpty() and contains()

public static boolean isNullOrEmpty(String str) {
        return str==null || str.isEmpty();
 }
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • Buyt this causes a null point exception also public class Arrayif { public static void main(String[] args) { String[] strarray = new String[1]; String liveBoA = "test"; if (strarray[0].isEmpty()) { strarray[0] = liveBoA; System.out.println("hello"); } else if (strarray[0].contains(liveBoA)) { System.out.println("bellow"); } } } – Ingram Apr 27 '19 at 10:42
  • you need init your array to use it as updated answer – Hien Nguyen Apr 27 '19 at 10:45
  • But what i aim to is create an array. Test if the postion in the array contains the value then if it is empty put in the String in the index position. As this is running on scheduled loop on the second time it go straight to the else if part. I want it to start off empty and then populate. Can you advise? – Ingram Apr 27 '19 at 10:48
  • You are confusing "the array" and "what is in the array". ```array = new String[1]``` is an array with one slot that is not filled. You fill it by ```array[0] = new String()```. Until filled, ```array[0] == null```. –  Apr 27 '19 at 10:51
  • you need add strarray[0] != null before using any string method .isEmpty() or contains() – Hien Nguyen Apr 27 '19 at 10:55
  • @Ingram "But what i aim to is create an array" and that is what `new String[size]` does, it creates an array, but that array is container *for* elements, not *with* elements (even empty ones like `""`). Byt default array of objects like Strings is filled with `null`s not `""`, otherwise people would expect that array like `new File[size]` should also contain some instances of `File` class, but what instances should it hold (and please remember that `File` is immutable, you can't modify its state, so you can't change what location it will represent). Only save default common value here is null. – Pshemo Apr 27 '19 at 11:09
  • 3
    @HienNguyen `if(str != null && !str.isEmpty()) return false; return true;` can be shortened to `return str==null || str.isEmpty();` which also IMO better fits `isNullOrEmpty` description :) – Pshemo Apr 27 '19 at 11:21
  • @Pshemo I updated answer base on your suggestion – Hien Nguyen Apr 27 '19 at 23:28