Array_3.java 1.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Test to make sure null arrays throw the right execption

public class Array_3
{
  static Object foo ()
  {
    return null;
  }

  static int[] bar ()
  {
    return null;
  }

Andrew Haley committed
15 16 17 18 19 20 21
  static int baz ()
  {
    int[] x = (int[])null;
    int nn = x.length;
    return 5;
  }

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  public static void main(String args[])
  {
    boolean ok = false;
    int nn = 0;

    try
      {
	int[] x = (int[])foo();
	nn = x.length;
      }
    catch (NullPointerException _)
      {
	ok = true;
      }
    if (!ok)
Andrew Haley committed
37
      throw new RuntimeException("test failed:1");
38 39 40 41 42 43 44 45 46 47 48 49

    ok = false;
    try
      {
	int[] x = bar();
	nn = x.length;
      }
    catch (NullPointerException _)
      {
	ok = true;
      }
    if (!ok)
Andrew Haley committed
50
      throw new RuntimeException("test failed:2");
51 52 53 54 55 56 57 58 59 60 61 62 63

    ok = false;
    try
      {
	int[] x = bar();
	nn = x[0];
      }
    catch (NullPointerException _)
      {
	ok = true;
      }

    if (!ok || nn != 0)
Andrew Haley committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77
      throw new RuntimeException("test failed:3");

    ok = false;
    try
      {
	int[] x = (int[])null;
	nn = x.length;
      }
    catch (NullPointerException _)
      {
	ok = true;
      }
    if (!ok)
      throw new RuntimeException("test failed:4");
Andrew Haley committed
78 79 80 81 82 83 84 85 86 87 88 89

    ok = false;
    try
      {
	nn = baz ();
      }
    catch (NullPointerException _)
      {
	ok = true;
      }
    if (!ok)
      throw new RuntimeException("test failed:5");
90 91
  }
}