Browsing 7239 questions and answers with Jon Skeet

Java + Eclipse: False possible null pointer dereference?

I'm experiencing a warning in Eclipse regarding a "possible null pointer dereference" but i really don't get why. The warning occurs in the following code snippet in the last...
Jon Skeet
people
quotationmark

You're assuming that just because view.get() returns a non-null value the first time, it will return a non-null value the second time. It may not - the target could be removed between the two calls. You could eliminate this with: if (view... more 10/17/2013 9:45:06 AM

people

Undefined constructor error in a double bracket collection initializer

In the following code the getEntriesNotWorking method reports a compile-time error: public class DoubleBracketInitializerTest { class Container {} class Entry { ...
Jon Skeet
people
quotationmark

The problem is that within new ArrayList<Entry>() { ... }, this is an ArrayList<Entry>. The clue is in the error message, which specifies which constructor signature it's looking for. If you really want to use this sort of... more 10/17/2013 9:23:09 AM

people

How can I use the async keywords in a project targeting.net 4.0

I would like to use the async keywords in a project that is created in .net 4.0. If I go to the nuget.org website and I look for "async", i many results, but mainly I get...
Jon Skeet
people
quotationmark

You want the Microsoft.Bcl.Async package. That's a properly released, non-CTP package that is stable. This also requires VS2012 since an updated compiler is needed to understand async and await. In theory one could use older tools from... more 10/17/2013 8:41:00 AM

people

Capture all return values from a delegate

I was playing around with delegates and anonymous methods and came across the code below: delegate int IntegerGenerator(); static void Main() { IntegerGenerator...
Jon Skeet
people
quotationmark

You'd need to use MulticastDelegate.GetInvocationList and invoke each delegate separately. For example: List<int> results = numberGenerator.GetInvocationList() .Cast<IntegerGenerator>() ... more 10/17/2013 8:31:00 AM

people

Why does hyper threading benefit my algorithm?

I have a dual core machine with 4 logical processors thanks to hyper-threading. I am executing a SHA1 pre-image brute force test in C#. In each thread I basically have a for loop...
Jon Skeet
people
quotationmark

Hyperthreading effectively gives you more cores, for integer operations - it allows two sets of integer operations to run in parallel on a single physical core. It doesn't help floating point operations as far as I'm aware, but presumably... more 10/17/2013 6:57:03 AM

people

while(true) and Collections

I can't understand the difference between these two codes; public static void main(String[] args) { List<String> list = new ArrayList<>(); while (true) { ...
Jon Skeet
people
quotationmark

Simply put - it takes quite a while to display 20 million lines of text. It's easy enough to show that. Run this code: for (int x = 0; x < 20767725; x++) { System.out.println(x); } I suspect that'll take about 5 minutes as well. more 10/17/2013 6:53:21 AM

people

Output Stream NullPointerException

I'm trying to get the string input from the user and store it in a file but an error comes out saying: I don't know what the issue is, it doesn't pop up as an error until I...
Jon Skeet
people
quotationmark

This is the problem: String nameToWrite = fn; ... outStream.write(nameToWrite.getBytes()); fn will only have a non-null value after actionPerformed has been called at least once. So when you call the constructor, fn is null, so... more 10/16/2013 8:37:26 PM

people

Quickest way to Read Specific Line from Multiple Files one Line at a time

I have a text-based database that represents logs, sorted by timestamp. For testing purposes my database has approximately 10,000 lines but this number can be larger. It is of the...
Jon Skeet
people
quotationmark

It sounds like you're going to want to have in memory (for display in textboxes) everything that the user selects - so that's a natural boundary for what's feasible anyway. I suggest the following approach: Read all of the matching... more 10/16/2013 8:31:44 PM

people

Why my data type is not automatically promoted to Double

I know that a data type is automatically promoted to the upper data type byte-short-int class Temp { void check(byte x) { System.out.println(x + " is the byte...
Jon Skeet
people
quotationmark

my question is that why b*b promote to int Because that's what the language specification says it will do. and why [...] this line got 0 why not promote to that double Again, because that's not how the language is... more 10/16/2013 7:58:24 PM

people

Object Lock for unique values

Hi I have one synchronized method which returns ms. Can anyone tell whether where each object will get the unique value in below code. public static synchronized Long...
Jon Skeet
people
quotationmark

No - there's no guarantee that each call will return a different value. It's not inconceivable that the call (including synchronization) could take less time than the granularity of the internal clock used for nanoTime(). (Indeed, I can... more 10/16/2013 7:05:07 PM

people