?
Solved

null pointer and illegal argument exceptions

Posted on 2014-10-01
7
Medium Priority
?
227 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 668 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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 664 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 668 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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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