title: How to throw exception while converting from JsonStr to PojoClassObj if any key/value is missing in the JsonStr?
body: <p>I tried using <code>@NotNull</code> annotation but it did not work. If any field is missing in the <code>JsonString</code>, it gives <code>null(String)</code> or <code>zero(int)</code>. But what I want is if any field is missing in the <code>JsonStr</code> defined in the class should throw an exception.</p>
  
  <p><strong>Add:</strong> My <code>PojoClass</code> may have object reference or multilevel object reference. I am using <strong>Gson</strong> for the conversion of the <code>String</code> to obj.
  for more clarification, I have added my code below:</p>
  
  <p><strong>JsonStr:</strong>  </p>
  
  <pre><code>{
     "name":"John",
     "id":1,
     "roll":100,
     "c":{
        "city":"Dhaka",
        "school":"B. govt. School"
     }
  }
  </code></pre>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>public class C {
  
      private String city;
      private String school;
  
      public String getCity() {
          return city;
      }
  
      public void setCity(String city) {
          this.city = city;
      }
  
      public String getSchool() {
          return school;
      }
  
      public void setSchool(String school) {
          this.school = school;
      }
  
  }
  </code></pre>
  
  <p><strong>ClassB:</strong></p>
  
  <pre><code>public class B {
  
      private String name;
      private int id;
      @NotNull
      private int roll;
      private C c;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public int getRoll() {
          return roll;
      }
  
      public void setRoll(int roll) {
          this.roll = roll;
      }
  
      public C getC() {
          return c;
      }
  
      public void setC(C c) {
          this.c = c;
      }
  
  }
  </code></pre>
  
  <p><strong>MainClass:</strong></p>
  
  <pre><code>try {
                  B obj = new B();
                  String str = "{\"name\":\"John\",\"id\":1,\"c\":{\"city\":\"dhaka\",\"school\":\"school\"}}";
                  obj = gson.fromJson(str, B.class);
  
              } catch (RuntimeException e) {
                   System.out.println("exception Message");
  
              }
  </code></pre>
  
  <p>For the field roll, I used <code>@NotNull</code> to throw an exception if that field is not present in the <code>JsonStr</code> but it gives 0 value without throwing any exception.</p>
  
  <p>How can I implement that?</p>
  
  <p>Please don't say this is duplicate, because I have seen these questions:</p>
  
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/35883297/gson-throwing-null-pointer-exception-when-a-field-is-missing-in-json/%22this%22">GSON throwing null pointer exception when a field is missing in json [duplicate]</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it/%22this%22">What is a NullPointerException, and how do I fix it?
  </a></p></li>
  </ul>