Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-28-2008, 04:28 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,556
Norm is on a distinguished road
Debugging - Finding runtime problems
Java debugging

I've never had an IDE with interactive debugging and have had to rely on print statements to tell me where the program is executing and how variables values are changing.

For example: System.out.println("Here I am. x=" + x);

Often conditional using a boolean so I can turn the output on and off with a single statement change:
Code:
boolean debug = true; ... if(debug){ System.out.println("Here I am. x=" + x); }
To find out how a method is being called, I add the following statement to print the call stack:
Code:
try{throw new Exception("who called");}catch(Exception ex) {ex.printStackTrace();}
You'll need the -g option on the javac compile to have the statement numbers in the code.

If the method is called many times you may only want to see it once:
Code:
boolean oneTime = true; .... if(oneTime) { oneTime = false; try{throw new Exception("who called");}catch(Exception ex) {ex.printStackTrace();} }

Finding NullPointerException in large program that doesn't have enough try{}catch blocks:
Copy the NullPointerException.java class from the source.zip file for your JDK and modify:
Code:
// Insert our own special debugging version public String getMessage() { try{throw new Exception("Where am I");}catch(Exception ex){ex.printStackTrace();} return super.getMessage(); }
Compile it and put the class file(s) in its own jar file. Prepend that jar file to the classpath for the java program using the -Xbootclasspath option:
Code:
java -Xbootclasspath/p:D:\JavaDevelopment\MyJavaClasses.jar -classpath .... ...
When you get a NullPointerException, your code will be called with its printStackTrace() showing who called.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Eclipse Debugging - III JavaForums Java Blogs 0 06-17-2008 12:00 PM
Eclipse Debugging - II JavaForums Java Blogs 0 06-17-2008 12:00 PM
Eclipse Debugging - I JavaForums Java Blogs 0 06-16-2008 11:30 AM
Debugging In NetBeans IDE JavaForums NetBeans 0 07-31-2007 12:13 AM


All times are GMT +3. The time now is 05:59 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org