title: java. Method return Null. What exception I must generated?
body: <p>I have this line code:</p>
  
  <pre><code>String name = Book.getName();
  
  /*next lines of code*/
  </code></pre>
  
  <p>Next, variable <code>name</code> processing in other code without any checks.
  In some cases, possible situation, when <code>name=null</code> and other code will exit with an error.</p>
  
  <p>It is bad. </p>
  
  <p>Also, I cant access to the other code.</p>
  
  <p><strong>So, what do you think, my next implementation is correct</strong>:</p>
  
  <pre><code>    try
      {
      String name = Book.getName();
      if(null== name)
  
      throw new NullPointerException("method 'getName' return null");
  
  /*next lines of code*/
      }
  
      catch(NullPointerException e)
      {
      System.out.print("Hey! Where book name? I exit!");
      System.exit();
      }
  </code></pre>
  
  <p><strong>I have any other choose in this case?</strong></p>
  
  <p>It is possible to generate any other type of <code>Exception</code> or only <code>NullPointerException</code>?</p>
  
  <p>Thanks.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Ok, </p>
  
  <pre><code>  String name = Book.getName();
  </code></pre>
  
  <p>it's imagine code line. In real case, I have more complex code:</p>
  
  <pre><code>List&lt;Book&gt; bookList= new ArrayList&lt;Book&gt;();
  String name = null;
  
  Iterator i = BookShop.getBooks.iterator(); //BookShop it is input parameter!
  
  while(i.hasNext())
  {
  
  Book book = (Book) i.next;
  name = book.getName();
  
  nameList.add(name);
  
  
  }
  </code></pre>
  
  <p>This example more full.</p>
  
  <p>So, in this code input parameter <code>BookShop</code> Object.</p>
  
  <p>What problem I can have with this Object?</p>
  
  <ol>
  <li>BookShop can be <code>NULL</code>;</li>
  <li>method <code>BookShop.getBooks()</code> can return <code>NULL</code>;</li>
  </ol>
  
  <p>Also, <code>getName()</code> can return <code>NULL</code> too.</p>
  
  <p>So, general problem next: <strong>there is no guarantee the correctness of input parameter BookShop!</strong></p>
  
  <p>And I must to consider every possible option (3 <code>NULL</code>)</p>
  
  <p>For me, add General <code>try-catch</code> block and that all.</p>
  
  <p>No?</p>