-4

The following code to try to catch a NullPointerException via logging is not working. Can someone please look at this and guide me.

try {
    if (results1 != 00.00) {
        throw new Exception("Data must be added");
    }
}
catch (Exception e) {  
    log.error("You have an error", e);
}  

The user must enter a value for results1 as it is used for a calculation. Results1 is a double as it is a percentage, I am willing to change it to an int if thats what it takes to make it work. I also want to check through the try and catch technique if a user accidently added a % sign. I want my logger to catche the NumberFormatException for testing purposes.

UPDATE: Ok thanks for the feedback ( I somehow overlooked that). It still doesn't work. I think this might be the problem:

          (results1 != 00.00)

Is this the best way to check if the input is empty for a double. Also How can i check if a string is added?

user207421
  • 305,947
  • 44
  • 307
  • 483
user2241922
  • 11
  • 1
  • 1
  • 5
  • Why are throwing `NullPointerException` for `results1 != 00.00`!This doesn't make sense. – Anirudha Apr 04 '13 at 06:23
  • 1
    You should create a new question for your edited part. Accept one of the answers to the current question. – maba Apr 04 '13 at 06:29
  • 2
    Whoa, the question has been changed considerably, hence the old answers don't make sense any more. – A.H. Apr 04 '13 at 06:46
  • Is nobody going to point out that using exceptions for control flow is a bad idea? Also: if result1 is a double it can never cause a NumberFormatException or a NullPointerException. Please show more of your code and explain what you are trying to do exactly. – Adriaan Koster Apr 04 '13 at 07:52
  • This code cannot possibly throw a `NullPointerException`, so of course it isn't being caught. @AdriaanKoster Exceptions *are* a form of flow control. – user207421 Apr 05 '13 at 00:46
  • @EJP That's true, but they are supposed to be used for _exceptional_ cases that cannot be handled at the level in the callstack where they occur. In this example an exception is used where a regular control mechanism would suffice, which is bad practice. See also this discussion: http://stackoverflow.com/questions/9306913/using-exceptions-for-flow-control – Adriaan Koster Apr 05 '13 at 08:36

5 Answers5

2

NullPointerExeption isn't an instance of NumberFormatException thus it wasnt caught.

You should catch common ancestor of those two, here are the docs: NumberFormatException and NullPointerException. But from my point of view it's better to throw IllegalArgumentException and handle that correctly.

Marcin Pietraszek
  • 3,134
  • 1
  • 19
  • 31
2

NullPointerException is not an instanceof NumberFormatException. You need to catch the correct exception.

Keppil
  • 45,603
  • 8
  • 97
  • 119
0

If you change your code to this it should work:

try {
   if (results1 != 00.00 ) { 
    throw new Exception("Data must be added"); 
   }
} catch (Exception e)   {  
   log.error("You have an error", e);
}  
mjuarez
  • 16,372
  • 11
  • 56
  • 73
0

You have to catch the null pointer exception as another catch block. Null pointer exception is not an instance of Numberformat exception

Vasu
  • 149
  • 7
0

You should catch NullPointerException instead of NumberFormatException.

eatSleepCode
  • 4,427
  • 7
  • 44
  • 93