Lambda stack trace missing when using NativeMethodAccessor instead of GeneratedMethodAccessor - java

A couple days ago, I got a support ticket for this NullPointerException:
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract com.redacted.SalesResponsePagination com.redacted.StatisticsService.findSalesData(com.redacted.ConfStats) throws com.redacted.AsyncException' threw an unexpected exception: java.lang.NullPointerException
at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:389)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:579)
at ... (typical GWT + Tomcat stacktrace)
Caused by: java.lang.NullPointerException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.util.concurrent.ForkJoinTask.getThrowableException(Unknown Source)
at java.util.concurrent.ForkJoinTask.reportException(Unknown Source)
at java.util.concurrent.ForkJoinTask.invoke(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp.evaluateParallel(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateParallel(Unknown Source)
at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.util.stream.ReferencePipeline.forEach(Unknown Source)
at com.redacted.StatisticsControllerImpl.replacePrices(StatisticsControllerImpl.java:310)
at com.redacted.StatisticsControllerImpl.findSalesData(StatisticsControllerImpl.java:288)
at com.redacted.StatisticsServiceImpl.findSalesData(StatisticsServiceImpl.java:83)
at sun.reflect.GeneratedMethodAccessor752.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
... 33 more
Caused by: java.lang.NullPointerException
at com.redacted.StatisticsControllerImpl.lambda$replacePrices$27(StatisticsControllerImpl.java:317)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(Unknown Source)
at ... (typical stream.forEach stacktrace)
Now, this was an easy one because the exact line number for the NPE was in plain sight; all I had to do was go to StatisticsControllerImpl.java:317:
salesResponsePagination.getSalesResponses().parallelStream()
.peek(sr -> /*...*/)
.filter(sr -> /*...*/)
/*310*/ .forEach(sr -> {
final List<CartElement> sentCEs = DaoService.getCartElementDAO().getSentCEs(/*...*/);
if (sentCEs != null && !sentCEs.isEmpty() && sentCEs.get(0) != null) {
final CartElement ce = sentCEs.get(0);
// some more non-NPE lines...
/*317*/ if (sr.getCurrency().equals(ce.getPurchaseCurrency()) && sr.getPrice().equals(ce.getPurchasePrice().intValue()) && !ce.getCurrency().equals(ce.getPurchaseCurrency())) {
// Some currency exchanging
}
// Etcetera (about 12 lines more)
});
And replace the .equals() calls with Object.equals() to avoid the NPE (investigating the reasons why some sales were registered with a NULL price or currency came later). Test, commit, push, send ticket to QA.
However, the next day QA returned the ticket saying that the NPE persisted, and they included a new, almost similar stack trace:
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract com.redacted.SalesResponsePagination com.redacted.StatisticsService.findSalesData(com.redacted.ConfStats) throws com.redacted.AsyncException' threw an unexpected exception: java.lang.NullPointerException
at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:389)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:579)
at ... (typical GWT + Tomcat stacktrace)
Caused by: java.lang.NullPointerException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.util.concurrent.ForkJoinTask.getThrowableException(Unknown Source)
at java.util.concurrent.ForkJoinTask.reportException(Unknown Source)
at java.util.concurrent.ForkJoinTask.invoke(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp.evaluateParallel(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateParallel(Unknown Source)
at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.util.stream.ReferencePipeline.forEach(Unknown Source)
at com.redacted.StatisticsControllerImpl.replacePrices(StatisticsControllerImpl.java:310)
at com.redacted.StatisticsControllerImpl.findSalesData(StatisticsControllerImpl.java:288)
at com.redacted.StatisticsServiceImpl.findSalesData(StatisticsServiceImpl.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
... 33 more
Caused by: java.lang.NullPointerException
This stack trace was exactly the same as the previous one, except for two things:
This call was using NativeMethodAccessorImpl instead of GeneratedMethodAccessor752. Compare:
at com.redacted.StatisticsServiceImpl.findSalesData(StatisticsServiceImpl.java:83)
at sun.reflect.GeneratedMethodAccessor752.invoke(Unknown Source)
vs
at com.redacted.StatisticsServiceImpl.findSalesData(StatisticsServiceImpl.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
This one was missing the stack trace for the lambda. The line where the NPE happened was missing.
This threw me off initially. Why was the last part of the stack trace missing? I asked QA to re-test and re-attach the stack trace a couple times, until I got a complete one; however, the complete one I got in the end was based on GeneratedMethodAccesor once again.
Now, if I understand correctly, sun.reflect.NativeMethodAccessorImpl is used for the first invocations of a method, until JIT has enough info to generate an optimized accesor for that method in the form of sun.reflect.GeneratedMethodAccessorNNN.
What I don't understand is: if Native is using my code and Generated is using JIT's generated code, shouldn't Native show more info about my code, not less?
So my question is:
Why do lambda runtime exceptions thrown inside sun.reflect.NativeMethodAccessorImpl seem to be missing the lambda's stack trace?
Can this be a bug in JDK's source code? Especially when the very same exception thrown inside sun.reflect.GeneratedMethodAccessor includes the lambda stack trace without problem.
The following code manages to get a stack trace similar to the first one. I can force the use of NativeMethodAccessor or GeneratedMethodAccesor by running it with a sufficiently low or high first parameter, respectively (i.e. java test.Main 1 or java test.Main 30).
However, the lambda part of it is always present, whether using Native or Generated.
package test;
import java.lang.reflect.Method;
import java.util.stream.IntStream;
class MyOtherClass {
public void methodWithLambda(boolean fail) {
IntStream.range(0, 1000).parallel().forEach(k -> {
if (fail && k % 500 == 0)
throw new NullPointerException();
});
}
public String methodProxy(boolean fail) {
methodWithLambda(fail);
return "OK";
}
}
class MyClass {
public String methodReflected(Boolean fail) {
return new MyOtherClass().methodProxy(fail);
}
}
class Main {
public static void main(String[] args) throws Exception {
Class<MyClass> clazz = MyClass.class;
Object instance = clazz.newInstance();
Method method = clazz.getMethod("methodReflected", Boolean.class);
int reps = args.length >= 1 ? Integer.valueOf(args[0]) : 20;
for (; reps --> 0;) {
// Several non-failing calls to force creation of GeneratedMethodAccesor
System.out.println((String) method.invoke(instance, false));
}
// Failing call
System.out.println((String) method.invoke(instance, true));
}
}
Stack trace for the above code when using NativeMethodAccesor:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at test.Main.main(Main.java:36)
Caused by: java.lang.NullPointerException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.util.concurrent.ForkJoinTask.getThrowableException(Unknown Source)
at java.util.concurrent.ForkJoinTask.reportException(Unknown Source)
at java.util.concurrent.ForkJoinTask.invoke(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp.evaluateParallel(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfInt.evaluateParallel(Unknown Source)
at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.util.stream.IntPipeline.forEach(Unknown Source)
at java.util.stream.IntPipeline$Head.forEach(Unknown Source)
at test.MyOtherClass.methodWithLambda(Main.java:8)
at test.MyOtherClass.methodProxy(Main.java:14)
at test.MyClass.methodReflected(Main.java:21)
... 5 more
Caused by: java.lang.NullPointerException
at test.MyOtherClass.lambda$methodWithLambda$0(Main.java:10)
at java.util.stream.ForEachOps$ForEachOp$OfInt.accept(Unknown Source)
at java.util.stream.Streams$RangeIntSpliterator.forEachRemaining(Unknown Source)
at java.util.Spliterator$OfInt.forEachRemaining(Unknown Source)
at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
at java.util.stream.ForEachOps$ForEachTask.compute(Unknown Source)
at java.util.concurrent.CountedCompleter.exec(Unknown Source)
at java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
at java.util.concurrent.ForkJoinPool$WorkQueue.execLocalTasks(Unknown Source)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(Unknown Source)
at java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
at java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
EDIT: Just to be clear: I am not looking for ways to fix this NPE, nor ways to force the lambda's stack trace to print. What I want to know is the reason why the above happens: different implementations? A bug? Something to do with forEach()?

This might be an issue of JDK-6678999, “Stacktrace missing after null string comparisons”:
After comparing a string to null and catching the exception and repeating the operation, JVM starts throwing "stackless" NullPointerException (it occurs after 9000 loops but this is variable)
The evaluation of the issue is
When the server compiler compiles a method, the stack trace in an exception thrown
by that method may be omitted for performance purposes.
[…] If the user always wants stack traces, use the -XX:-OmitStackTraceInFastThrow option to the VM.
So, the option -XX:-OmitStackTraceInFastThrow may solve the issue.
Note that the bug report was against Java 6, but since it has been closed as “Won't Fix”, it may still be relevant, though you would have to replace “server compiler” with “c2 compiler” in the explanation now.
The use of NativeMethodAccessorImpl or GeneratedMethodAccessor… is not relevant to this issue, except that both have a common cause; a higher number of executions may trigger the optimizations.

Related

Catching Exceptions in lambda expressions

I have the simplest code using Files.walk:
Stream<Path> stream = Files.walk(Paths.get("C:\\"));
stream.forEach(f -> {
System.out.println(f);
});
This code throws
Exception in thread "main" java.io.UncheckedIOException: java.nio.file.AccessDeniedException: C:\Documents and Settings
at java.nio.file.FileTreeIterator.fetchNextIfNeeded(Unknown Source)
at java.nio.file.FileTreeIterator.hasNext(Unknown Source)
at java.util.Iterator.forEachRemaining(Unknown Source)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining (Unknown Source)
at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential (Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source)
at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.util.stream.ReferencePipeline.forEach(Unknown Source)
at devl.test.FindDuplicates.main(FindDuplicates.java:53)
Caused by: java.nio.file.AccessDeniedException: C:\Documents and Settings
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsDirectoryStream.<init>(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream (Unknown Source)
at java.nio.file.Files.newDirectoryStream(Unknown Source)
at java.nio.file.FileTreeWalker.visit(Unknown Source)
at java.nio.file.FileTreeWalker.next(Unknown Source)
... 11 more
And the stacktrace points to stream.forEach(f -> {. Adding a try/catch before the code makes it to stop reading the files when the exception is thrown.
The thing is - I want the code to continue reading different files on drive C even though the exception is thrown.
It seems like somehow adding a try/catch inside the foreach would solve it - how do I do it (a try/catch on f -> {, not the System.out.println(f);)?
Please note that I tried to circumvent this by doing
Stream<Path> s = stream.filter(f -> !f.toFile().getAbsolutePath().contains("Documents and Setting"));
s.forEach(f -> {
But the same exception is thrown (even doing stream.filter(f -> false) fails).
EDIT: Please note that I don't want to re-throw an exception (like I was suggested). So using s.forEach(LambdaExceptionUtil.rethrowConsumer(System.out::println)) still fails with the exact stacktrace.
To ignore errors during recursive directory traversal you need to use walkFileTree with a FileVisitor that does error handling.
The stream based convenience wrappers (walk) simply terminate on the first error they encounter, you can't prevent that.
If the listing of directories is what you're after, then why not use DirectoryStreamPaths instead? See the doc here for more: https://docs.oracle.com/javase/7/docs/api/java/nio/file/DirectoryStream.html
Furthere you can use it in conjuction with Java8 and what's more the exception handling is quite good.

java.lang.reflect.Proxy: Huge exception stack trace

Here is a simple Java application:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class Main {
interface MyInterface {
void myMethod();
}
public static void main(String[] args) {
MyInterface myInterface = (MyInterface) Proxy.newProxyInstance(Main.class.getClassLoader(), new Class[] {MyInterface.class},
new InvocationHandler() {
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(proxy, args);
}
});
myInterface.myMethod();
}
}
I would expect to get a simple StackOverflowError here, because I am recursively calling the same method on the proxy instance.
However, the stack trace produced by the exception contains millions of lines and hundreds of MBs in size.
The first part of the stack trace starts like this:
java.lang.reflect.UndeclaredThrowableException
at $Proxy0.myMethod(Unknown Source)
at Main.main(Main.java:22)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at Main$1.invoke(Main.java:18)
... 2 more
Caused by: java.lang.reflect.UndeclaredThrowableException
at $Proxy0.myMethod(Unknown Source)
... 7 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at Main$1.invoke(Main.java:18)
... 8 more
and extends to:
Caused by: java.lang.reflect.UndeclaredThrowableException
at $Proxy0.myMethod(Unknown Source)
... 1022 more
Then follow millions of lines like:
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at Main$1.invoke(Main.java:18)
at $Proxy0.myMethod(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at Main$1.invoke(Main.java:18)
.......
and:
Caused by: java.lang.reflect.UndeclaredThrowableException
at $Proxy0.myMethod(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at Main$1.invoke(Main.java:18)
at $Proxy0.myMethod(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at Main$1.invoke(Main.java:18)
.......
The last printed line is (after the repeating sequence above):
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "main"
I suspected that stack trace generation mechanism was calling some methods on the proxy instance (maybe toString to print it, or something else), thus repeating the recursion over and over again (because each method call on the proxy leads to infinite recursion), but the total count of proxy method executions is 1919 (measured by incrementing a counter in the InvocationHandler.invoke method).
In my real use case I fixed the infinite recursion issue of course; I am just curious if anyone knows if this is some bug or there is a reasonable explanation?
Java version:
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)
EDIT
#JiriTousek and #AndrewWilliamson kindly provided an analysis of what may be the cause for this. I implemented a simulation based on their input:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
public class Test {
private static int counter = 0;
public static void main(String[] args) {
proxy();
}
private static void proxy() {
try {
methodInvoke();
} catch (Throwable e) {
throw new UndeclaredThrowableException(e);
}
}
private static void methodInvoke() throws InvocationTargetException {
try {
myMethod();
} catch (Throwable e) {
throw new InvocationTargetException(e);
}
}
private static void myMethod() {
if (counter++ == 5) {
throw new StackOverflowError();
}
proxy();
}
}
This results in the following stack trace:
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at Test.proxy(Test.java:16)
at Test.main(Test.java:9)
Caused by: java.lang.reflect.InvocationTargetException
at Test.methodInvoke(Test.java:24)
at Test.proxy(Test.java:14)
... 1 more
Caused by: java.lang.reflect.UndeclaredThrowableException
at Test.proxy(Test.java:16)
at Test.myMethod(Test.java:32)
at Test.methodInvoke(Test.java:22)
... 2 more
Caused by: java.lang.reflect.InvocationTargetException
at Test.methodInvoke(Test.java:24)
at Test.proxy(Test.java:14)
... 4 more
Caused by: java.lang.reflect.UndeclaredThrowableException
at Test.proxy(Test.java:16)
at Test.myMethod(Test.java:32)
at Test.methodInvoke(Test.java:22)
... 5 more
Caused by: java.lang.reflect.InvocationTargetException
at Test.methodInvoke(Test.java:24)
at Test.proxy(Test.java:14)
... 7 more
Caused by: java.lang.reflect.UndeclaredThrowableException
at Test.proxy(Test.java:16)
at Test.myMethod(Test.java:32)
at Test.methodInvoke(Test.java:22)
... 8 more
Caused by: java.lang.reflect.InvocationTargetException
at Test.methodInvoke(Test.java:24)
at Test.proxy(Test.java:14)
... 10 more
Caused by: java.lang.reflect.UndeclaredThrowableException
at Test.proxy(Test.java:16)
at Test.myMethod(Test.java:32)
at Test.methodInvoke(Test.java:22)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at Test.methodInvoke(Test.java:24)
at Test.proxy(Test.java:14)
... 13 more
Caused by: java.lang.reflect.UndeclaredThrowableException
at Test.proxy(Test.java:16)
at Test.myMethod(Test.java:32)
at Test.methodInvoke(Test.java:22)
... 14 more
Caused by: java.lang.reflect.InvocationTargetException
at Test.methodInvoke(Test.java:24)
at Test.proxy(Test.java:14)
... 16 more
Caused by: java.lang.StackOverflowError
at Test.myMethod(Test.java:30)
at Test.methodInvoke(Test.java:22)
... 17 more
So, there is no line numbers growth for each stack frame.
My interpretation of this stack trace is:
When the stack overflow finally happened, a StackOverflowError was thrown
This error was caught by method.invoke() and translated into an InvocationTargetException (as per it's javadoc), with the original exception as a cause
Since this exception is a checked exception, the Proxy could not let it fall through itself, so it caught it and translated it into a UndeclaredThrowableException, again with the previous as a cause
This way, for each level of recursion before stack overflow you got two other "caused by" exceptions along with their stack traces - a lot of output. As for how much output, let's guesstimate it:
about 2000 invocations as per your post
in each invocation, the stack trace seems to grow by 5 lines
2 errors and stack traces printed for each invocation
So the biggest stack trace will have some 10000 lines, average stack trace will have about 5000 lines, everything is printed twice (once for each exception type), that sums up to about 2000 * 5000 * 2 = 20 millions of lines.
The printed stack trace is not truncated when depth of identical traces is longer than 1024 (default value). That's why the last truncated trace ends with ... 1022 more while all of the subsequent ones are printed fully.
The default can be changed by setting MaxJavaStackTraceDepth JVM argument to the desired value. When I increased it for the original example with Proxy by running it with -XX:MaxJavaStackTraceDepth=8192, the entire printed stack trace dropped to about 12500 lines, ending with:
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at Main1$1.invoke(Main1.java:16)
... 7003 more
Caused by: java.lang.reflect.UndeclaredThrowableException
at $Proxy0.myMethod(Unknown Source)
... 7007 more
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "main"

NUllPointerException on non null String

I am working an a small database assignment, and I seem to get a weird nullPointerException where I cannot find the problem
The following snippet is the error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at interfaces.FindDueActivityForFriend$1.actionPerformed(FindDueActivityForFriend.java:65)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
As you can see the problem occurs in the 65 of the FindDueActivityForFriend class, and to be more specific in this code snippet:
friendSearch = friendSearchField.getText();
console.out("Searching for the name that was entered");
console.out("friendSearch variable is: " + friendSearch );
if(con.GetNameExcists(friendSearch )){
//more code
the error occurs at the if statement.
In this if statement a class is called that connects to the database, checks if a name is already in the database, and then returns a true if that name is already in the database.
I have used that specific method around 10 times around the program without problems, but not this time.
the console.out("String"); that I am calling just before it is for a small console windows that I made that just prints what I tell it to, this is to have a console when not running the application out of for example eclipse.
I tried printing the variable just to see what it was, and it is not null.
following is a copy paste of the console:
[Wed May 14 20:04:26 CEST 2014] [INFO] Opened the findDueForFriend screen
[Wed May 14 20:04:30 CEST 2014] [INFO] Searching for the name that was entered
[Wed May 14 20:04:30 CEST 2014] [INFO] FriendSearch variable is: Ylva
I went on into ant debugger, and is doesn't even go in the method, it throws the nullpointer before actually opening the method
I don't have a clue what I am doing wrong anymore so if anyone has any suggestions please post them.
just for completeness here is the declaration of the variable aswell:
String friendSearch;
public FindDueActivityForFriend(Console Console){
console = Console;
friendSearch = "";
I even tried declaring the String as an empty string, not sure if that makes it not null but I think it should, and the console window clearly shows that the string contains something before throwing the nullpointer exception...
If it occurs in this if statement
if(con.GetNameExcists(friendSearch ))
and you know that friendSearch is not null, then con must be null, in order to get a NullPointerException in the if statement.
the only possibility with the below code is
if(con.GetNameExcists(friendSearch ))
either con is null or friendSearch is null which you might be accessing at the
begining of the method and performing some operation like friendSearch.caoncat or something.
mostly it should be con is null it seems.
try to print con value inside log.
if con.getNameExcists(...) return Boolean instead of boolean and possibly returns null, then you get a NullPointerException from auto-unboxing.

"Prohibited package name: java.util" on Class.forName("java.util...") in applet

I have an applet that references 2 signed jars:
myapplet.jar
jackson-all-1.9.9.jar
When starting the applet the second time (first time is without errors), I get this:
Exception in thread "thread applet-main.MyApplet-1"
java.lang.ExceptionInInitializerError
at org.codehaus.jackson.map.deser.StdDeserializerProvider.<init>(StdDeserializerProvider.java:81)
at org.codehaus.jackson.map.ObjectMapper.<init>(ObjectMapper.java:398)
at org.codehaus.jackson.map.ObjectMapper.<init>(ObjectMapper.java:358)
at org.codehaus.jackson.map.ObjectMapper.<init>(ObjectMapper.java:328)
at net.Remote.<init>(Remote.java:50)
at main.Env.init(Env.java:44)
at main.MyApplet.init(MyApplet.java:25)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.SecurityException: Prohibited package name: java.util
at java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.codehaus.jackson.map.deser.BasicDeserializerFactory.<clinit>(BasicDeserializerFactory.java:74)
... 9 more
The line in question is the first one in the following try-catch block:
try {
Class<?> key = Class.forName("java.util.ConcurrentNavigableMap");
Class<?> value = Class.forName("java.util.ConcurrentSkipListMap");
#SuppressWarnings("unchecked")
Class<? extends Map<?,?>> mapValue = (Class<? extends Map<?,?>>) value;
_mapFallbacks.put(key.getName(), mapValue);
} catch (ClassNotFoundException cnfe) { // occurs on 1.5
}
A couple of things I do not understand:
Why does my Java7 JVM not take it out of its runtime library? But rather
Why does it try to download /java/util/ConcurrentNavigableMap.class from my server, which obviously fails with a 404?
As that fails, why does it try to re-download myapplet.jar 25 times in rapid succession, each time successfully (200), and each time returning the same jar file?
Update I'm not sure whether the 25 retries are caused by the class loader trying to load the class, it might be some other code trying to load a resource (which would still be odd, but not related to the CurrentNavigableMap issue), so I'll exclude that from my question.
N.B. I guess it does not try to re-download the jackson jar file, as that one is listed in the cache_archive attribute.
Is this?
wrong:
Class.forName("java.util.ConcurrentNavigableMap");
Correct:
http://java.sun.com/javase/ja/6/docs/ja/api/java/util/concurrent/package-tree.html
Class.forName("java.util.concurrent.ConcurrentNavigableMap");

App Engine / Quercus datastore prepare query error

I'm trying to replicate the java guestbook example on Quercus on AppEngine and I'm getting an error having to do with preparing the query:
$greetings = $datastore->prepare($query)->asIterable();
I'm not a java developer so I can't make sense of the error trace. How can I get the greeting items without triggering this error?
Here is the entire error page:
HTTP ERROR 500
Problem accessing /index.php. Reason:
INTERNAL_SERVER_ERROR
Caused by:
java.lang.NullPointerException at
com.google.appengine.api.datastore.dev.LocalDatastoreService.next(LocalDatastoreService.java:1089)
at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at
com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.callInternal(ApiProxyLocalImpl.java:498)
at
com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:452)
at
com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:430)
at java.util.concurrent.Executors$PrivilegedCallable$1.run(Unknown
Source) at java.security.AccessController.doPrivileged(Native Method)
at java.util.concurrent.Executors$PrivilegedCallable.call(Unknown
Source) at java.util.concurrent.FutureTask$Sync.innerRun(Unknown
Source) at java.util.concurrent.FutureTask.run(Unknown Source) at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
If it's a null pointer exception then you are trying to access a method or property of a null object
First you need to check if $datastore is null, then if the return of the query is not null. Also you need to check if that error is on that particular line of code (maybe it fails somewhere else)
You can access the database at this link /_ah/admin. Maybe there is a corrupt entity in there

Categories