Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  NPE's sometimes get ignored?  (Read 1736 times)
0 Members and 1 Guest are viewing this topic.
Offline JonathanC

Senior Newbie





« Posted 2008-04-16 06:13:21 »

I just spent hours narrowing down a phantom problem, only to realize that there was a NPE being thrown, but that never hit the console for some reason. Only after I put an explicit try/catch block around the faulty code, did the NPE get thrown, but this is hardly standard protocol at all. NPE's are thrown, regardless, right?

Is there some reason for these NPE's to get consumed and never reported? Given the frequency of this kind of exception, this is terribly difficult to debug.
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #1 - Posted 2008-04-16 08:50:25 »

I've never had that happen to me. Uncaught exceptions that bubble all the way to the top of the stack (or is it bottom?) always print a stacktrace in stderr for me.

Are you sure there's no "catch {Exception e} { /* todo: report this */}" anywhere in the code?

Play Minecraft!
Offline g666

Junior Devvie





« Reply #2 - Posted 2008-04-16 09:38:21 »

long shot but there could be a default exception handler that simply ignores it?

desperately seeking sanity
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #3 - Posted 2008-04-16 09:43:31 »

Oh, cool, I never knew that existed! I've learned something new today! =D

Play Minecraft!
Offline fletchergames

Senior Devvie





« Reply #4 - Posted 2008-04-16 16:43:42 »

It seems most likely to me that it was getting thrown and just not getting caught.

When an exception is thrown but not caught, the thread the exception was thrown in should die.  If it was the main thread, the JVM will output a message to the console.  If it wasn't the main thread, the thread will just die without outputting anything to the console.

Did the NPE occur outside of the main thread?  Even if you don't have any threads of your own, it might have occurred on the AWT Event Thread.

I had a try-catch block in the main method that caught all exceptions.  But when I had an exception on the AWT Event Thread or a different thread, it just caused the thread to die.  At first, I started inserting try-catch blocks in all my run methods in Runnables and anywhere else I had a thread.

Then I wrote the following class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
package cg.concurrent;
import cg.error.*;

/**This class defines a Runnable that can't silently die due to an exception.
 * @author Steven Fletcher
 * @since 2008/02/23
 * @version 2008/02/23
 */

public abstract class SafeRunnable implements Runnable {
/**Constructor.
 * @param name the name of this SafeRunnable (to be output in error messages)
 */

public SafeRunnable(final String name) {
   this.name = name;
} //end constructor

/* (non-Javadoc)
 * @see java.lang.Runnable#run()
 */

public final void run() {
   try {
      runSafely();
   } catch(Throwable throwable) {
      ErrorStream.output(throwable, ConcurrentErrorList.safeRunnableRunFailed, name);
   }
} //end run

//PROTECTED METHODS//////////////////////////////////////////////////////////////////////
/**The method that will be run within a try-catch block.
 */

protected abstract void runSafely();

//VARIABLES//////////////////////////////////////////////////////////////////////////////
private final String name;
} //end class SafeRunnable


It's just like a Runnable except that you implement the runSafely method instead of the run method.

And I put the error message in a separate file (I like to put all the error messages for a package in one file):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
package cg.concurrent;

/**This class stores error messages for the cg.concurrent package.
 * @author Steven Fletcher
 * @since 2008/02/23
 * @version 2008/02/23
 */

interface ConcurrentErrorList {
static final String safeRunnableRunFailed = "SafeRunnable [%s]'s run method failed.";
} //end interface ConcurrentErrorList
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #5 - Posted 2008-04-16 20:08:57 »

If it wasn't the main thread, the thread will just die without outputting anything to the console.

I tested it today at work, and all threads (even daemon ones) spit out the exception to stderr when they die from exception.
I've got some flavor of java 1.6 at work, and this statement is based on experimentation, not documented truth.

Play Minecraft!
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2008-04-16 20:41:57 »

Uncaught Throwables ALWAYS get printed to STDERR, except for ThreadDeath, if there is no user-defined DefaultExceptionHandler


This statement:
Quote
If it wasn't the main thread, the thread will just die without outputting anything to the console.
is false, and never ever has been true.

Just think of the debug-nightmare...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline fletchergames

Senior Devvie





« Reply #7 - Posted 2008-04-17 15:11:05 »

I tested it today at work, and all threads (even daemon ones) spit out the exception to stderr when they die from exception.
I've got some flavor of java 1.6 at work, and this statement is based on experimentation, not documented truth.
I see.

It must be something screwy with my code or with how I have my environment set up.  In any case, I made it work for my situation with the SafeRunnable class.

Perhaps I was catching the exceptions and ignoring them somewhere without realizing it.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (24 views)
2014-11-25 12:00:59

SHC (23 views)
2014-11-25 11:53:45

Norakomi (19 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (68 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!