title: Problems with reading text file from assets (xamarin)
body: <p>I have problem with reading simple txt from assets directory. I can't really figure out why it doesn't work and what is wrong.</p>
  
  <p>Here is the code of method I wrote:</p>
  
  <pre><code>        private string ReadFile(){
          var stream = Assets.Open ("sampleText.txt");
          StreamReader sr = new StreamReader (stream);
          string text = sr.ReadToEnd ();
          sr.Close ();
          return text;
      }
  </code></pre>
  
  <p>And here is the error:</p>
  
  <pre><code>Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2970/46c3f7e0/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at Android.Runtime.JNIEnv.CallNonvirtualObjectMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod) [0x00084] in /Users/builder/data/lanes/2970/46c3f7e0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:596 
  at Android.Content.ContextWrapper.get_Assets () [0x0005f] in /Users/builder/data/lanes/2970/46c3f7e0/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Content.ContextWrapper.cs:154 
  </code></pre>
  
  <p>I had no problems with running my whole application on windows, just I have problems to run it on android. Tried various things, like instance of AssetManager, but didn't work as well.</p>
  
  <p>The text file is marked as AndroidAsset.</p>
  
  <p>This is my <code>TextChangeBench</code>:</p>
  
  <pre><code>public class TextChangeBench : Activity
  {
      public TextChangeBench (){}
  
      private void SaveFile(string[] tab){
  
          string saveLocation = "sampleTextReworked.txt";
          StreamWriter sw = new StreamWriter (saveLocation);
          foreach (string s in tab) {
              sw.Write (s);
          }
          sw.Close ();
      }
  
      private string ReadFile(){
          var stream = Assets.Open ("sampleText.txt");
          StreamReader sr = new StreamReader (stream);
          string text = sr.ReadToEnd ();
          sr.Close ();
          return text;
      }
      public void ChangeText(){
          try{
              File.Delete("sampleTextReworked.txt");
          }catch(FileNotFoundException e){Console.WriteLine (e);}
  
          try{
              string text = ReadFile ();
              char c;
              string[] newTab = new string[text.Length];
              for (int i = 0; i &lt; text.Length; i++)
              {
                  c = (char)text [i];
                  if (Char.IsUpper(c))
                  {
                      newTab[i] = text[i].ToString().ToLower();
                  }
                  else if (Char.IsLower(c))
                  {
                      newTab[i] = text[i].ToString().ToUpper();
                  }
                  else
                  {
                      newTab[i] = text[i].ToString();
                  }
              }
              SaveFile(newTab);
          }
          catch(Exception e){Console.WriteLine ("{0} ", e);}
      }
  }
  </code></pre>
  
  <p>This is my <code>MainActivity</code>:</p>
  
  <pre><code>[Activity (Label = "csBench", MainLauncher = true, Icon = "@mipmap/icon")]
  public class MainActivity : Activity
  {
      protected override void OnCreate (Bundle savedInstanceState)
      {
          base.OnCreate (savedInstanceState);
  
          SetContentView (Resource.Layout.Main);
          Button startButton = FindViewById&lt;Button&gt; (Resource.Id.start);
          startButton.Click += delegate {
  
              MathBench mb = new MathBench ();
              TextChangeBench tcb = new TextChangeBench ();
              PassedTime pt = new PassedTime ();
              EditText et = (EditText)FindViewById (Resource.Id.textInfo);
  
              for (int i = 0; i &lt; 5; i++) {
                  pt.StartMeasuring ();
                  //mb.Silnia (25);
                  //mb.Fibonacci (32);
                  //mb.BubbleSort ();
                  tcb.ChangeText ();
                  if (i == 4) {
                      pt.StopMeasuring ();
  
                      et.SetText(pt.ReturnResult(), TextView.BufferType.Normal);
                      //et.SetText(pt.ReturnResult().ToString());
                      //Console.WriteLine(pt.ReturnResult ());
                      pt.ResetTimers ();
                  }
              }  
          };
  
      }
  }
  </code></pre>
  
  <p>Any help is welcome, thanks in advance.</p>