• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Catching a NullPointerException

 
Robert Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers:
I am working on a class assignment that requires an applet to calculate miles per gallon using data input to a "miles driven" Text Field and a "gallons used" Text Field with the result (or an appropriate error message) displayed in a label. A CALCULATE button triggers the calculation. Additionally a scrolling message must appear in a Canvas object on the window.
The object of this assignment is the use of multithreading and exception handling.
My project is completed and functioning as expected with the exception of the following exception reported on the command line following every program close:
java.lang.NullPointerException
at Marquee.run(App.java:202)
at java.lang.Thread.run(Thread.java:536)
Here is the code for the Marquee class (which is where I think this is comming from:

The other part of the program that deals with exception handling is pretty straight forward and seems to work fine. It checks the validity of the miles and gallons entered before performing the calculation and displays an error message as appropriate.

Can anyone explain the likely source of the NullPointerException being thrown and is it something I should be trying to catch?
TIA
Bob
[ March 19, 2004: Message edited by: Robert Davis ]
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robert,

java.lang.NullPointerException
at Marquee.run(App.java:202)

You have a file called "App.java" and the exception is occurring at its line 202). Just check that line and you'll know where the exception came from. A NullPointerException is usually considered a programming error.
Ed
 
ChandraMouli Vidiyala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert,
Can catch not only compile time exceptions but also runtime exceptions like NullPointerException etc. using the following code.
try {
// Code that can throw compile/runtime exceptions
}catch(Throwable t){
System.out.println("Exception raised is : " + t);
}
Pls. correct, if i am wrong.
-Mouli.
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Eddie said, a NullPointerException is a programming error. Ideally you should track down and fix the bug that caused the exception.
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You should use .equals() to compare strings rather than == like this:

But, this assumes that err and err.getMessage() are not null. It may be an assumption too far. A commonly used trick is to do the comparison the other way round. For example:

In general, testing the contents of the exception's error message should be avoided because if you change the message you will break the code. It would be better to use a subclass of the Exception object and then catch that.

Example of catching it:
 
Robert Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for your thoughtful replies:
You have a file called "App.java" and the exception is occurring at its line 202). Just check that line and you'll know where the exception came from. A NullPointerException is usually considered a programming error.


Eddie and David - I tried to track this line down but I can only count 157 lines of code in the file (I thought blanks and commented out lines didn't count). If I include the commented out lines I still only have 199. Any ideas about line numbers?
As to the exception handling and comparing err.message to a string. The assignment requires the following method (exactly):


Additional exceptions must also be caught (for example a space or alphabetic character entered instead of a number in the miles or gallons Text Field).

My thinking for this try/catch block was that the catch only applies when an exception occurs. In this case the exception could come from one of the parseDouble statements (for data not compatible with a Double) or from the exception thrown by the mpg method. While somewhat simplistic, for the limited purpose of this assignment it seems to work ok.
As I stated before, my program seems to function exactly as expected except when I close it down - meaning all the calculations work with correct data, the expected error messages occur with incorrect data, no exceptions show up on the command line while the program is running. Only when I close it (by clicking the X in the top right hand corner of the window) does the NullPointerException occur.
Is it possibly coming from the interuption of this endless loop?

Thanks again for all you assistance.
Bob
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robert,
The line number in the exception will be the exact line number in your file, including blank and commented out lines. Use a text editor like textpad or dos edit that shows the line you are on. Sometimes, with just in time (jit) compiling the line number will be after the block of the code where the error occurred.
Your sleep exception does not have any code but also should not generate a null pointer exception.
 
Robert Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eddie:
Thank you for the clarification on line numbers.
I have narrowed this NullPointerException:
java.lang.NullPointerException
at Marquee.run(App.java:202)
at java.lang.Thread.run(Thread.java:536)
to the following block of code:

And it disappears when I add this try/catch block (as expected)

Is this a reasonable way to handle this type of error? As I stated earlier, the application works fine except for the NullPointerException experienced on termination.
If anyone can explain the why of this NullPointerException (does the object disappear before the thread ends?) I would be eternally grateful.
TIA
Bob
[ March 21, 2004: Message edited by: Robert Davis ]
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert,

You need to add some lines to the exception handler to see what variable is null, and then go backwards and see where they should have got their values from.
Ed
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the component is not visible at the time the Graphics object is created the Graphics object will be null.
run this, then comment out the
g = getGraphics();
which is after setVisible(true), and uncomment the line before setVisible(true) and run it again.
Don't know if this is your problem, but worth looking into.
 
Robert Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranchers (particularly Eddie and Michael):
Thank you for your valuable insight.
This is what I ended up with (and it avoids the NullPointerException created when the object was being destroyed but still trying to get a graphics reading from it).

Your suggestions were just enough to help me find the right spots to check (with the Sysem.out.println()) in combination with identifying the null object and where to break the loop.
Thanks again.
Bob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic