title: How to programmatically set a TextView width and height to Wrap Content
body: <p>I am writing a table programmatically from an SQLite database.</p>
  
  <p>In a loop I am generating  needed <code>TextViews</code> and am attempting to wrap the data in a <code>TextView</code> called <code>descCol</code> when the data is longer than the existing screen allows.</p>
  
  <p>Suggestions to do this were offered in the following link:
  <a href="https://stackoverflow.com/questions/4854492/setting-width-to-wrap-content-for-textview-through-code/47366121#47366121">Setting width to wrap_content for TextView through code</a></p>
  
  <p>However when using either of the suggested methods I'm getting a <code>java.lang.NullPointerException</code>.</p>
  
  <p>Here's my code example:</p>
  
  <pre><code>TextView descCol = new TextView(this);
  descCol.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
  </code></pre>
  
  <p>In this case debug shows it throws a <code>java.lang.NullPointerException</code> on the <code>getLayoutParams()</code> line.</p>
  
  <p>Also I've tried:</p>
  
  <pre><code>TextView descCol = new TextView(this);
  ViewGroup.LayoutParams params = descCol.getLayoutParams();
  params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
  params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
  descCol.setLayoutParams(params);
  </code></pre>
  
  <p>In this case debug shows it throws a <code>java.lang.NullPointerException</code> on the <code>params.height</code> line.</p>
  
  <p>Debug shows that in either case after performing the <code>getLayoutParams()</code> method, params is equal to null, apparently throwing the exception.</p>
  
  <p>I've tried to assign other parameters first to <code>descCol</code> (textcolor, text, gravity etc.) prior to the <code>getLayoutParams()</code> but get the same result.</p>
  
  <p>Suggestions as to how to avoid the <code>java.lang.NullPointerException</code> would be appreciated.</p>