title: How to fix nullPointerException in newInstance of JAXB Context
body: <p>I am trying to unmarshall my XML document into some java objects. While calling the newInstance methode of JAXBContext I am getting a NullPointerException. I am using JDK 1.8.</p>
  
  <p>This is my error:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.NullPointerException     at
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)     at
    javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)  at
    javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)  at
    javax.xml.bind.ContextFinder.find(ContextFinder.java:390)     at
    javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)  at
    javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)  at
    de.bwb.TestOrgCompare.main(TestOrgCompare.java:58)</p>
  </blockquote>
  
  <p>I have an jaxb.properties file in my object package with following content:
  javax.xml.bind.context.factory=javax.xml.bind.JAXBContextFactory</p>
  
  <p>I have imported 2 external jars: jaxb-api.jar and jaxb-impl-2.3.0.jar and put them into a lib folder in my project.</p>
  
  <p>I already tried to use org.eclipse.persistence.jaxb.JAXBContextFactory in my jaxb.properties file and imported org.eclipse.persistence.moxy.jar but i got the same NullPointerException.</p>
  
  <p>Here is my code snippet:</p>
  
  <pre><code>            [...]
              javax.xml.bind.JAXBContext jaxbContext;
              DTORGANISATIONSYST dtorganisationSyst = null;
              try {
                  //the following line is line 58: where the error appears
                  jaxbContext = javax.xml.bind.JAXBContext.newInstance(DTORGANISATIONSYST.class);
                  javax.xml.bind.Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                  //Overloaded methods to unmarshal from different xml sources
                  dtorganisationSyst = (DTORGANISATIONSYST) jaxbUnmarshaller.unmarshal( new StringReader(xmlString) );
  
              } catch (JAXBException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }   
             [...]
  </code></pre>
  
  <p>This is my DTORGANISATIONSYST Object:</p>
  
  <pre><code>    //
  // Diese Datei wurde mit der JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 generiert 
  // Siehe &lt;a href="http://java.sun.com/xml/jaxb"&gt;http://java.sun.com/xml/jaxb&lt;/a&gt; 
  // Änderungen an dieser Datei gehen bei einer Neukompilierung des Quellschemas verloren. 
  // Generiert: 2019.07.19 um 12:43:25 PM CEST 
  //
  
  
  package de.bwb.bant.objects;
  
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.XmlType;
  
  
  /**
   * &lt;p&gt;Java-Klasse für DT_ORGANISATION_SYST complex type.
   * 
   * &lt;p&gt;Das folgende Schemafragment gibt den erwarteten Content an, der in dieser Klasse enthalten ist.
   * 
   * &lt;pre&gt;
   * &amp;lt;complexType name="DT_ORGANISATION_SYST"&gt;
   *   &amp;lt;complexContent&gt;
   *     &amp;lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
   *       &amp;lt;sequence&gt;
   *         &amp;lt;element name="HCM" type="{urn:bwb.de:ps:bant}DT_ORGANISATION_BASE" minOccurs="0"/&gt;
   *         &amp;lt;element name="BA" type="{urn:bwb.de:ps:bant}DT_ORGANISATION_BASE" minOccurs="0"/&gt;
   *       &amp;lt;/sequence&gt;
   *     &amp;lt;/restriction&gt;
   *   &amp;lt;/complexContent&gt;
   * &amp;lt;/complexType&gt;
   * &lt;/pre&gt;
   * 
   * 
   */
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "DT_ORGANISATION_SYST", propOrder = {
      "hcm",
      "ba"
  })
  @XmlRootElement(name="MT_ORGANISATION_SYST")
  public class DTORGANISATIONSYST {
  
      @XmlElement(name = "HCM")
      protected DTORGANISATIONBASE hcm;
      @XmlElement(name = "BA")
      protected DTORGANISATIONBASE ba;
  
      /**
       * Ruft den Wert der hcm-Eigenschaft ab.
       * 
       * @return
       *     possible object is
       *     {@link DTORGANISATIONBASE }
       *     
       */
      public DTORGANISATIONBASE getHCM() {
          return hcm;
      }
  
      /**
       * Legt den Wert der hcm-Eigenschaft fest.
       * 
       * @param value
       *     allowed object is
       *     {@link DTORGANISATIONBASE }
       *     
       */
      public void setHCM(DTORGANISATIONBASE value) {
          this.hcm = value;
      }
  
      /**
       * Ruft den Wert der ba-Eigenschaft ab.
       * 
       * @return
       *     possible object is
       *     {@link DTORGANISATIONBASE }
       *     
       */
      public DTORGANISATIONBASE getBA() {
          return ba;
      }
  
      /**
       * Legt den Wert der ba-Eigenschaft fest.
       * 
       * @param value
       *     allowed object is
       *     {@link DTORGANISATIONBASE }
       *     
       */
      public void setBA(DTORGANISATIONBASE value) {
          this.ba = value;
      }
  
  }
  </code></pre>
  
  <p>And this is my DTORGANISATIONBASE Class:</p>
  
  <pre><code>    //
  // Diese Datei wurde mit der JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 generiert 
  // Siehe &lt;a href="http://java.sun.com/xml/jaxb"&gt;http://java.sun.com/xml/jaxb&lt;/a&gt; 
  // Änderungen an dieser Datei gehen bei einer Neukompilierung des Quellschemas verloren. 
  // Generiert: 2019.07.21 um 10:27:45 AM CEST 
  //
  
  
  package de.bwb.bant.objects;
  
  import java.math.BigInteger;
  import java.util.ArrayList;
  import java.util.List;
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlType;
  
  
  /**
   * &lt;p&gt;Java-Klasse für DT_ORGANISATION_BASE complex type.
   * 
   * &lt;p&gt;Das folgende Schemafragment gibt den erwarteten Content an, der in dieser Klasse enthalten ist.
   * 
   * &lt;pre&gt;
   * &amp;lt;complexType name="DT_ORGANISATION_BASE"&gt;
   *   &amp;lt;complexContent&gt;
   *     &amp;lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
   *       &amp;lt;sequence&gt;
   *         &amp;lt;element name="SESSION_ID" type="{urn:bwb.de:ps:bant}DT_SESSION_ID_BASE" minOccurs="0"/&gt;
   *         &amp;lt;element name="ITEM" maxOccurs="unbounded"&gt;
   *           &amp;lt;complexType&gt;
   *             &amp;lt;complexContent&gt;
   *               &amp;lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
   *                 &amp;lt;sequence&gt;
   *                   &amp;lt;element name="ORG_HCM_ID" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
   *                   &amp;lt;element name="ORG_BA_ID" type="{http://www.w3.org/2001/XMLSchema}integer" minOccurs="0"/&gt;
   *                   &amp;lt;element name="PAR_HCM_ID" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
   *                   &amp;lt;element name="PAR_BA_ID" type="{http://www.w3.org/2001/XMLSchema}integer" minOccurs="0"/&gt;
   *                   &amp;lt;element name="DESCR" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
   *                   &amp;lt;element name="ACTIV" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
   *                 &amp;lt;/sequence&gt;
   *               &amp;lt;/restriction&gt;
   *             &amp;lt;/complexContent&gt;
   *           &amp;lt;/complexType&gt;
   *         &amp;lt;/element&gt;
   *       &amp;lt;/sequence&gt;
   *     &amp;lt;/restriction&gt;
   *   &amp;lt;/complexContent&gt;
   * &amp;lt;/complexType&gt;
   * &lt;/pre&gt;
   * 
   * 
   */
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "DT_ORGANISATION_BASE", propOrder = {
      "sessionid",
      "item"
  })
  public class DTORGANISATIONBASE {
  
      @XmlElement(name = "SESSION_ID")
      protected String sessionid;
      @XmlElement(name = "ITEM", required = true)
      protected List&lt;DTORGANISATIONBASE.ITEM&gt; item;
  
      /**
       * Ruft den Wert der sessionid-Eigenschaft ab.
       * 
       * @return
       *     possible object is
       *     {@link String }
       *     
       */
      public String getSESSIONID() {
          return sessionid;
      }
  
      /**
       * Legt den Wert der sessionid-Eigenschaft fest.
       * 
       * @param value
       *     allowed object is
       *     {@link String }
       *     
       */
      public void setSESSIONID(String value) {
          this.sessionid = value;
      }
  
      /**
       * Gets the value of the item property.
       * 
       * &lt;p&gt;
       * This accessor method returns a reference to the live list,
       * not a snapshot. Therefore any modification you make to the
       * returned list will be present inside the JAXB object.
       * This is why there is not a &lt;CODE&gt;set&lt;/CODE&gt; method for the item property.
       * 
       * &lt;p&gt;
       * For example, to add a new item, do as follows:
       * &lt;pre&gt;
       *    getITEM().add(newItem);
       * &lt;/pre&gt;
       * 
       * 
       * &lt;p&gt;
       * Objects of the following type(s) are allowed in the list
       * {@link DTORGANISATIONBASE.ITEM }
       * 
       * 
       */
      public List&lt;DTORGANISATIONBASE.ITEM&gt; getITEM() {
          if (item == null) {
              item = new ArrayList&lt;DTORGANISATIONBASE.ITEM&gt;();
          }
          return this.item;
      }
  
  
      /**
       * &lt;p&gt;Java-Klasse für anonymous complex type.
       * 
       * &lt;p&gt;Das folgende Schemafragment gibt den erwarteten Content an, der in dieser Klasse enthalten ist.
       * 
       * &lt;pre&gt;
       * &amp;lt;complexType&gt;
       *   &amp;lt;complexContent&gt;
       *     &amp;lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
       *       &amp;lt;sequence&gt;
       *         &amp;lt;element name="ORG_HCM_ID" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
       *         &amp;lt;element name="ORG_BA_ID" type="{http://www.w3.org/2001/XMLSchema}integer" minOccurs="0"/&gt;
       *         &amp;lt;element name="PAR_HCM_ID" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
       *         &amp;lt;element name="PAR_BA_ID" type="{http://www.w3.org/2001/XMLSchema}integer" minOccurs="0"/&gt;
       *         &amp;lt;element name="DESCR" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
       *         &amp;lt;element name="ACTIV" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
       *       &amp;lt;/sequence&gt;
       *     &amp;lt;/restriction&gt;
       *   &amp;lt;/complexContent&gt;
       * &amp;lt;/complexType&gt;
       * &lt;/pre&gt;
       * 
       * 
       */
      @XmlAccessorType(XmlAccessType.FIELD)
      @XmlType(name = "", propOrder = {
          "orghcmid",
          "orgbaid",
          "parhcmid",
          "parbaid",
          "descr",
          "activ"
      })
      public static class ITEM {
  
          @XmlElement(name = "ORG_HCM_ID")
          protected String orghcmid;
          @XmlElement(name = "ORG_BA_ID")
          protected BigInteger orgbaid;
          @XmlElement(name = "PAR_HCM_ID")
          protected String parhcmid;
          @XmlElement(name = "PAR_BA_ID")
          protected BigInteger parbaid;
          @XmlElement(name = "DESCR")
          protected String descr;
          @XmlElement(name = "ACTIV")
          protected String activ;
  
          /**
           * Ruft den Wert der orghcmid-Eigenschaft ab.
           * 
           * @return
           *     possible object is
           *     {@link String }
           *     
           */
          public String getORGHCMID() {
              return orghcmid;
          }
  
          /**
           * Legt den Wert der orghcmid-Eigenschaft fest.
           * 
           * @param value
           *     allowed object is
           *     {@link String }
           *     
           */
          public void setORGHCMID(String value) {
              this.orghcmid = value;
          }
  
          /**
           * Ruft den Wert der orgbaid-Eigenschaft ab.
           * 
           * @return
           *     possible object is
           *     {@link BigInteger }
           *     
           */
          public BigInteger getORGBAID() {
              return orgbaid;
          }
  
          /**
           * Legt den Wert der orgbaid-Eigenschaft fest.
           * 
           * @param value
           *     allowed object is
           *     {@link BigInteger }
           *     
           */
          public void setORGBAID(BigInteger value) {
              this.orgbaid = value;
          }
  
          /**
           * Ruft den Wert der parhcmid-Eigenschaft ab.
           * 
           * @return
           *     possible object is
           *     {@link String }
           *     
           */
          public String getPARHCMID() {
              return parhcmid;
          }
  
          /**
           * Legt den Wert der parhcmid-Eigenschaft fest.
           * 
           * @param value
           *     allowed object is
           *     {@link String }
           *     
           */
          public void setPARHCMID(String value) {
              this.parhcmid = value;
          }
  
          /**
           * Ruft den Wert der parbaid-Eigenschaft ab.
           * 
           * @return
           *     possible object is
           *     {@link BigInteger }
           *     
           */
          public BigInteger getPARBAID() {
              return parbaid;
          }
  
          /**
           * Legt den Wert der parbaid-Eigenschaft fest.
           * 
           * @param value
           *     allowed object is
           *     {@link BigInteger }
           *     
           */
          public void setPARBAID(BigInteger value) {
              this.parbaid = value;
          }
  
          /**
           * Ruft den Wert der descr-Eigenschaft ab.
           * 
           * @return
           *     possible object is
           *     {@link String }
           *     
           */
          public String getDESCR() {
              return descr;
          }
  
          /**
           * Legt den Wert der descr-Eigenschaft fest.
           * 
           * @param value
           *     allowed object is
           *     {@link String }
           *     
           */
          public void setDESCR(String value) {
              this.descr = value;
          }
  
          /**
           * Ruft den Wert der activ-Eigenschaft ab.
           * 
           * @return
           *     possible object is
           *     {@link String }
           *     
           */
          public String getACTIV() {
              return activ;
          }
  
          /**
           * Legt den Wert der activ-Eigenschaft fest.
           * 
           * @param value
           *     allowed object is
           *     {@link String }
           *     
           */
          public void setACTIV(String value) {
              this.activ = value;
          }
  
      }
  
  }
  </code></pre>
  
  <p>I have not much experiance with Jaxb and marshalling/unmarshalling XML documents and hope you can give me some hints.</p>
  
  <p><strong>One notice</strong>: If I delete the jaxb.properties file, the code works properly. But I need to export my code (as jar archive) to another System and this system needs a jaxb.properties file to run my classes. So it has to work with this file.</p>
  
  <p>Therefore after solving this NullPointerException the rest of the code should be allright. </p>
  
  <p>Many thanks for you help!</p>
  
  <p>Greetings Enrico</p>