I am trying the following simple code, which works as per expectation on my localmachine
public class NewTest
{
#Test
public void f() throws IOException
{
Properties obj = new Properties();
FileInputStream fileobj = new FileInputStream("C:\\selenium_Automation\\data_links.properties");
obj.load(fileobj);
System.setProperty("webdriver.chrome.driver", "c:\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(obj.getProperty("crm_url"));
System.out.println("Complete");
}
}
but when i try the same code on a different machine i get the following
FAILED: f
java.lang.NullPointerException: null value in entry: url=null
at com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:33)
at com.google.common.collect.SingletonImmutableBiMap.<init>(SingletonImmutableBiMap.java:39)
at com.google.common.collect.ImmutableBiMap.of(ImmutableBiMap.java:57)
at com.google.common.collect.ImmutableMap.of(ImmutableMap.java:80)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:306)
The code works fine if i replace (obj.getProperty("crm_url")) with the actual URL, but i have several different links stored in the properties file and i need them to be read from that place. What i am doing wrong can some please tell me the reason behind the NUll pointer expection for the URL
This is the error you get when you try to add a null object to an immutable map in the google common library. My guess would be that the org.openqa.selenium.remote.RemoteWebDriver.get is attempting to find do that and your file path is null or something similar. I would check the url but that is only a guess.
If I had to guess, I would say that the location of your properties file is different on the other machine.
The issue here I can assume is that the url you are using in the properties file must be wrong or it might have the inverted commas. If it has the inverted commas i.e
url="https://www.google.com" then remove it.
Even I had faced this issue. I deleted the target folder before running the code and my issue was resolved.
Related
I'm following a course on how to use Dapper however I've come across an error I'm unable to find a solution for.
The error being "System.InvalidOperationException: 'The ConnectionString property has not been initialized.'"
I've done some debugging and have noticed that when my initialize function reads from the appsettings.json file, nothing seems to be available within the "config" variable that is returned.
This also seems to be the case for when the function CreateRepository is called after, a null string is being sent to the repository.
Would appreciate if someone can see what seems to be wrong?
Initialize - object of 0 options returned to config
private static void Initialize()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
config = builder.Build();
}
Contact Repository - when setting a breakpoint in ContactRepository - the parameter passed is null
private static ContactRepository CreateRepository()
{
return new ContactRepository(config.GetConnectionString("DefaultConnection"));
}
App Settings
{
"ConnectionStrings": {
"DefaultConnection": "server=.\\SQLEXPRESS2014;database=ContactsDB;Trusted_Connection=Yes;"
}
}
The issue seemed to have been that the "AppSettings.json" file wasn't being added to the location where builder.SetBasePath was hence why it was returning null. A manual copy and paste of the file fixed the issue
I am getting the following error when trying to run fortify scan on my silverlight project.
The method createObject() in Silverlight.js can dereference a null pointer on line 2,
thereby raising a NullExcpetion.
This silverlight.js file is created by Microsoft. Does anybody have pointers on how this can be resolved?
Given that the file has come from Microsoft I would assume that the error is theoretical rather than actual. Checking the debug version of the file for CreateObject I find the following code:
Silverlight.createObject = function(source, parentElement, id, properties, events, initParams, userContext)
{
var slPluginHelper = new Object();
var slProperties = properties;
var slEvents = events;
slPluginHelper.version = slProperties.version;
slProperties.source = source;
slPluginHelper.alt = slProperties.alt;
...
I can only assume that fortify is objecting to the following line:
slPluginHelper.version = slProperties.version;
as slProperties could be null if the properties argument is null.
If you really must have these errors fixed then you need to report the issue to Microsoft and hope that they release an updated version of the file. In the meantime you could modify the code (edit the .debug version of the file) and use that. However, you will have to reapply your edits if you download a new version of the file at any point in the future.
The assembly qualified string used as a parameter below for a Uri works in XAML, but gives me the error shown when used in code.
I tried every kind of UriKind with the same result. How can I fix this?
[Test]
public void LargeImageSource_IsKnown()
{
var uri = new Uri(
"pack://application:,,,/" +
"MyAssembly.Core.Presentation.Wpf;component/" +
"Images/Delete.png", UriKind.RelativeOrAbsolute);
Assert.That(
_pickerActivityCollectionVm.DeleteActivityCommand.LargeImageSource,
Is.EqualTo(uri));
}
System.UriFormatException : Invalid URI: Invalid port specified.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Uri..ctor(String uriString, UriKind uriKind)
UPDATE
Based on Thomas' superb answer and my own comments about readability, I wound up using the following in my BaseTestFixture class. Hope this helps someone else.
protected virtual void OnFixtureSetUp() {
// logging, other one time setup stuff...
const string scheme = "pack";
if (!UriParser.IsKnownScheme(scheme)) {
Assert.That(PackUriHelper.UriSchemePack, Is.EqualTo(scheme));
}
}
That's because you're executing this code while the pack:// scheme is not yet registered. This scheme is registered when you create the Application object. You can add this code in the setup of your test fixture:
[SetUp]
public void Setup()
{
if (!UriParser.IsKnownScheme("pack"))
new System.Windows.Application();
}
EDIT: actually it seems the pack:// scheme is registered in the type initializer of the PackUriHelper class (which happens to be used by the Application class). So actually you don't need to create an instance of Application, you only need to access a static member of PackUriHelper to ensure the type initializer has run:
[SetUp]
public void Setup()
{
string s = System.IO.Packaging.PackUriHelper.UriSchemePack;
}
It appears that accessing PackUriHelper.UriSchemePack only registers the pack scheme, not the application scheme, which I needed to use the pack://application:,,,/ syntax in my unit tests. I therefore had to use the new Application() approach, which worked fine for registering both schemes.
If you're seeing this error in a Windows Store / WinRT project:
I wasn't able to use the "pack://" syntax at all when trying to load a resource in my C# app. What worked was ms-appx:// syntax of this kind:
ms-appx://[project folder]/[resource path]
For example, I wanted to load a resource dictionary named "styles.xaml" from a folder "core". This URI ended up working for me:
dictionary.Source = new System.Uri("ms-appx:///core/styles.xaml");
Even though the question specified WPF, the problem seemed extremely similar but ended up having a completely different solution, which took a while to find, and existing answers didn't help at all.
Again, this solution does not apply to WPF
I am having trouble instantiating a view model wich loads an image from Resources.
The line that fails in the assembly I'm testing is:
get { return new ImageSourceConverter().ConvertFromString("pack://application:,,,/Resources/Icons/Commands/DisabledNewSessionIcon.png") as ImageSource; }
the exception is:
Unable to create instance of class
GPAnalysisSuite.Tests.View_Models.Session_Controller.SessionControllerViewModel_NonDefaultConstructorTester.
Error:
System.IO.DirectoryNotFoundException:
Could not find a part of the path
'C:\TGP\GP Analysis
Suite\Application\Tests\TestResults\Paul_PAUL-GP
2011-03-17
11_27_28\Out\Resources\Icons\Commands\DisabledNewSessionIcon.png'..
I have already found a solution to a simular problem and included the following to the TestClass:
[AssemblyInitialize]
public static void InitialisePackageUriHelper(TestContext context)
{
PackUriHelper.Create(new Uri("reliable://0"));
new FrameworkElement();
System.Windows.Application.ResourceAssembly = typeof(App).Assembly;
}
I can see that I need to preserve the Uri of the assembly I'm testing but have no idea how to do it, can anyone help?
I appear to have solved the problem by changing the resource build action from Content to Resource.
Although I have to rebuild the solution everytime I want to run the unit tests this is now workable at least.
I'm developing my first windows phone 7 app, and I've hit a snag. basically it's just reading a json string of events and binding that to a list (using the list app starting point)
public void Load()
{
// form the URI
UriBuilder uri = new UriBuilder("http://mysite.com/events.json");
WebClient proxy = new WebClient();
proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(OnReadCompleted);
proxy.OpenReadAsync(uri.Uri);
}
void OnReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
var serializer = new DataContractJsonSerializer(typeof(EventList));
var events = (EventList)serializer.ReadObject(e.Result);
foreach (var ev in events)
{
Items.Add(ev);
}
}
}
public ObservableCollection<EventDetails> Items { get; private set; }
EventDetails is my class that wraps the json string. this class has to be correct because it is an exact copy of the class used by that website internally from which the json is generated...
I get the json string correctly from the webclient call (I read the memorystream and the json is indeed there) but as soon as I attempt to deserialize the string, the application exits and the debugger stops.
I get no error message or any indication that anything happen, it just stops. This happens if I type the deserialize method into the watch window as well...
I have already tried using JSON.net in fact I thought maybe it was a problem with JSON.net so I converted it to use the native deserializer in the .net framework but the error is the same either way.
why would the application just quit? shouldn't it give me SOME kind of error message?
what could I be doing wrong?
many thanks!
Firstly, the fact that you have some string there that looks like JSON does not mean that you have a valid JSON. Try converting a simple one.
If your JSON is valid, it might be that your JSON implementation does not know how to convert a list to EventList. Give it a try with ArrayList instead and let me know.
The application closes because an unhandled exception happens. If check the App.xaml.cs file you will find the code that closes your app. What you need to do is try catch your deserialization process and handle it locally. So most likely you have some JSON the DataContractJsonSerializer does not like. I have been having issue with it deserializing WCF JSON and have had to go other routes.
You may want to check to ensure your JSON is valid, just because your website likes it does not mean it is actually valid, the code on your site may be helping to correct the issue. Drop a copy of your JSON object (the string) in http://jsonlint.com/ to see if it is valid or not. Crokford (the guy who created JSON) wrote this site to validate JSON, so I would rely on it more than your site ;) This little site has really helped me out of some issues over the past year.
I ran into this same kind of problem when trying to migrate some existing WM code to run on WP7. I believe that the WP7 app crashes whenever it loads an assembly (or class?) that references something that's not available in WP7. In my case, I think it was Assembly.Load or something in the System.IO namespace, related to file access via paths.
While your case might be something completely different, the symptoms were exactly the same.
The only thing I can recommend is to go through the JSON library and see if it's referencing base classes that are not allowed in WP7. Note that it doesn't even have to hit the line of code that's causing the issue - it'll crash as soon as it tries to hit the class that contains the bad reference.
If you can step into the JSON library, you can get a better idea of which class is causing the problem, because as soon as the code references it, the whole app will crash and the debugger will stop.