Using the latest builds of Oxygene in VS2013, I was writing a small console app and was ready to move some code out of the main method to a sub method. I had read about the new ExtractMethod refactoring capability, so highlighted a few lines of code and selected the pop-up menu option. When the New Name prompt came up, there was a warning text under the edit box: “Cannot extract: Object reference not set to an instance of an object.” Silly me, I ignored it and typed in the new name for the method anyway and hit OK.
What code triggered this? I don’t understand your question.
I was in the Visual Studio 2013 code editor, making a change to a small console app using Oxygene. The code had a few methods and the main method was getting too large so decided to extract several lines out to a new method.
Since this is pretty much what I said before, I must not be giving you the information you need. What else can I tell you about the project?
I should have been more clear. The NRE doesn’t happen on every bit of code but is specific to the code that was selected at the time Extract Method was triggered. Any chance you can send that so I can try it here? (private is fine too of course, support@)
Well, I’ve already manually extracted it, but it’s listed below. The method MainLoop was originally in the “Main” method.
namespace PrimeQuery;
interface
type
MenuAction = enum (maInvalid, maTestPrime, maListPrimes, maExit);
ConsoleApp = class
public
const MaxLastPrime = 1000000000000000;
class method ConsoleMenu: MenuAction;
class method ListPrimes(LastPrime: UInt64);
class method TestPrime;
class method MainLoop;
class method Main(args: array of String);
end;
implementation
class method ConsoleApp.ConsoleMenu: MenuAction;
var
cmd: String;
begin
Console.WriteLine;
Console.WriteLine('- Prime Query - ');
Console.WriteLine('X - eXit');
Console.WriteLine('T - Test a number for primality');
Console.WriteLine('L - List prime numbers less than {0}', MaxLastPrime);
Console.Write('Enter Command: ');
cmd := Console.ReadLine;
Console.WriteLine;
Console.WriteLine('> ' + cmd);
Console.WriteLine;
case cmd.Substring(0, 1).ToUpper of
'X': result := MenuAction.maExit;
'T': result := MenuAction.maTestPrime;
'L': result := MenuAction.maListPrimes;
else result := MenuAction.maInvalid;
end;
end;
class method ConsoleApp.ListPrimes(LastPrime: UInt64);
begin
for i: UInt64 := 1 to LastPrime do
if PrimeNumberQuery.IsPrime(i) then begin
if i > 2 then
Console.Write(', ');
Console.Write(i.ToString);
end;
Console.WriteLine;
end;
class method ConsoleApp.TestPrime;
var
num: UInt64;
begin
Console.Write('Enter a number: ');
UInt64.TryParse(Console.ReadLine, out num);
if PrimeNumberQuery.IsPrime(num) then
Console.WriteLine('{0} IS A PRIME!', [num])
else
Console.WriteLine('{0} is not a prime.', [num]);
end;
class method ConsoleApp.MainLoop;
var
done := false;
begin
repeat
case ConsoleMenu of
MenuAction.maExit:
done := True;
MenuAction.maTestPrime:
TestPrime;
MenuAction.maListPrimes:
ListPrimes(MaxLastPrime);
MenuAction.maInvalid:
Console.WriteLine('Invalid command');
end;
until done;
end;
class method ConsoleApp.Main(args: array of String);
const
CRLF = #13#10;
ProgramHelp = 'PrimeQuery Console - a simple program to test the primality of a number,' + CRLF +
' or to list all primes up to a certain number.' + CRLF +
'If started with /? then this help message is listed.' + CRLF +
'If started with /list 123 then all primes up to 123 are listed' + CRLF +
' (replace 123 with any positive number up to 4 quadrillion).';
var
LastPrime: UInt64;
begin
if length(args) > 0 then begin
for i: Integer := 0 to length(args) - 1 do
if args[i].Equals('/?') then
Console.WriteLine(ProgramHelp)
else if (args[i].Equals('/list', StringComparison.OrdinalIgnoreCase)) and (length(args) > i) then begin
UInt64.TryParse(args[i+1], out LastPrime);
ListPrimes(LastPrime);
break;
end else
Console.WriteLine('Invalid Parameter: ' + args[i]);
end else
MainLoop;
end;
end.