title: NullPointerException when creating new String
body: <blockquote>
  <p><strong>SOLUTION</strong></p>
  <p>I was adding a <strong>new instance</strong> of <code>A</code> to the list: <code>aList.add( new A() )</code>, whose <code>name</code> property is of course null, <strong>instead of adding the actual initialized instance</strong>. Sorry for the dumb question.</p>
  </blockquote>
  <hr />
  <p>I have this <code>A</code> class of <code>ProjectA</code> which overrides its <code>toString</code> method to return a clone of its <code>name</code> property, like this:</p>
  <pre><code>public class A {
      private String name;
  
      @Override
      public String toString() {
          return new String(name);
      }
  }
  </code></pre>
  <p>I then export this library to a jar and import it into <code>ProjectB</code> and when I call <code>a.toString()</code> I get a <code>NullPointerException</code> that says there is an error exactly on the return line: <code>return new String(name);</code>.</p>
  <p>However, if I put it like this:</p>
  <pre><code>public class A {
      private String name;
  
      @Override
      public String toString() {
          return name;
      }
  }
  </code></pre>
  <p>I don't get any exception but the <code>String</code> returned is <code>null</code>.</p>
  <p>I built the jar (<code>ProjectA</code>) using Eclipse and imported it into ADT (<code>ProjectB</code> - Eclipse too).</p>
  <p><strong>NOTE:</strong></p>
  <p>I omitted the getters/setters intentionally for the sake of simplicity, but they're in there in the original code and I'm pretty sure I set the <code>name</code> property way before calling the <code>toString()</code> method. In fact, if I call the <code>getName()</code> method, the name is returned perfectly fine, but I'm using lists and I need the <code>toString()</code> method.</p>
  <p>This is the part of the code where the <code>List</code> of <code>A</code> objects is created (<code>ProjectA</code> too):</p>
  <pre><code>ArrayList&lt;A&gt; aList = new ArrayList&lt;Categoria&gt;();
  for (int i = 0; i &lt; random.nextInt(3)+1; i++) {
      A a = new A();
      a.setId(0);
      a.setName(&quot;Test name&quot;);
      a.setDescription(&quot;Test desc.&quot;);
      aList.add(a);
  
      Log.d(&quot;app&quot;, &quot;Created object A: &quot;+a.getName()); // The name is displayed OK here
  }
  aList.trimToSize();
  </code></pre>
  <p>And this is the exact part of the code where the <code>toString()</code> method is called (<code>ProjectA</code>):</p>
  <pre><code>new ArrayAdapter&lt;A&gt;(
      actionBar.getThemedContext(),
      android.R.layout.simple_list_item_1,
      android.R.id.text1, 
      DataAccess.getAList() // The ArrayAdapter class calls the toString method to populate a list
  )
  </code></pre>
  <p>As you could see, I in fact verify the content of the <code>name</code> property via the <code>getName()</code> method and it is okay. I have to mention that the first approach (the on which uses <code>new String(name)</code>, without an finals nor null checks) worked flawlessly on another project.</p>
  <p>This is the (relevant part of the) <code>DataAccess</code> class:</p>
  <pre><code>public final class DataAccess {
      private static final Data data;
      public static Arrayist&lt;A&gt; getAList() {
          return this.data.getAList();
      }
  }
  </code></pre>