#224: ModelUtil error in JAXBContext.newInstance()

Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
diff --git a/api/src/main/java/jakarta/xml/bind/ContextFinder.java b/api/src/main/java/jakarta/xml/bind/ContextFinder.java
index bbb67f0..15fffa4 100644
--- a/api/src/main/java/jakarta/xml/bind/ContextFinder.java
+++ b/api/src/main/java/jakarta/xml/bind/ContextFinder.java
@@ -302,16 +302,18 @@
         String factoryName = classNameFromSystemProperties();
         if (factoryName != null) return newInstance(contextPath, contextPathClasses, factoryName, classLoader, properties);
 
-        Object factory = properties.get(factoryId);
-        if (factory != null) {
-            if (factory instanceof String)  {
-                factoryName = (String) factory;
-            } else {
-                throw new JAXBException(Messages.format(Messages.ILLEGAL_CAST, factory.getClass().getName(), "String"));
+        if (properties != null) {
+            Object factory = properties.get(factoryId);
+            if (factory != null) {
+                if (factory instanceof String) {
+                    factoryName = (String) factory;
+                } else {
+                    throw new JAXBException(Messages.format(Messages.ILLEGAL_CAST, factory.getClass().getName(), "String"));
+                }
             }
-        }
-        if (factoryName != null) {
-            return newInstance(contextPath, contextPathClasses, factoryName, classLoader, properties);
+            if (factoryName != null) {
+                return newInstance(contextPath, contextPathClasses, factoryName, classLoader, properties);
+            }
         }
 
         JAXBContextFactory obj = ServiceLoaderUtil.firstByServiceLoader(
@@ -344,16 +346,18 @@
         String factoryClassName = classNameFromSystemProperties();
         if (factoryClassName != null) return newInstance(classes, properties, factoryClassName);
 
-        Object ctxFactory = properties.get(JAXBContext.JAXB_CONTEXT_FACTORY);
-        if (ctxFactory != null) {
-            if (ctxFactory instanceof String)  {
-                factoryClassName = (String) ctxFactory;
-            } else {
-                throw new JAXBException(Messages.format(Messages.ILLEGAL_CAST, ctxFactory.getClass().getName(), "String"));
+        if (properties != null) {
+            Object ctxFactory = properties.get(JAXBContext.JAXB_CONTEXT_FACTORY);
+            if (ctxFactory != null) {
+                if (ctxFactory instanceof String) {
+                    factoryClassName = (String) ctxFactory;
+                } else {
+                    throw new JAXBException(Messages.format(Messages.ILLEGAL_CAST, ctxFactory.getClass().getName(), "String"));
+                }
             }
-        }
-        if (factoryClassName != null) {
-            return newInstance(classes, properties, factoryClassName);
+            if (factoryClassName != null) {
+                return newInstance(classes, properties, factoryClassName);
+            }
         }
 
         JAXBContextFactory factory =
diff --git a/api/src/main/java/jakarta/xml/bind/ModuleUtil.java b/api/src/main/java/jakarta/xml/bind/ModuleUtil.java
index af02171..8644464 100644
--- a/api/src/main/java/jakarta/xml/bind/ModuleUtil.java
+++ b/api/src/main/java/jakarta/xml/bind/ModuleUtil.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, 2021 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Distribution License v. 1.0, which is available at
@@ -112,6 +112,14 @@
 
         Module jaxbModule = JAXBContext.class.getModule();
 
+        if (!jaxbModule.isNamed()) {
+            //we are not on the module path, so assume class-path mode
+            if (LOGGER.isLoggable(Level.FINE)) {
+                LOGGER.log(Level.FINE, "Using jakarta.xml.bind-api on the class path.");
+            }
+            return;
+        }
+
         for (Class<?> cls : classes) {
             Class<?> jaxbClass = cls.isArray() ?
                     cls.getComponentType() : cls;