Solved

null pointer and illegal argument exceptions

Posted on 2014-10-01
7
199 Views
Last Modified: 2014-10-05
Hi,

Effective Java book wrote:
NullPointerException : Parameter value is null where prohibited.
IllegalArgumentException : Non-null parameter value is inappropriate.

I have not understood meaning of above two lines clearly. Can you please explain me with detailed code example. What are the best practices/tips/common pitfalls in exception handling.
How to handle exception in case of below method
backAround("Hello") → "oHelloo"

public String backAround(String str) {
int n=str.length();
if(str.length()>=1)
{
return str.substring(n-1,n)+str+str.substring(n-1,n);
}
  return null;
}

Open in new window


Any links, resources highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
7 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 40355686
You seem to be asking two different questions here. Can you clarify what you need help with?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40357803
I mainly need to know below one

Effective Java book wrote:
NullPointerException : Parameter value is null where prohibited.
IllegalArgumentException : Non-null parameter value is inappropriate.

I have not understood meaning of above two lines clearly. Can you please explain me with detailed code example for NullPointerException  and IllegalArgumentException  
0
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 167 total points
ID: 40357809
There's not much to understand here. If your method requires a non-null parameter, but the calling code passed a null value to it, your method could throw NullPointerException. If your method requires a parameter with a specific value, but the calling code passed a value that doesn't make sense, your method could throw IllegalArgumentException.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 7

Author Comment

by:gudii9
ID: 40358564
how is null different from ""

please advise
0
 
LVL 14

Expert Comment

by:CPColin
ID: 40358725
Please see this comment in one of your other questions. I answered that question there.

Asking the difference between null and "" is like asking the difference between null and 0. One is null and the other is not.
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 166 total points
ID: 40358888
>> how is null different from ""
Consider the variable myObject of type String.
if myObject equals null, then it doesn't contain "anyhting". Or you could say it is not initialized with a value yet.
if myObject equals "", then it contains the empty String "".
0
 
LVL 32

Accepted Solution

by:
awking00 earned 167 total points
ID: 40359458
Declare two variables as
String noString = null;
String emptyString = ""
If you call backAround(noString), you will get a NullPointerException
If backAround were to contain additional code like the following:
if (str.equals("")) {
      IllegalArgumentException iae =
            new IllegalArgumentException("Invalid entry. Empty string is not allowed");
      throw iae;
}
and you call backAround(emptyString), you will get an IllegalArgumentException
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java Timer (static) 9 36
xampp tool 12 25
Way to decrease size of apk file 9 43
Interview question Javascript, database 12 31
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This video teaches viewers about errors in exception handling.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now