Browsing 7239 questions and answers with Jon Skeet

Mockito can't instantiate the class under test

I've got three classes A, B and C: public class A { @Autowired private B someB; private C someC = someB.getSomeC(); } @Service public class B { C getSomeC() { return...
Jon Skeet
people
quotationmark

Well this is always going to fail: public class A { private B someB; private C someC = someB.getSomeC(); } You're trying to call getSomeC() on a value which will always be null... that will always thrown NullPointerException. You... more 12/12/2013 10:27:23 AM

people

How to use call by reference(address) and default parameter in Java?

I am C++/C# developer and new to Java. Some reference found on internet suggests that we cannot have call by reference in Java. Is it completely true, or by any way I can return...
Jon Skeet
people
quotationmark

Some reference found on internet suggests that we cannot have call by reference in Java. Is it completely true Yes. Java is pass-by-value, always. The values which are passed are either references or primitive values. (Objects are... more 12/12/2013 10:20:35 AM

people

Retrieving sub elements of xml using Xdocument

I want to parse an XML file like this: <?xml version="1.0" encoding="utf-8" ?> <database name="myDb"> <table name="myTable"> <field...
Jon Skeet
people
quotationmark

This is the problem: foreach (XNode field in xdoc.Descendants("table").Nodes()) That's looking for all nodes under all table elements. You don't want that. You've already got the table you're looking at as an XElement - so use that. It... more 12/12/2013 8:04:32 AM

people

How to parse xml link tag href attribute using c#

This is the sample xml of a feed item <item> <pubDate>2013-12-11 10:28:55</pubDate> <title> SAG Awards Nominations: 12 Years a Slave, Breaking...
Jon Skeet
people
quotationmark

You need to specify the namespace when you look for the element: XNamespace atom = "http://www.w3.org/2005/Atom"; ... rss.Link1 = rssItem.Element(atom + "link").Attribute("href").Value; LINQ to XML makes namespace handling much simpler... more 12/12/2013 7:21:00 AM

people

Is size of protobuf serialized data fixed for simple classes?

By simple classes i mean classes/structures that do not contain vectors, stacks, linked list or any other variable length data structures. If we have a class with some simple...
Jon Skeet
people
quotationmark

You need to read the wire format documentation, basically. The size of floating point fields will always be fixed, but for integers there are options: fixed32 and fixed64 will be fixed (as the name suggests), but int32, int64, sint32,... more 12/12/2013 6:53:33 AM

people

Behavior of static blocks with Abstract classes

Child Class: public class ChildExtending extends ParentAbstract{ public int childInt =111213; static{ System.out.println("child static block executed"); } ...
Jon Skeet
people
quotationmark

Well first ChildExtending needs to be initialized (as a type). That will produce the output of parent static executed child static block executed as per section 12.4.2 of the JLS. Only once the type is initialized can the constructor... more 12/12/2013 6:50:33 AM

people

C# Documentation Plugin for Visual Studio

I am looking for a Visual Studio 2012 plugin / method that will allow me to access the .NET Framework 4.5 section of the MSDN library without an internet connection. Any...
Jon Skeet
people
quotationmark

You should just be able to install the Help Viewer (if you haven't already got it). In Visual Studio, under the "Help" menu, set the "Help Preference" to "Launch in Help Viewer" - it should download the relevant content for you if you... more 12/11/2013 9:56:05 PM

people

Return list of string from list nested in a list

I have three classes; State, County, and City. State contains a list of County. County contains a list of City. public class State { public string StateCode; ...
Jon Skeet
people
quotationmark

It sounds like you just need: return Counties.SelectMany(county => county.Cities) // IEnumerable<City> .Select(city => city.CityName) // IEnumerable<string> .ToList(); ... more 12/11/2013 8:57:52 PM

people

Main class cannot find .dat file

I am doing a simple exercise where a program reads data from a .dat file and prints it out on the console. I am working with NetBeans. I saved the .dat file in the default...
Jon Skeet
people
quotationmark

I am working with NetBeans. I saved the .dat file in the default package folder where the main file is located. And it cannot find the .dat file. Presumably that folder isn't the same as the working directory when you launch it. Use... more 12/11/2013 8:33:35 PM

people

Is there a performance boost by using yield with foreach?

After reading a few articles : http://csharpindepth.com/Articles/Chapter6/IteratorBlockImplementation.aspx http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx There is one...
Jon Skeet
people
quotationmark

I was curious to know a foreach loop can utilize yielded function's results before all the yields have been completed. Absolutely, that's part of the point. The iterator block executes lazily. The method effectively "pauses" when it... more 12/11/2013 6:29:21 PM

people