[301] NRE

Maybe these are in progress from the comments made in 300, but 301 gives
the NRE below...

private static void Initialiser(object sender, InitialiserEventArgs e)
{
if (e.NewObject is TransformPackage && e.CallingObject is Model)
{
Model model = e.CallingObject as Model;
TransformPackage package = e.NewObject as TransformPackage;

// NRE warning both model and package below.
int order = model.GetNextOrder();
package.Order = order;
package.ParentPackage.SingleItem = model;
}
}


3 comments
Comment actions Permalink

Frankly speaking, NRE analysis is quite right here

1) Formal analysis algorithm cannot be sure that subsequential calls to
property (say, e.NewObject) returns the same value
2) After checking type by 'is' expression, it is a good idea to use direct
cast to checked type. Otherwise, if you do use 'as' expression, the 'null'
result is expected

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Sean Kearon" <no@spam.org> wrote in message
news:adc66bd2b8c8c8d39d473f687e@news.jetbrains.com...

Maybe these are in progress from the comments made in 300, but 301 gives
the NRE below...

>

private static void Initialiser(object sender, InitialiserEventArgs
e)
{
if (e.NewObject is TransformPackage && e.CallingObject is Model)
{
Model model = e.CallingObject as Model;
TransformPackage package = e.NewObject as TransformPackage;

>

// NRE warning both model and package below.
int order = model.GetNextOrder();
package.Order = order;
package.ParentPackage.SingleItem = model;
}
}

>



0
Comment actions Permalink

Sean Kearon schrieb:

Maybe these are in progress from the comments made in 300, but 301 gives
the NRE below...


Which is correct.

private static void Initialiser(object sender, InitialiserEventArgs e)
{
if (e.NewObject is TransformPackage && e.CallingObject is Model)
{
Model model = e.CallingObject as Model;


If the e.CallingObject is not from type Model, then model ist null.

The same with package.

Regards

Albert

0
Comment actions Permalink

Thanks Eugene.....

Frankly speaking, NRE analysis is quite right here

1) Formal analysis algorithm cannot be sure that subsequential calls
to
property (say, e.NewObject) returns the same value


Good point - it's a case here of R# improving my code.....which is always
a good thing ;)

2) After checking type by 'is' expression, it is a good idea to use
direct
cast to checked type. Otherwise, if you do use 'as' expression, the
'null'
result is expected


An opportunity to use the "Convert to direct cast" context action.....if
there was one ;)

Regards

Sean


0

Please sign in to leave a comment.