< prev index next >

test/hotspot/jtreg/runtime/exceptionMsgs/NullPointerException/NullPointerExceptionTest.java

Print this page

 288                          (hasDebugInfo ? "\"ia1\"" : "\"<local1>\"") + " is null");
 289         }
 290         // fastore
 291         try {
 292             fa1[0] = 0.7f;
 293             System.out.println(fa1[0]);
 294             Asserts.fail();
 295         } catch (NullPointerException e) {
 296             checkMessage(e, "fa1[0] = 0.7f;", e.getMessage(),
 297                          "Cannot store to float array because " +
 298                          (hasDebugInfo ? "\"fa1\"" : "\"<local2>\"") + " is null");
 299         }
 300         // aastore
 301         try {
 302             oa1[0] = new Object();
 303             System.out.println(oa1[0]);
 304             Asserts.fail();
 305         } catch (NullPointerException e) {
 306             checkMessage(e, "oa1[0] = new Object();", e.getMessage(),
 307                          "Cannot store to object array because " +
 308                          (hasDebugInfo ? "\"oa1\"" : "\"<local3>\"") + " is null");
 309         }
 310         // bastore (boolean)
 311         try {
 312             za1[0] = false;
 313             System.out.println(za1[0]);
 314             Asserts.fail();
 315         } catch (NullPointerException e) {
 316             checkMessage(e, "za1[0] = false;", e.getMessage(),
 317                          "Cannot store to byte/boolean array because " +
 318                          (hasDebugInfo ? "\"za1\"" : "\"<local4>\"") + " is null");
 319         }
 320         // bastore (byte)
 321         try {
 322             ba1[0] = 0;
 323             System.out.println(ba1[0]);
 324             Asserts.fail();
 325         } catch (NullPointerException e) {
 326             checkMessage(e, "ba1[0] = 0;", e.getMessage(),
 327                          "Cannot store to byte/boolean array because " +
 328                          (hasDebugInfo ? "\"ba1\"" : "\"<local5>\"") + " is null");

1349                          (hasDebugInfo ? "\"f2\"" : "\"<local7>\"") + " is null");
1350             e2 = e;
1351         }
1352         e1.setStackTrace(e2.getStackTrace());
1353         checkMessage(e1, "f1.i = 18;", e1.getMessage(),
1354                      "Cannot assign field \"i\" because " +
1355                      (hasDebugInfo ? "\"f1\"" : "\"<local6>\"") + " is null");
1356         checkMessage(e2, "f1.i = 18;", e2.getMessage(),
1357                      "Cannot assign field \"i\" because " +
1358                      (hasDebugInfo ? "\"f2\"" : "\"<local7>\"") + " is null");
1359 
1360         // If created via reflection, the message should not be generated.
1361         ex = NullPointerException.class.getDeclaredConstructor().newInstance();
1362         Asserts.assertNull(ex.getMessage());
1363     }
1364 
1365     public void testNative() throws Exception {
1366         // If NPE is thrown in a native method, the message should
1367         // not be generated.
1368         try {
1369             Array.get(null,0);
1370             Asserts.fail();
1371         } catch (NullPointerException e) {
1372             Asserts.assertNull(e.getMessage());
1373         }
1374     }
1375 
1376     // Test we get the same message calling npe.getMessage() twice.
1377     @SuppressWarnings("null")
1378     public void testSameMessage() throws Exception {
1379         Object null_o = null;
1380         String expectedMsg =
1381             "Cannot invoke \"Object.hashCode()\" because " +
1382             (hasDebugInfo ? "\"null_o" : "\"<local1>") + "\" is null";
1383 
1384         try {
1385             null_o.hashCode();
1386             Asserts.fail();
1387         } catch (NullPointerException e) {
1388             String msg1 = e.getMessage();
1389             checkMessage(e, "null_o.hashCode()", msg1, expectedMsg);

 288                          (hasDebugInfo ? "\"ia1\"" : "\"<local1>\"") + " is null");
 289         }
 290         // fastore
 291         try {
 292             fa1[0] = 0.7f;
 293             System.out.println(fa1[0]);
 294             Asserts.fail();
 295         } catch (NullPointerException e) {
 296             checkMessage(e, "fa1[0] = 0.7f;", e.getMessage(),
 297                          "Cannot store to float array because " +
 298                          (hasDebugInfo ? "\"fa1\"" : "\"<local2>\"") + " is null");
 299         }
 300         // aastore
 301         try {
 302             oa1[0] = new Object();
 303             System.out.println(oa1[0]);
 304             Asserts.fail();
 305         } catch (NullPointerException e) {
 306             checkMessage(e, "oa1[0] = new Object();", e.getMessage(),
 307                          "Cannot store to object array because " +
 308                          (hasDebugInfo ? "\"oa1\"" : "\"<local3>\"") + " is null or is a null-free array and there's an attempt to store null in it");
 309         }
 310         // bastore (boolean)
 311         try {
 312             za1[0] = false;
 313             System.out.println(za1[0]);
 314             Asserts.fail();
 315         } catch (NullPointerException e) {
 316             checkMessage(e, "za1[0] = false;", e.getMessage(),
 317                          "Cannot store to byte/boolean array because " +
 318                          (hasDebugInfo ? "\"za1\"" : "\"<local4>\"") + " is null");
 319         }
 320         // bastore (byte)
 321         try {
 322             ba1[0] = 0;
 323             System.out.println(ba1[0]);
 324             Asserts.fail();
 325         } catch (NullPointerException e) {
 326             checkMessage(e, "ba1[0] = 0;", e.getMessage(),
 327                          "Cannot store to byte/boolean array because " +
 328                          (hasDebugInfo ? "\"ba1\"" : "\"<local5>\"") + " is null");

1349                          (hasDebugInfo ? "\"f2\"" : "\"<local7>\"") + " is null");
1350             e2 = e;
1351         }
1352         e1.setStackTrace(e2.getStackTrace());
1353         checkMessage(e1, "f1.i = 18;", e1.getMessage(),
1354                      "Cannot assign field \"i\" because " +
1355                      (hasDebugInfo ? "\"f1\"" : "\"<local6>\"") + " is null");
1356         checkMessage(e2, "f1.i = 18;", e2.getMessage(),
1357                      "Cannot assign field \"i\" because " +
1358                      (hasDebugInfo ? "\"f2\"" : "\"<local7>\"") + " is null");
1359 
1360         // If created via reflection, the message should not be generated.
1361         ex = NullPointerException.class.getDeclaredConstructor().newInstance();
1362         Asserts.assertNull(ex.getMessage());
1363     }
1364 
1365     public void testNative() throws Exception {
1366         // If NPE is thrown in a native method, the message should
1367         // not be generated.
1368         try {
1369             Array.getLength(null);
1370             Asserts.fail();
1371         } catch (NullPointerException e) {
1372             Asserts.assertNull(e.getMessage());
1373         }
1374     }
1375 
1376     // Test we get the same message calling npe.getMessage() twice.
1377     @SuppressWarnings("null")
1378     public void testSameMessage() throws Exception {
1379         Object null_o = null;
1380         String expectedMsg =
1381             "Cannot invoke \"Object.hashCode()\" because " +
1382             (hasDebugInfo ? "\"null_o" : "\"<local1>") + "\" is null";
1383 
1384         try {
1385             null_o.hashCode();
1386             Asserts.fail();
1387         } catch (NullPointerException e) {
1388             String msg1 = e.getMessage();
1389             checkMessage(e, "null_o.hashCode()", msg1, expectedMsg);
< prev index next >