0

I'm pretty new to Java so bear with me. Basically I have a class named "returnPages" that returns the parent page that you are currently on. The problem is when you're at the highest level or the root it throws an error, logically because the root doesn't have a parent it will throw a null pointer exception. How would I prevent an error being thrown when your at the root? I thought the below code would work but I'm simply getting a null pointer exception were I start my conditional. Note: I'm trying to run this in the JSP, modifying the returnPages class will result in multiple conflicts across the site. Also the returnPages class takes two arguments the first is parent page followed by the current page your on.

Page rootPage = properties.get("rootPage", "");
if(returnPages.getPath(rootPage, currentPage) != null){
  List<Page> trail = returnPages.getPath(rootPage, currentPage);
} else {
//show something here
}

Any help is greatly appreciated!

RNJ
  • 15,272
  • 18
  • 86
  • 131
Delmon Young
  • 2,013
  • 5
  • 39
  • 51
  • You can check to see if the value is null and you can also catch a NullPointerException and handle it that way. Sorry there's not really enough information for me to give you a better answer. By the way classes don't return a value, the getPath() method of the returnPages class returns a value. – Kevin Bigler Apr 14 '13 at 19:36
  • An existing issue which was solved in an earlier question. See my answer below. If real-time help is needed I am available for chat. – Cebence Apr 14 '13 at 20:42

3 Answers3

4

You can use

return Collections.emptyList<Page>();

Or simply

return new LinkedList<Page>();

The first option returns an immutable list, so attempting to add anything to that list will fail with an exception. You use less memory though and ensure the list is not modified, which is sometimes a good thing.

Edit: why are you doing the lookup twice?

List<Page> trail = returnPages.getPath(rootPage, currentPage);
if (trail == null) {
  trail = Collections.emptyList<Page>();
}
Dariusz
  • 21,561
  • 9
  • 74
  • 114
  • This solution is elegant but the issue is that `getPath()` is throwing an NPE, not returning `null`. – Cebence Apr 14 '13 at 20:36
1

If returnPages.getPath(rootPage, currentPage) is throwing an NPE yo uhave to deal with it. you cannot check if it returns null or not

try {
   List<Page> trail = returnPages.getPath(rootPage, currentPage);
} catch (NullPointerException e){
   //show something here
}

There are great debates about unchecked and checked exceptions in java. NullPointer is an unchecked exception so usually means programmer error. Some people say you should not catch them. What you could do instead of returning null is return an empty list

Edit after comment:

A null pointer is thrown when you try and access something on a null object. (It's really a NullReferenceException but again there are many debates on that)

What you are doing is checking if the returned object from the method is null. Inside the returnPages.getPath(rootPage, currentPage) it is throwing a NullPointerException - not returning it. (in effect returning nothing because of this error condition and throwing the exception). When to throw an exception? gives details on this. In older languages error codes are returned so you could do a check like you are. Java has an exception handling framework which is why the author of getPath has decided to throw an exception rather than return null

Community
  • 1
  • 1
RNJ
  • 15,272
  • 18
  • 86
  • 131
  • Thanks @RNJ this is really helpful worked perfect! I appreciate the code snippet. Bear in mind I'm new but why couldn't you check if it was null I'm kind of confused on what you mean you have to deal with it. – Delmon Young Apr 14 '13 at 19:42
  • @DelmonYoung updated answer. let me know if it is not clear or if you require any other info – RNJ Apr 14 '13 at 19:59
  • This **IS** an [old issue](http://stackoverflow.com/questions/15981798/java-getparent-returning-null/15982382#15982382), and "modifying the returnPages class will result in multiple conflicts across the site." doesn't make sense at all since neither the method interface nor its functionality has changed. – Cebence Apr 14 '13 at 20:34
0

You should paste the stacktrace of that NullPointerException because it is not the same thing if that exception comes from getPath() or if it comes from the condition if (returnPages.getPath(....

This might be the same issue as in your earlier question.

UPDATE: (Moving my comment to this answer.) This is an old issue (see the question). The sentence "modifying the returnPages class will result in multiple conflicts across the site" doesn't make sense at all since neither the method interface nor its functionality has changed.

Community
  • 1
  • 1
Cebence
  • 2,406
  • 2
  • 19
  • 20