Solved

null pointer and illegal argument exceptions

Posted on 2014-10-01
7
193 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 31

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 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

20 Experts available now in Live!

Get 1:1 Help Now