Understanding Error Report - c#

Can anyone help me understand this error report:
Error: Object reference not set to an instance of an object.
at Project.Web.Config.Data.DataProvider.get_ConfigSettingsConnectionString()
at Project.Web.Config.Data.DataProvider.GetConfigSettings()
at Project.Web.Config.ConfigBase.GetConfigSettings()
at Project.Web.Config.ConfigBase.GetConfigSettingValue(String configName)
at Project.Web.Config.ConfigBase.StringConfigSetting(String configName, String defaultValue)
at Project.Web.Config.Brands.get_BrandsConnectionString()
at Project.Web.Engine.Brands.Data.BrandsDataProvider.GetBrandByHostname(String hostname)
at Project.Web.Engine.Brands.Brands.CurrentBrand()
at Project.Web.Engine.Placeholders.BrandedResource.GetHtmlContent(String resourcePostingName, String resourceName, String cultureCode)
at Project.BWMembership.TranslationHelper.GetTranslation(String sResourcePostingName, String sResourceName, String sCultureCode)
at Project.BWMembership.TranslationHelper.TranslateCountries(ListResponse[] arrCountries, String sCultureCodes)
at ASP.update_aspx.Bind(Member oMembership, Order oOrder) in c:\Projects\Project\zsys_admin\update.aspx:line 82
at ASP.update_aspx.Page_Load(Object sender, EventArgs e) in c:\Projects\Project\zsys_admin\update.aspx:line 58
I know it is telling me there is a null object, and it is coming from:
at Project.Web.Config.Data.DataProvider.get_ConfigSettingsConnectionString()
And I know the rest of the data is a list of methods leading to the error
I am trying to identify what the null object is and how to resolve it.
I know each line corresponds to calling a method, for example, I can call the method:
Project.BWMembership.TranslationHelper.TranslateCountries(ListResponse[] arrCountries, String sCultureCodes)
By typing that in my code. I know this method is in the DLL:
Project.BWMembership
and in the class:
TranslationHelper
and the method is called:
TranslateCountries(ListResponse[] arrCountries, String sCultureCodes)
But when I type:
Project.Web.Config.Data.DataProvider.get_ConfigSettingsConnectionString()
It does not recognize this.
Visual studio will offer me the code up to:
Project.Web.Config
But from there will stop.
Is it the case that Config is a class name, and Data is an instance of a different DLL included in Project.Web and being instantiated in this method?
If so, can I assume there is a class called DataProvider in this DLL that has a method called Get_ConfigSettingsConnectionString() ?
and if this is the case, does this mean essentially this is trying to find some data that does not exist? For example it is looking for a connection string that either does not exist in the existing web config, or is looking for a web config that does not exist?
Or is it that Project.Web.Config refers to the config file of the DLL?
Which opens up a whole new set of questions!

There's no method named get_ConfigSettingsConnectionString(), but there's a propery named ConfigSettingsConnectionString (see this SO answer) in class DataProvider.
Generally speaking, you cannot infer the exact name of the DLL from full type name: for example, System.Int32 comes from mscorlib.dll.
Now, what the error you are having is telling you is that there's something inside get part of said property that is accessing a null object reference.

Related

New-WebServiceProxy error with message "could not load file or assembly"

I'm getting the following error when using the New-WebServiceProxy cmdlet:
Could not load file or assembly 'file:///.dll'
or one of its dependencies. The system cannot find the file
specified.
The WSDL was perfectly valid. After many hours of debugging, I found out that this message happens because the C# code generated for the Web Service classes is invalid. I don't know exactly why, but it appears that this error will always occur when the following naming pattern is used for the Web Methods:
[WebMethod]
public void AnyMethodName(...) { ... }
[WebMethod]
public void AnyMethodNameAsync(...) { ... }
I'm not sure of what is really happening, but I can tell that changing the name of the first method will work, as well as changing the order that the methods appear within the class - that is, the first method cannot be placed just before the second method.
The exception is thrown inside the method GenerateWebServiceProxyAssembly when returning the value of results.CompiledAssembly, as you can see here.
Does anyone knows what is really happening? Is this a bug of this cmdlet? Are there any solutions other than renaming the methods or chaging it's order inside the class?

Xamarin Android error: <identifier> expected package #string

In my app I tried reducing the amount of places I needed to update a string name, so I added a string in the Resources/values/Strings.xml file, but when I tried to test my build I got a weird error that I coudnt find a solution on, and have no idea why its doing that.
Someone know anything about this?
heres the xml code:
<resources>
<string name="service_name">ITMNotificationService</string>
</resources>
Using it in a Service attribute:
[Service (Label = "#string/service_name"
, Name = "#string/service_name"
, Icon = "#drawable/ITMIcon"
)]
And the error in the console;
The Name parameter for the ServiceAttribute is defining the Java class name that will be used in the ACW that is generated at compile time.
Thus the literal string of #string/service_name can not be used for a Java class name as that is invalid. Typically this would be the fully-qualified class name that includes your package id, ie.
Name = "com.sushihangover.coolapp.mywickedservice"
You could define this via a C# const string, but not an Android resource-based string value.
Note: You do not need to define the Name and Xamarin will auto-generated one via an MD5 hash routine. You then use typeof instead of the Java class name when calling the StartService method.

Member access call does not compile but static call does

So today I faced interesting problem while trying to build our company solution and I wanted to ask you guys do you know why is this happening. I've been told that it might be from my machine/visual studio because other people did not have same problem.
So we have a method in project A:
private static string RpcRoutingKeyNamingConvention(Type messageType, ITypeNameSerializer typeNameSerializer)
{
string queueName = typeNameSerializer.Serialize(messageType);
return messageType.GetAttribute<GlobalRPCRequest>() != null || AvailabilityZone == null
? queueName
: queueName + "_" + AvailabilityZone;
}
where GetAttribute<GlobalRPCRequest>() is defined in public static class ReflectionHelpers
public static TAttribute GetAttribute<TAttribute>(this Type type) where TAttribute : Attribute;
then we have project B which have method:
public static string GetAttribute(this XElement node, string name)
{
var xa = node.Attribute(name);
return xa != null ? xa.Value : "";
}
I have to point out that we have reference to project B in project A.
Now what happens is that when I try to build I get compile error:
Error 966 The type 'System.Xml.Linq.XElement' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. D:\Repositories\website\website\submodules\core\src\A\Extensions\Extensions.cs 37 13 A
Whats happening is that compiler thinks that I am actually using GetAttribute method from project B(in my opinion!). Why this is happening? Since when I try to navigate to GetAttribute VS leads me to the right method (the one that is in ReflectionHelpers).
Could it be because of the reflection? NOTE: I fixed this issue by calling the method statically or adding reference to System.Xml.Linq in my project A, but I am curious of the strange behavior of VS/syntax-checking feature.
It's a guess, but I think your function:
private static string RpcRoutingKeyNamingConvention(Type messageType, ITypeNameSerializer typeNameSerializer) does not match your helper method signature because you try returning a string:
public static TAttribute GetAttribute<TAttribute>(this Type type) where TAttribute : Attribute; which expects a TAttribute return type.
Maybe, you can try modifiying your function RpcRoutingKeyNamingConvention to return GlobalRPCRequest and check if the compiler continues to go crazy.
Visual Studio gets confused all the times! I tried to reproduce the scenario in my VS 2015 (.NET 4.6) and it compiles just fine. I didn't have to add reference to System.Xml.Linq in my Project A.
My guess is that it might be a cache issue. You might want to try this:
Remove reference to Project B
Clean then rebuild both solutions
Add the reference back
Rebuild and voila!! Well.. hopefully
Hope it helps, let me know :)
I guess that's going on:
- B has reference to System.Xml.Linq
- B is built without a problem.
- You are referencing B in A
- A hasn't got a reference to System.Xml.Linq
- A seem to consume the function defined in B
- When you try to build project A, it produces that error
Am I right?
If that's the case, it is totally normal. Because a project which consumes a reference (A) must have a reference to what is referenced (System.Xml.Linq) by what it references (B).
Think like this: When you try to add a nuget package to your project if it has a dependency, nuget will install it too. Why? Because of this situation.
This is completely normal if I understand your answer correctly.

Test always gives "Object reference not set to an instance of an object"

I am currently writing a unit test for class methods but accessor always throwing an exception.
Consider the following test method:
namespace GSVMTestCase
{
[TestClass]
public class GatewaySettingsViewModelTest
{
[TestMethod]
[DeploymentItem("SmartGatewayUABL.dll")]
public void AddGatewayTest1()
{
GatewaySettingsViewModel_Accessor target = new GatewaySettingsViewModel_Accessor(); // error occurs here
target.GatewayIPAddress = null;
target.AddGateway();
Assert.AreEqual(TextStrings.TxtErrInvalidIP, target.ErrorMessage);
}}}
}
always throwing exception:
Object reference not set to an instance of an object
hey got the solution of the problem while calling constructor there was configuration file was missing.So after adding app.config it get resolved. Thanks everyone for your valueable review
Given the naming scheme of what you're doing here, it looks as though you're employing some trickery to test this assembly. If this "SmartGatewayUABL.dll" is your assembly, then I'd add a project reference to the test assembly and test it without the deployment item stuff.
If it's not your assembly, then one would have to wonder why you're seeking to test a class from it. This is generally not desirable if you want to persist and maintain the test. If you're just experimenting, I'd suggest considering your experiment here at an end and trying something else.
Just to make it clearer for other people with this issue. Your main project can get data from the back-end to the front-end because a connectionString must be setup in its web.config file. So, you need to create a Application Configuration File called *.config in your unit test project and paste this connectionString inside.

Get name of project from where dll is referenced via code

I'm making a class library project in C#. How to get the name of the project which will call the method from my class library?
I tried with Reflection:
System.Reflection.Assembly.GetExecutingAssembly()
and with
GetCallingAssembly
but it didn't work.
I am looking for this also. File name I can get from StackFrame just like the answer by #Maheep but to get project name is not really straight forward.
A. I just got a simple solution:
Based on the FileName (path) found from StackFrame, iterate for each parent folder.
Find any file with *.csproj (C#)
Optionally open csproj file to ensure the current file <Compile Include="Folder\File.cs" /> is included
The file name of csproj is the project name
B. I found alternative by using pre-build macro $(ProjectDir) and access the result in code here: What's an easy way to access prebuild macros such as $(SolutionDir) and $(DevEnvDir) from code in C#?
If I understand your question directly, this is not possible to do in code. The only way to do this is with static analysis of the code.
Resharper has this ability - To find out where a particular class/method/property is used, you can right click on the declaration and select "Find Usages". This is a very handy feature :)
But that only works of the code calling your method is known (in the same solution). It wont work when third parties consume your library.
What exactly are you trying to achieve here? If your method needs to identify callers, you should add this as a requirement (ie add a parameter containing the caller identity).
public void MyMethod()
{
// I need name of caller project, but how?
}
public void MyMethod(String callerProject)
{
// People who call this method know the name of their own project :)
}
You will have to use StackTrace Class for this. StackTrace class has GetFrame method which will give you calling method name. This will return MethodBase class object which has property DeclaringType. Using this type information you can get assembly details as well.
private void YouCalledMethod()
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
Assembly assembly = stackFrame.GetMethod().DeclaringType.Assembly;
//use this assembly object for your requirement.
}
Look at this How to print the current Stack Trace in .NET without any exception? question also.

Categories

Resources