title: (Kotlin) Loading file from internal storage and using it to draw on a canvas
body: <p>I'm having problems using a text file saved in my app's Internal Storage and using it to determine what bitmaps will be drawn on a canvas and what'll be their placement. The file is being succesfully created by my app. It seems like there's a problem with "filesDir" but only under certain circumstances.</p>
  
  <p>The problem is, when I'm inserting</p>
  
  <pre><code>val file: List&lt;String&gt; = File(this.filesDir, "TempLayoutData.txt").readLines()
  </code></pre>
  
  <p>inside "class Canvass" or "override fun onDraw", "filesDir" suddenly becomes an "unresolved reference".</p>
  
  <p>So - in order to bypass this - I've created class "Layout". It looks like this:</p>
  
  <pre><code>class Layout : AppCompatActivity(){
      val file: List&lt;String&gt; = File(this.filesDir, "TempLayoutData.txt").readLines()
      val stagewidth = file[0].toInt() * 10
      val stagelength = file[1].toInt() * 10
  
      var layout = Array(stagelength) { IntArray(stagewidth) }
  
      var rowcounter = 0
      var columncounter = 0
  
      var read = File(this.filesDir, "TempLayout.txt").forEachLine {
          for (i in it) {
              layout[rowcounter][columncounter] = (i - 48).toInt()
              columncounter++
          }
          rowcounter++
          columncounter = 0
      }
  }
  </code></pre>
  
  <p>I've tested it inside "override fun onCreate" of my ViewLayout class (which also containts class Canvas) and it worked like a charm.</p>
  
  <p>When it's right inside class Canvas brought to life using</p>
  
  <pre><code>val layout = Layout()
  </code></pre>
  
  <p>it builds without errors, however when I'm running my app on my phone, the following error occurs: "Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference"</p>
  
  <p>As the source of the error, it points directly at</p>
  
  <pre><code>val file: List&lt;String&gt; = File(this.filesDir, "TempLayoutData.txt").readLines()
  </code></pre>
  
  <p>inside my Layout class, when its' instance is being created inside Canvas class.</p>
  
  <p>And I just don't know what to do. I've done some search on Google and there's literally nothing about people trying to use file inside app's Internal Storage to base their drawing on it.</p>