Solved

null pointer and illegal argument exceptions

Posted on 2014-10-01
7
207 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 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