C# NullReferenceException but nothing is null - c#

I have a C# class with a field and a property that looks like this.
public static class Config {
// ...
private static string admin_email;
public static string AdminEmail {
get {
if (admin_email == null) {
admin_email = config_xml.Element("admin_email").Value;
// ^ The exception is thrown here.
}
return admin_email;
}
}
}
In the above code, config_xml is an XElement which contains a child element that looks like
<admin_email>myemail#example.com</admin_email>
However, when I try to access this property, I get a NullReferenceException even though the debugger shows that nothing is null.
I checked the debugger, and watching config_xml.Element("admin_email").Value shows the email, as expected.
The weird part is that when I put a breakpoint on that line and step in one step at a time there is no exception thrown.
I have tried with and without enabling the option Just My Code.
In case this helps, I try to access the property on a line like this (from a different project)
message.From = new MailAddress(Config.AdminEmail);
Edit
After changing the code to this, I realised that c was still null.
get {
if (admin_email == null) {
XElement c = config_xml;
XElement e = c.Element("admin_email");
// ^ Exception is now thrown here
string v = e.Value;
admin_email = v;
}
return admin_email;
}

Thank you David, asawyer, and Lasse V. Karlsen for helping me realise my mistake. I changed my code to this, and now it works.
admin_email = new Email(ConfigXml.Element("admin_email").Value;
I was using a similar technique for config_xml and ConfigXml, so I would only load the XML into the field config_xml if it was ever needed, and I forgot to access it with the property ConfigXml (which did the loading) instead of the field config_xml (which was null until I used the property).
I don't know why it was working with a breakpoint, maybe when I watched the property it assigned it? I don't know.

Related

New to c# need to understand How can I Fix this error? "Possible null reference return" I've tried every option in visual studio and nothing fixed it

Learning C# should this be fixed or left alone? I can't seem to find an answer that works every action in VS didn't resolve the issue answer's I found online I either didn't understand or failed as well. Why I am asking here.
`
public class AdventureService : IAdventureService
{
public Adventure GetInitialAdventure()
{
var basePath = $" {AppDomain.CurrentDomain.BaseDirectory}Adventures";
var initialAdventure = new Adventure();
if (File.Exists($"{basePath}\\initial.json"))
{
var directory = new DirectoryInfo(basePath);
var initialJsonFile = directory.GetFiles("initial.json");
using (StreamReader fi = File.OpenText(initialJsonFile[0].FullName))
{
initialAdventure = JsonConvert.DeserializeObject<Adventure>(fi.ReadToEnd());
}
}
return initialAdventure;
}
`
You need to decide what the method will do if the deserialize call returns null.
One option is for the method to return null, in which case you just need to change the return type:
public Adventure? GetInitialAdventure()
{
//Etc.
If you prefer that the method never return null, you could change the return statement so that it replaces a null with a new Adventure.
//Rest of method up here
return initialAdventure ?? new Adventure();
}
There are maybe other options as well but these are the basics.

Error CS0103 when a variable is actually in the right scope

I have this control (db is the Entity Framework context):
if (db.Sites.Any(s => s.Name.Equals(name))) throw new NameAlreadyInUseException(name);
When I run my tests and debug it fails giving me the error:
Error CS0103: the name 's' does not exist in the current context.
I honestly can't get my head around it and Google hasn't really been helping... any help is appreciated, thanks in advance. Isn't s used correctly here? (I'm still learning, so maybe I missed something but my code here looks ok to me)
Edit:
the debugger triggers the error on this line and I am not using s in any other place other than inside that if statement. (I edited the line to show what happens with the if)
Edit2: complete code of the function
public void CreateSiteOnDb(string connectionString, string name, int timezone, int sessionExpirationTimeInSeconds,
double minimumBidIncrement)
{
CheckInput_CreateSiteOnDb(connectionString, name, timezone, sessionExpirationTimeInSeconds, minimumBidIncrement);
try
{
using (var db = new AuctionSiteContext(connectionString))
{
if (db.Sites.Any(s => s.Name.Equals(name))) throw new NameAlreadyInUseException(name);
var site = new Entities.Site
{
Name = name,
Timezone = timezone,
MinimumIncrement = minimumBidIncrement,
SessionExpirationInSeconds = sessionExpirationTimeInSeconds
};
db.Sites.Add(site);
db.SaveChanges();
}
}
catch(NameAlreadyInUseException)
{
throw;
}
catch(Exception)
{
throw new UnavailableDbException();
}
}
Edit3: Error as shown during debugging
It is not in the right scope.
In the screenshot you are in the scope of CreateSiteOnDb -> try -> using, but s does not belong to that context.
In very basic terms s => .... is converted to function of a class, and it is called from inside Any. so let's assume that our expression is function named Steve. Steve would look like this:
bool Steve(ISite s)
{
return s.Name.Equals(name);
}
this means s is the parameter of Steve, and is only valid inside Steve, which becomes CreateSiteOnDb -> try -> using -> Any -> Steve
So to see s you need to be in two more levels. Please, put your cursor inside the expression and then put a breakpoint.

Replace method's Body with Body of another method using Mono.Cecil?

With Mono.Cecil it looks quite simple when we can just set the Body of the target MethodDefinition to the Body of the source MethodDefinition. For simple methods, that works OK. But for some methods whereas a custom type is used (such as to init a new object), it won't work (with an exception thrown at the time writing the assembly back).
Here is my code:
//in current app
public class Form1 {
public string Test(){
return "Modified Test";
}
}
//in another assembly
public class Target {
public string Test(){
return "Test";
}
}
//the copying code, this works for the above pair of methods
//the context here is of course in the current app
var targetAsm = AssemblyDefinition.ReadAssembly("target_path");
var mr1 = targetAsm.MainModule.Import(typeof(Form1).GetMethod("Test"));
var targetType = targetAsm.MainModule.Types.FirstOrDefault(e => e.Name == "Target");
var m2 = targetType.Methods.FirstOrDefault(e => e.Name == "Test");
var m1 = mr1.Resolve();
var m1IL = m1.Body.GetILProcessor();
foreach(var i in m1.Body.Instructions.ToList()){
var ci = i;
if(i.Operand is MethodReference){
var mref = i.Operand as MethodReference;
ci = m1IL.Create(i.OpCode, targetType.Module.Import(mref));
}
else if(i.Operand is TypeReference){
var tref = i.Operand as TypeReference;
ci = m1IL.Create(i.OpCode, targetType.Module.Import(tref));
}
if(ci != i){
m1IL.Replace(i, ci);
}
}
//here the source Body should have its Instructions set imported fine
//so we just need to set its Body to the target's Body
m2.Body = m1.Body;
//finally write to another output assembly
targetAsm.Write("modified_target_path");
The code above was not referenced from anywhere, I just tried it myself and found out it works for simple cases (such as for the 2 methods Test I posted above). But if the source method (defined in the current app) contains some Type reference (such as some constructor init ...), like this:
public class Form1 {
public string Test(){
var u = new Uri("SomeUri");
return u.AbsolutePath;
}
}
Then it will fail at the time writing the assembly back. The exception thrown is ArgumentException with the following message:
"Member 'System.Uri' is declared in another module and needs to be imported"
In fact I've encountered a similar message before but it's for method calls like (string.Concat). And that's why I've tried importing the MethodReference (you can see the if inside the foreach loop in the code I posted). And really that worked for that case.
But this case is different, I don't know how to import the used/referenced types (in this case it is System.Uri) correctly. As I know the result of Import should be used, for MethodReference you can see that the result is used to replace the Operand for each Instruction. But for Type reference in this case I totally have no idea on how.
All my code posted in my question is fine BUT not enough. Actually the exception message:
"Member 'System.Uri' is declared in another module and needs to be imported"
complains about the VariableDefinition's VariableType. I just import the instructions but not the Variables (which are just referenced exactly from the source MethodBody). So the solution is we need to import the variables in the same way as well (and maybe import the ExceptionHandlers as well because an ExceptionHandler has CatchType which should be imported).
Here is just the similar code to import VariableDefinition:
var vars = m1.Body.Variables.ToList();
m1.Body.Variables.Clear();
foreach(var v in vars){
var nv = new VariableDefinition(v.Name, targetType.Module.Import(v.VariableType));
m1.Body.Variables.Add(nv);
}

C# foreach against populated list is returning null

Following some major refactoring (moving to a PCL) I have some code (which was not part of the refactor) that was running fine but is now throwing exceptions.
The code is part of a Xamarin Android project which was using File Linking before the move to a Portable Class Library.
It's a simple thing but I can't see why this would happen
foreach(var station in stationList)
{
// Breakpoint on next line shows station to be null..!
if(station.ClusterId != Guid.Empty)
{
// Code in here
}
}
The problem is that although the stationList contains a number of StationViewModel objects the station instance is always null - how can that be?
I have tried replacing the foreach with a for loop but the result was the same - station was null.
I've also restarted Visual Studio and rebooted.
No Xamarin updates appear to be outstanding.
The code was running fine and the generation of the stationList has not changed nor has the implementation of this class.
EDIT:
The stationList creation process is:
Call made to SQLite 'repo' in PCL which returns IList<station> (which is populated)
_loadedStations = await _stationManager.GetStationsAsync();
Using AutoMapper a new List<StationViewModel> is generated from the above list (which is populated correctly)
fullStationList = AutoMapper.Mapper.Map<IList<Station>, IList<StationViewModel>>(_loadedStations);
In a separate method the view model list above is filtered based on the LatLng coordinates.
var stationList = fullStationList.Where(x => mapBounds.Contains(new LatLng(x.Latitude, x.Longitude))).ToList();
The foreach follows the above line of code..
SOLUTION:
Well I've 'solved' the problem but still don't know what caused it.
In the same method as the foreach there is another, contained within an if. It too has the station identifier;
if (zoomChanged)
{
foreach (var station in fullStationList)
{
station.ClusterId = Guid.Empty;
}
RunOnUiThread(() => _stationMap.Clear());
_clusters.Clear();
}
By changing either of the variable names the code will run fine and the previously erroring loop will run without any problem.
Note that this 2nd loop was not within the 1st one - that's obviously
not going to work, but I can't see why this was causing a problem.
It seems like this has something to do with the way Xamarin works , changing var name solves the issue
foreach(var stationItem in stationList)
{
// Breakpoint on next line shows station to be null..!
if(stationItem.ClusterId != Guid.Empty)
{
// Code in here
}
}
I tried it in this way and it works:
internal class CStation
{
private Guid _clusterId;
public CStation()
{
_clusterId = Guid.NewGuid();
}
public Guid StationName
{
get { return _clusterId; }
}
}
private voit TestList()
{
List<CStation> stationList = new List<CStation>();
CStation test1 = new CStation();
CStation test2 = new CStation();
CStation test3 = new CStation();
stationList.Add(test1);
stationList.Add(test2);
stationList.Add(test3);
foreach (var station in stationList)
{
// Breakpoint on next line shows station to be null..!
if (station == null )
{
throw new ArgumentNullException();
}
}
}
I think you didnt instantiate the Guid.
I did it in the constructor of Station -> _clusterId = Guid.NewGuid();

Custom workflow activity errors 'Value cannot be null'

I'm creating a custom workflow activity in VS2010 targeting .NET 3.5. The DLL is actually being used in a Microsoft System Center Service Manager custom workflow, but I don't think that is my issue.
I have a public string property, that the user types in the string of what the activity should use. However, when the WF runs, it errors out 'value cannot be null'. I want to target if it is my code or something else.
When we drag my custom activity onto the designer, I'm able to type in the text of the string on the designer for that property.
public static DependencyProperty ChangeRequestStageProperty = DependencyProperty.Register("ChangeRequestStage", typeof(String), typeof(UpdateChangeRequestStage));
[DescriptionAttribute("The value to set the ChangeRequestStage Property in the ChangeRequest Extension class.")]
[CategoryAttribute("Change Request Extension")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public String Stage
{
get { return ((String)(base.GetValue(UpdateChangeRequestStage.ChangeRequestStageProperty))); }
set { base.SetValue(UpdateChangeRequestStage.ChangeRequestStageProperty, value); }
}
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
EnterpriseManagementGroup emg = CreateEMG();
//System.WorkItem.ChangeRequest Extension - ClassExtension_928bec0a_cac4_4a0a_bd89_7146c9052fbe
ManagementPackClass mpcChangeRequest = emg.EntityTypes.GetClass(new Guid("8c6c6057-56ad-3862-47ec-dc0dde80a071"));
//System.WorkItemContainsActivity Relationship Class
ManagementPackRelationship workItemContainsActivityRelationship = emg.EntityTypes.GetRelationshipClass(new Guid("2DA498BE-0485-B2B2-D520-6EBD1698E61B"));
EnterpriseManagementObject changeRequest = null;
//Loop thru each emo (Change Request in this case), and assign it. There will never be more than 1 emo returned
foreach (EnterpriseManagementObject obj in emg.EntityObjects.GetRelatedObjects<EnterpriseManagementObject>(executionContext.ContextGuid, workItemContainsActivityRelationship, TraversalDepth.OneLevel, ObjectQueryOptions.Default))
{ changeRequest = obj; }
EnterpriseManagementObjectProjection emop = new EnterpriseManagementObjectProjection(changeRequest);
if (emop != null)
{ emop.Object[mpcChangeRequest, "ChangeRequestStage"].Value = Stage; }
emop.Commit();
return base.Execute(executionContext);
}
Since it is getting a 'value cannot be null' error, I'm guessing it's on this line:
emop.Object[mpcChangeRequest, "ChangeRequestStage"].Value = Stage;
I'm going to test and see if hardcoding a value works or not. Any ideas?
enter code here
try this
if (emop != null && emop.Object[mpcChangeRequest, "ChangeRequestStage"] != null)
emop.Object[mpcChangeRequest, "ChangeRequestStage"].Value = Stage
I didn't want to leave this question wide open, so I'm updating it as to how I resolved this (a long time ago).
Rather than working with an EnterpriseManagementObjectProjection (emop), I worked with a standard EnterpriseManagementObject (emo). From there, I was able to follow a similar format from above:
ManagementPackClass mpcChangeRequest = emg.EntityTypes.GetClass(new Guid("8c246fc5-4e5e-0605-dc23-91f7a362615b"));
changeRequest[mpcChangeRequest, "ChangeRequestStage"].Value = this.Stage;
changeRequest.Commit();

Categories

Resources