Browsing 7239 questions and answers with Jon Skeet

java.lang.NoSuchMethodError on raspberry pi only

I'm running Eclipse Kepler Service Release 2. My program works fine when I run it in Eclipse, and it also works fine when I run the .jar using windows cmd. However, putting that...
Jon Skeet
people
quotationmark

You're trying to use java.nio.file.Files.readAllLines(Path), which was introduced in Java 8. You're not going to be able to use that in Java 7. Options: Upgrade to Java 8 on the raspberry pi Don't use any classes/methods which are... more 5/30/2014 2:59:53 PM

people

Best way to implement a generic method in a type generic/agnostic way

Suppose we have a generic method with such signature: T Obfuscate<T>(T value) where T : IConvertible I'm setting type constraint to IConvertible so this method can digest...
Jon Skeet
people
quotationmark

As you say, you've got to perform type checking of some description. However, you can easily break it down into smaller methods and even have an open registration scheme: private readonly Dictionary<Type, Delegate> obfuscators = ... more 5/30/2014 2:37:36 PM

people

How To Remove Last Node In XML? C#

I am trying to remove the last node from an XML file, but cannot find any good answers for doing this. Here is my code: XmlReader x = XmlReader.Create(this.PathToSpecialFolder +...
Jon Skeet
people
quotationmark

It sounds like you want something like: var doc = XDocument.Load(path); var lastFile = doc.Descendants("File").LastOrDefault(); if (lastFile != null) { lastFile.Remove(); } // Now save doc or whatever you want to do with it... more 5/30/2014 2:20:42 PM

people

Class specific method visibility

Is there some object oriented thing that you can call some methods from certain classes, but not all of them? Is there something like that which is similiar to protected? Say you...
Jon Skeet
people
quotationmark

No, there's nothing like that in Java. The closest you've got is putting classes within the same package, at which point they have access to any members which don't specify any access modifier. You can't specify particular classes... more 5/30/2014 12:45:28 PM

people

Adding to or updating a dictionary with not reference type value

Before flagging this as a duplicate please double check. I'm accumulating some stats in dictionary. Dictionary<string, int> stats; When an event happens I update...
Jon Skeet
people
quotationmark

You can fetch the value when you check for containment. In fact, in this case you can make it unconditional: int value; stats.TryGetValue(eventName, out value); stats[eventName] = value + eventValue; This uses the fact that value will... more 5/30/2014 10:24:30 AM

people

Is it necessary to await a single asynchronous call

If I'm returning the result of a single asynchronous function e.g. in a service between application layers, is there any difference between: public Task<byte[]>...
Jon Skeet
people
quotationmark

There are a few very subtle differences in some cases - for example, if the original Task returns with a status of Faulted but with an OperationCanceledException then the async version would return a task with a status of Canceled... but... more 5/30/2014 8:58:40 AM

people

char operations on byte arrays in Java

I have a byte array which "contains" text - the encoding/charset is unkown at this time. How can I remove whitespace, \n, \r characters, of course without creating a String...
Jon Skeet
people
quotationmark

I have a byte array which "contains" text - the encoding/charset is unkown at this time. If you don't know the encoding, then there is simply no concept of whitespace, \r, \n characters etc. Those characters could map to any... more 5/30/2014 7:51:21 AM

people

How to document old value vs new value in generic function callback

I have a generic callback function like this to notify listeners when a value has changed: public Func<T, T, Task> ValueChangedAsync { get; set; } where the first T...
Jon Skeet
people
quotationmark

You could do this with events and a custom delegate type, where the delegate type documentation can indicate the old value and new value parts. For example: // As we want a non-void return type, we're not following the event... more 5/30/2014 7:15:28 AM

people

Losing time when converting from Epoch time

I am getting the time in EpochTime. When I convert to local time using the funciton, I am losing 1 hour. I am not sure if the calculation is wrong. can you review it. ...
Jon Skeet
people
quotationmark

The problem is that you're adding seconds after converting to local time. In other words, you're converting the Unix epoch to EST, then adding 1401395106 seconds. Insteead, you should be adding 1401395106 seconds to the Unix epoch, and... more 5/29/2014 8:45:17 PM

people

Why does casting null to type for method selection work?

According to the answer to C#: Passing null to overloaded method - which method is called?, nulls seem to carry type information. In fact, I can also use class Program { ...
Jon Skeet
people
quotationmark

No, the null itself doesn't carry the type information. The cast just tells the compiler what the type of the variables a and b should be... it can't tell without the cast, because null is convertible to any reference type or nullable... more 5/29/2014 8:39:28 PM

people