title: java.lang.NullPointerException at com.d4a.tobias.Book.onCreateView
body: <p>Hello I am working on a app and when I test it I get a crash on startup </p>
  
  <p>here's my logcat:</p>
  
  <pre><code>    11-29 00:16:19.582: E/memtrack(1549): Couldn't load memtrack module (No such file or directory)
  11-29 00:16:19.582: E/android.os.Debug(1549): failed to load memtrack module: -2
  11-29 00:16:28.782: E/memtrack(1609): Couldn't load memtrack module (No such file or directory)
  11-29 00:16:28.782: E/android.os.Debug(1609): failed to load memtrack module: -2
  11-29 00:16:29.462: E/AndroidRuntime(1620): FATAL EXCEPTION: main
  11-29 00:16:29.462: E/AndroidRuntime(1620): Process: com.d4a.tobias, PID: 1620
  11-29 00:16:29.462: E/AndroidRuntime(1620): java.lang.NullPointerException
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at com.d4a.tobias.Book.onCreateView(Book.java:55)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.support.v4.view.ViewPager.populate(ViewPager.java:804)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.View.measure(View.java:16458)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.View.measure(View.java:16458)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.View.measure(View.java:16458)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.View.measure(View.java:16458)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.View.measure(View.java:16458)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.Choreographer.doFrame(Choreographer.java:532)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.os.Handler.handleCallback(Handler.java:733)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.os.Handler.dispatchMessage(Handler.java:95)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.os.Looper.loop(Looper.java:137)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at android.app.ActivityThread.main(ActivityThread.java:4998)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at java.lang.reflect.Method.invokeNative(Native Method)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at java.lang.reflect.Method.invoke(Method.java:515)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
  11-29 00:16:29.462: E/AndroidRuntime(1620):     at dalvik.system.NativeStart.main(Native Method)
  11-29 00:16:38.902: E/WindowManager(494): Starting window AppWindowToken{42e4abd8 token=Token{42681aa0 ActivityRecord{42311100 u0 com.d4a.tobias/.MainActivity t33}}} timed out
  </code></pre>
  
  <hr>
  
  <p>heres Book.java:</p>
  
  <pre><code>    package com.d4a.tobias;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.Arrays;
  import java.util.List;
  
  import android.content.ComponentName;
  import android.content.Context;
  import android.content.Intent;
  import android.content.res.AssetManager;
  import android.net.Uri;
  import android.os.Bundle;
  import android.os.Environment;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ListView;
  import android.widget.Toast;
  
  import com.actionbarsherlock.app.SherlockFragment;
  import com.d4a.tobias.R;
  import com.sibext.android_shelf.ImportBooks;
  import com.sibext.android_shelf.adapter.ShelfAdapter;
  
  public class Book extends SherlockFragment {
  
      Button bt1;
       //Shelf
          private static final String TARGET_DIRECTORY = "mnt/sdcard/shelf/";
          private ListView list;
  
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          // Get the view from fragmenttab1.xml
          View v=inflater.inflate(R.layout.fragmenttab1, container, false);
  
  
          // shelf
          File dir = new File(TARGET_DIRECTORY);
          if(!dir.exists()){
              dir.mkdirs();
              //past here
              addBooksFromAssetsToCard();
          }else{
              String files[] = dir.list();
              // line 55 if(files.length == 0){
                  //past here
                  addBooksFromAssetsToCard();
              }
          }
  
          list = (ListView) v. findViewById(R.id.list);
  
          ShelfAdapter adapter = new ShelfAdapter(this,TARGET_DIRECTORY);
          adapter.setToListView(list);
  
          //Intent in =  new Intent(v.getContext(), com.sibext.android_shelf.MainActivityShelf.class);
          //startActivity(in);
  
          //Toast.makeText(v.getContext(), "Did you see the toast msg?",Toast.LENGTH_LONG).show();
  
          //buttons for toolbelt do not take out!!
  
            v. findViewById(R.id.button10).setOnClickListener(new OnClickListener()
  
     {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
  
  
                  String url = "http://www.google.com";
                  Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                  startActivity(browserIntent);
              }
          });
  
  
     v. findViewById(R.id.button11).setOnClickListener(new OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
  
  
  
                  //  String url = "http://www.google.com";
                  //  Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
              //      startActivity(browserIntent);
  
  
  
              Intent i=new Intent(getActivity().getApplicationContext(),Launchalot.class);
  
              startActivity(i);
  
  
          }
  
     });
  
  
     v. findViewById(R.id.tools).setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
  
  
              //  String url = "http://www.google.com";
              //  Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
          //      startActivity(browserIntent);
  
  
  
                  Intent intent = new Intent(Intent.ACTION_MAIN);
                  intent.setComponent(new ComponentName("com.d4a.stz","com.d4a.stz.ToolsHome"));
                  intent.putExtra("grace", "Hi");
                  startActivity(intent);
  
  
      }
  
  });
  
  
  
      v. findViewById(R.id.googleplay).setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
  
  
  
              //  String url = "http://www.google.com";
              //  Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
              //      startActivity(browserIntent);
  
  
  
              Intent intent = new Intent(Intent.ACTION_VIEW);
              intent.setData(Uri.parse("http://play.google.com/store/apps"));
              startActivity(intent);
  
  
          }
  
      });
  
      v. findViewById(R.id.youtube).setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
  
  
              //  String url = "http://www.google.com";
              //  Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
              //      startActivity(browserIntent);
  
  
              Intent videoClient = new Intent(Intent.ACTION_VIEW);
              videoClient.setClassName("com.google.android.youtube", "com.google.android.youtube.WatchActivity");
              startActivity(videoClient);
  
  
          }
  
      });
  
      v. findViewById(R.id.email).setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
  
  
              Intent intent = new Intent(Intent.ACTION_MAIN);
              intent.setComponent(new ComponentName("com.d4a.eMailTime","com.fsck.k9.activity.Accounts"));
              intent.putExtra("grace", "Hi");
              startActivity(intent);
  
  
  
  
          }
  
      });
  
          //shelf
          //ShelfAdapter adapter = new ShelfAdapter(this, TARGET_DIRECTORY);
          //adapter.setToListView(list);
          return v;
  
      }
  
  
  
      public void addBooksFromAssetsToCard(){
          List&lt;String&gt; books;
          try {
              books = getBooksFromAsset(getActivity());
  
              for(String book : books){
                  copyFromAssets(book);
              }
  
          } catch (Exception e) {
          }
      }
  
      public List&lt;String&gt; getBooksFromAsset(Context ctx) throws Exception
      {
          AssetManager assetManager =ctx.getAssets();
          String[] files = assetManager.list("books");
          List&lt;String&gt; it=Arrays.asList(files);
          return it;
      }
  
      public void copyFromAssets(String book)
      {
          AssetManager assetManager = getActivity().getAssets();
          String[] files = null;
          InputStream in = null;
          OutputStream out = null;
          //String filename = "filename.ext";
          try
          {
              in = assetManager.open("books/"+book);
              out = new FileOutputStream(Environment.getExternalStorageDirectory()+File.separator+"/shelf/"+book);
              Log.d("Copying...", ""+book);
              copyFile(in, out); 
              in.close();
              in = null;
              out.flush();
              out.close();
              out = null;
          }
          catch(Exception e)
          { 
              Log.e("tag", "Failed to copy asset file: " + book, e);
          }      
      }
      public void copyFile(InputStream in, OutputStream out) throws Exception
      {
          byte[] buffer = new byte[1024];
          int read;
          while((read = in.read(buffer)) != -1)
          {
              out.write(buffer, 0, read);
          }
          Log.d("Copy_State", "Done...");
      }
  
      public void onImportClicked(View v){
          Toast.makeText(getActivity(), "Please Wait", Toast.LENGTH_LONG)
           .show();
          Intent in = new Intent(getActivity(), ImportBooks.class);
          startActivity(in);
  
  
  
  
  
  
  
  
  
  
  
          return;
  
      }
  
  
  
  
  
  
  
  
  
  
  
  
  
  }
  </code></pre>
  
  <p>EDIT I have tried changing dir.list()== 1 and I get a new logcat error:    </p>
  
  <pre><code>    11-29 00:29:29.662: E/AndroidRuntime(2710): java.lang.NullPointerException
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at com.d4a.tobias.Book.onCreateView(Book.java:55)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.support.v4.view.ViewPager.populate(ViewPager.java:804)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.View.measure(View.java:16458)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.View.measure(View.java:16458)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.View.measure(View.java:16458)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.View.measure(View.java:16458)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.View.measure(View.java:16458)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.Choreographer.doFrame(Choreographer.java:532)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.os.Handler.handleCallback(Handler.java:733)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.os.Handler.dispatchMessage(Handler.java:95)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.os.Looper.loop(Looper.java:137)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at android.app.ActivityThread.main(ActivityThread.java:4998)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at java.lang.reflect.Method.invokeNative(Native Method)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at java.lang.reflect.Method.invoke(Method.java:515)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
  11-29 00:29:29.662: E/AndroidRuntime(2710):     at dalvik.system.NativeStart.main(Native Method)
  11-29 00:29:38.802: E/WindowManager(494): Starting window AppWindowToken{429fc770 token=Token{42516cc8 ActivityRecord{426874a8 u0 com.d4a.tobias/.MainActivity t38}}} timed out
  </code></pre>
  
  <p>I have tried everything googling the logcat error asking a buddy that is good with java and still no result </p>
  
  <p>Any help would be amazing </p>
  
  <p>Thanks in advance </p>
  
  <p>Chris </p>