Solved

null pointer and illegal argument exceptions

Posted on 2014-10-01
7
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

740 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