• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dead code in Java -- dealing with NullPointerException  RSS feed

 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have an issue with the following code (it does almost nothing here, just converting String into String[]), which compiles fine but IDE Eclipse says that code between lines 20 and 22 is dead code. Why ? Why shouldn't be this code never reached ? Thanks for advice

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with simply throwing a null pointer exception? Do you really need to subclass it? Any class extending an Exception should be called XYZException.

It is possible to pass null to that method, in which case line 21 will be executed. But not in your class. Do you need that method called from other classes? Does it need public access, or can you give it a restrictive access? The only place you are calling that method in your class is from the main method, where you are assigning the String from a Scanner. Have you ever tried to get null from a Scanner? I have and I don't think ti can be done. It will either return a result, even if it is the empty String, or it will throw an exception as described here. Even if you turn System.in off, you never get null.

And you have fallen for the trap which Eclipse sets for the unwary, telling you that the Scanner pointing to System.in is a resource leak if you don't close it. I shall leave you to find out what happens if you don't resist that temptation. Mwaahaahaahaahaahaahaahaa!
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 18 you use source so it can't be null when you get to line 20; you would have already thrown a NullPointerException. Just moving the if block above line 18 will do the trick.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:In line 18 you use source so it can't be null when you get to line 20; you would have already thrown a NullPointerException. Just moving the if block above line 18 will do the trick.

Yes, indeed ! Thanks.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you need that method called from other classes? Does it need public access, or can you give it a restrictive access? The only place you are calling that method in your class is from the main method, where you are assigning the String from a Scanner.

I did it just to test a piece of code but indeed it's a nonsense as the one cannot leave the scanner empty.

Campbell Ritchie wrote:
And you have fallen for the trap which Eclipse sets for the unwary, telling you that the Scanner pointing to System.in is a resource leak if you don't close it. I shall leave you to find out what happens if you don't resist that temptation. Mwaahaahaahaahaahaahaahaa!

No idea for the moment
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! I missed that bit.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Damn! I missed that bit.

By the way, what did you mean by "It is possible to pass null to that method" ?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The current code cannot pass null but you have a public method. Somebody could writesomewhere else and it will explode in a different sense.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote:. . .
No idea for the moment
Simply do something with System.in after the close() call and you will find out
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!