### Question:
GenericSubFieldPackager extends GenericPackager implements ISOSubFieldPackager { @Override public byte[] pack(ISOComponent m) throws ISOException { LogEvent evt = new LogEvent (this, "pack"); try (ByteArrayOutputStream bout = new ByteArrayOutputStream(100)) { ISOComponent c; Map fields = m.getChildren(); if (emitBitMap()) { c = (ISOComponent) fields.get (-1); byte[] b = getBitMapfieldPackager().pack(c); bout.write(b); } for (int i=getFirstField(); i<=m.getMaxField(); i++) { c = (ISOComponent) fields.get (i); if (c == null && !emitBitMap()) c = new ISOField (i, ""); if (c != null) { try { byte[] b = fld[i].pack(c); bout.write(b); } catch (Exception e) { evt.addMessage ("error packing subfield "+i); evt.addMessage (c); evt.addMessage (e); throw e; } } } byte[] d = bout.toByteArray(); if (logger != null) evt.addMessage (ISOUtil.hexString (d)); return d; } catch (ISOException e) { evt.addMessage (e); throw e; } catch (Exception e) { evt.addMessage (e); throw new ISOException (e); } finally { Logger.log(evt); } } GenericSubFieldPackager(); @Override int getFieldNumber(); @Override int unpack(ISOComponent m, byte[] b); @Override byte[] pack(ISOComponent m);  }### Answer:
@Test public void testPackThrowsISOException() throws Throwable { try { new GenericSubFieldPackager().pack(new ISOMsg()); fail("Expected ISOException to be thrown"); } catch (ISOException ex) { if (isJavaVersionAtMost(JAVA_14)) { assertEquals("java.lang.NullPointerException", ex.getMessage(), "ex.getMessage()"); assertNull(ex.getNested().getMessage(), "ex.getNested().getMessage()"); } else { assertEquals("java.lang.NullPointerException: Cannot load from object array because \"this.fld\" is null", ex.getMessage(), "ex.getMessage()"); assertEquals("Cannot load from object array because \"this.fld\" is null", ex.getNested().getMessage(), "ex.getNested().getMessage()"); } } }
@Test public void testPackThrowsISOException1() throws Throwable { try { new GenericSubFieldPackager().pack(null); fail("Expected ISOException to be thrown"); } catch (ISOException ex) { if (isJavaVersionAtMost(JAVA_14)) { assertEquals("java.lang.NullPointerException", ex.getMessage(), "ex.getMessage()"); assertNull(ex.getNested().getMessage(), "ex.getNested().getMessage()"); } else { assertEquals("java.lang.NullPointerException: Cannot invoke \"org.jpos.iso.ISOComponent.getChildren()\" because \"m\" is null", ex.getMessage(), "ex.getMessage()"); assertEquals("Cannot invoke \"org.jpos.iso.ISOComponent.getChildren()\" because \"m\" is null", ex.getNested().getMessage(), "ex.getNested().getMessage()"); } } }
@Test public void testPackThrowsISOException2() throws Throwable { ISOFieldPackager[] fld = new ISOFieldPackager[3]; fld[1] = new IFA_LCHAR(0, "testGenericSubFieldPackagerDescription"); GenericSubFieldPackager genericSubFieldPackager = new GenericSubFieldPackager(); genericSubFieldPackager.setFieldPackager(fld); try { genericSubFieldPackager.pack(new ISOBinaryField(100)); fail("Expected ISOException to be thrown"); } catch (ISOException ex) { assertEquals("org.jpos.iso.IFA_LCHAR: Problem packing field unknown", ex.getMessage(), "ex.getMessage()"); if (isJavaVersionAtMost(JAVA_14)) { assertNull(ex.getNested().getMessage(), "ex.getNested().getMessage()"); } else { assertEquals("Cannot invoke \"org.jpos.iso.ISOComponent.getValue()\" because \"c\" is null", ex.getNested().getMessage(), "ex.getNested().getMessage()"); } } }