NullPointerException while setting LayoutParams <p>I want to add a button programmatically, which LayoutParams should be set too. Unfortunaly the app gives an exception: </p> <blockquote> <p>java.lang.NullPointerException: Attempt to write to field 'int android.view.ViewGroup$LayoutParams.height' on a null object reference</p> </blockquote> <p>I have no idea, why. Could you help me? Here is my code.</p> <pre><code> Button b = new Button(getApplicationContext()); b.setText(R.string.klick); ViewGroup.LayoutParams params = b.getLayoutParams(); params.height = ViewGroup.LayoutParams.MATCH_PARENT; params.height = ViewGroup.LayoutParams.WRAP_CONTENT; </code></pre>