title: Storing key using KeyStore in Android
body: <p>I am using KeyStore to protect my private key but when the line:</p>
  
  <pre><code>FileOutputStream fos = ctx.openFileOutput("bs.keystore", Context.MODE_PRIVATE);
  </code></pre>
  
  <p>is executed I have this exception: </p>
  
  <pre><code>'java.lang.NullPointerException'.
  </code></pre>
  
  <p>I don't understand where is the problem.</p>
  
  <p>Code:</p>
  
  <pre><code>    private Context ctx;
  
      public DataSec(Context ctx) 
      {
      ctx = this.ctx;
      }
  
      public void genKey() throws Exception
      {
      SecretKey key = KeyGenerator.getInstance("AES").generateKey();
  
      KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
      ks.load(null, "clavedekey".toCharArray());
  
      PasswordProtection pass = new PasswordProtection("fedsgjk".toCharArray());
      KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(key);
      ks.setEntry("secretKeyAlias", skEntry, pass);
  
      FileOutputStream fos = ctx.openFileOutput("bs.keystore", Context.MODE_PRIVATE);
      ks.store(fos, "clavedekey".toCharArray());
      fos.close();        
      }
  </code></pre>
  
  <p>Thanks for the Help!</p>