title: NullPointerException: LinearLayout is Null
body: <p>Please have a look at the following code</p>
  
  <pre><code>private class OpenFileEvent implements OnClickListener
      {
  
          @Override
          public void onClick(View arg0) {
  
              LinearLayout openFileDialogView = (LinearLayout)findViewById(R.id.open_file_dialog);
  
              // TODO Auto-generated method stub
              final Dialog openFileDialog = new Dialog(VoiceNotes.this);
              openFileDialog.setTitle("Open File");
              openFileDialog.setContentView(R.layout.open_dialog);
  
  
              //First, list all the available Files
              File folder = new File(Environment.getExternalStorageDirectory()+"/Voice/Notes/");
              File file = new File(folder.getAbsolutePath());
  
              File[] fileNameList = file.listFiles();
  
  
              if(fileNameList != null &amp;&amp; fileNameList.length&gt;0)
              {
                  for(int i=0;i&lt;fileNameList.length;i++)
                  {
                      //Get the sub views first
                      LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
                      View openThisFileView = inflater.inflate(R.layout.open_dialog_file, null);      
                      Button openThisFileButton = (Button)openThisFileView.findViewById(R.id.open_this_file_button);
                      Button appendThisFileButton = (Button)openThisFileView.findViewById(R.id.append_note_this_file);
                      TextView openThisFileNameTxt = (TextView)openThisFileView.findViewById(R.id.open_this_file_name);
  
                      //Set the Text
                      openThisFileNameTxt.setText(fileNameList[i].getName());
  
                      //Set the Listeners
  
  
                      //Add the View
                      openFileDialogView.addView(openThisFileView);
  
                  }
              }
              //Show the Dialog
              openFileDialog.show();
  
          }
  
      }
  </code></pre>
  
  <p>As soon as this code in running, I get the following error message, which is <code>NullPointerException</code></p>
  
  <pre><code>11-18 16:24:20.832: E/AndroidRuntime(1019): FATAL EXCEPTION: main
  11-18 16:24:20.832: E/AndroidRuntime(1019): java.lang.NullPointerException
  11-18 16:24:20.832: E/AndroidRuntime(1019):     at com.x.xxx.VoiceNotes$OpenFileEvent.onClick(VoiceNotes.java:227)
  11-18 16:24:20.832: E/AndroidRuntime(1019):     at android.view.View.performClick(View.java:4204)
  11-18 16:24:20.832: E/AndroidRuntime(1019):     at android.view.View$PerformClick.run(View.java:17355)
  11-18 16:24:20.832: E/AndroidRuntime(1019):     at android.os.Handler.handleCallback(Handler.java:725)
  11-18 16:24:20.832: E/AndroidRuntime(1019):     at android.os.Handler.dispatchMessage(Handler.java:92)
  11-18 16:24:20.832: E/AndroidRuntime(1019):     at android.os.Looper.loop(Looper.java:137)
  11-18 16:24:20.832: E/AndroidRuntime(1019):     at android.app.ActivityThread.main(ActivityThread.java:5041)
  11-18 16:24:20.832: E/AndroidRuntime(1019):     at java.lang.reflect.Method.invokeNative(Native Method)
  11-18 16:24:20.832: E/AndroidRuntime(1019):     at java.lang.reflect.Method.invoke(Method.java:511)
  11-18 16:24:20.832: E/AndroidRuntime(1019):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  11-18 16:24:20.832: E/AndroidRuntime(1019):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  11-18 16:24:20.832: E/AndroidRuntime(1019):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>The error is targetting to here</p>
  
  <pre><code>openFileDialogView.addView(openThisFileView);
  </code></pre>
  
  <p>Since it says that <code>LinearLayout</code> is null, here is the XML file where that layout belongs to.</p>
  
  <p><strong>open_dialog.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/open_file_dialog"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" 
      android:background="#ffffff"&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>This is the same layout which acts as the Content View for the <code>Dialog</code>.</p>
  
  <p>Why I think that <code>LinearLayout</code> is null, and not the layout which got inflated inside the class? Because the below code also generated <code>NullPointerException</code></p>
  
  <pre><code>openFileDialogView.addView(new Button(Notes.this));
  </code></pre>
  
  <p>Why I am getting this error? </p>