Error 1 (E0) Internal error: Object reference not set to an instance of an object. 0 0 ConsoleApplication1

Here is the code for the Console app:

namespace ConsoleApplication1;

interface

uses
   System.Windows.Forms,
  System.Linq;

type
  ConsoleApp = class
  public
     [STAThread]
    class method Main(args: array of String);
  end;

implementation

class method ConsoleApp.Main(args: array of String);
begin
  var x := 'test';

  var y := x.Encrypt( x );

  Console.WriteLine( 'x=' + x );
  Console.WriteLine( 'y=' + y );
  Console.ReadLine;
end;

end.

And here is the extension code:

namespace ConsoleApplication1;

interface

      Extension method String.Encrypt( dataToEncrypt:   String ) : String;

implementation

   uses 
      System.IO,
      System.Security.Cryptography, 
      System.Text;


   Extension method String.Encrypt(dataToEncrypt: String ) : String;
      var 
         aes              : AesManaged;
         password         : String;
         inBytes          : array of Byte;
         encryptTransform : ICryptoTransform;
         outBytes         : array of Byte;
         
      method GetSecretKey( password : String ) : array of Byte;
         var
            salt : array of Byte;
            iterations : Integer;
            rfc2898 : System.Security.Cryptography.Rfc2898DeriveBytes;
         begin
         salt       := new Byte[ (172, 137, 25, 56, 156, 100, 136, 211, 84, 67, 96, 10, 24, 111, 112, 137, 3 )];
         iterations := 1024;
         rfc2898    := new System.Security.Cryptography.Rfc2898DeriveBytes( password, salt, iterations );
         result     := rfc2898.GetBytes( 16 );
         end;
      begin

      password      := 'xxx';

      aes           := new AesManaged;
      aes.KeySize   := 128;
      aes.BlockSize := 128;
      aes.Mode      := CipherMode.CBC;
      aes.Padding   := PaddingMode.PKCS7;
      aes.Key       := GetSecretKey( password );

      inBytes           := System.Text.Encoding.UTF8.GetBytes( dataToEncrypt );
      encryptTransform  := aes.CreateEncryptor();
      outBytes          := encryptTransform.TransformFinalBlock( inBytes, 0, inBytes.Length);

      result            := Convert.ToBase64String( outBytes );
      end;
end.

… time passes …

It must have something to do with how to initialize a new array of byte. (I never know where to look as I can’t find it in documentation).

But this seemed to get past the compiler error:

     salt       := new Byte[16];
     salt       := [ 172, 137, 25, 56, 156, 100, 136, 211, 84, 67, 96, 10, 24, 111, 112, 137, 3 ];

Thanks, logged as bugs://71270

Ah yes:

     salt       := new Byte[ (172, 137, 25, 56, 156, 100, 136, 211, 84, 67, 96, 10, 24, 111, 112, 137, 3 )];

Isn’t actually a valid way to create a byte array, you’re essentially passing a tuple to new byte[], which wants an integer.

Also note that the following code:

 salt       := new Byte[16];
 salt       := [ 172, 137, 25, 56, 156, 100, 136, 211, 84, 67, 96, 10, 24, 111, 112, 137, 3 ];

Creates an array twice. Discarding the first one. You can just use the last one. I will of course fix the error.

bugs://71270 got closed with status fixed.

So what IS the right way to create and initialize the array? I almost never use arrays, and I couldn’t find an example in the documentation. Can you point me to an example in the docs?

both:

 salt       := new Byte[16];

and

 salt       := [ 172, 137, 25, 56, 156, 100, 136, 211, 84, 67, 96, 10, 24, 111, 112, 137, 3 ];

are examples of initializing an array. the first creates an array with 16 zeros, the send has 16 values.

There’s a small sample at http://docs.elementscompiler.com/Oxygene/Expressions/ArrayLiterals/ saying the same.

ok, thanks. How would I initialize an array of longint, for instance? Seems like the second statement wouldn’t know what type of array to create if it were var salt := …

there are several ways to do that. Essentially you make it know a type:

var x: array of Longint := [1,2,3,4];
or
var y := array of LongInt([1,2,3,4]);
or
var z := [Longint(1), Longint(2), Longint(3)];

ok. So it looks like you never “new up” an array? Can your examples be used in a declaration initialization?

like:

type
   myclass = public class
      public 
         x : array of longint := [1,2,3,4];

That doesn’t compile for me. Is there a way to do it?

That compiles just fine for me

Odd. Compiles for me too, now. Not sure what could have been different.

Thanks for all the replies Carlo.