-1

I've been trying to find a good solution to this problem for 2 hours, but I haven't found anything useful.

I have method that gets IssueData from mantisApi for use later in the code. It also catches any exceptions thrown:

try {
     IssueData issue = mantisApi.getIssue(issueId, user);
     task = prepareMantisTask(issue);
    } catch (Exception e) {
     System.out.println(e.getMessage());
    }
    return task;

The problem is when I get the IssueData I expect null because it might be an empty field. However when it returns, an exception is caught in this try/catch block. I tried to ignore using the following code:

public String getProjectNotNull(MantisTask mantisTask) {
    if (mantisTask == null){
        return null;
    }
    try{
    String project = mantisApiCache.getProject(mantisTask.getProject()).getName();
        return project;
    }
    catch(NullPointerException npe){
    log.info("TaskService.getProjectNotNull() throws controled null");
    return "-";
}

But it looks stupid when I have 20 or more records to check. Is there any other way to ignore nulls? Thank you for your time.

I'm sorry I'm at home now, and i cannot copy code. prepareMantisTask looks like:

MantisTask mantisTask;
mantistask.setId = issue.getId();

so example. if i do mantistask.setDueData = issue.getDueData(); it is null because not all issues have this parameter. So when the debugger get to this point, it returns to

} catch (Exception e) {
         System.out.println(e.getMessage()); 
} 

and left prepareMantisTask with task null.

This two pieces of code are from different parts of my program, I just wanted to show how it works.

kirsty
  • 267
  • 1
  • 2
  • 14
qusqui21
  • 170
  • 1
  • 12
  • 1
    It may sound mean, but you should try to write in a clearer English, it's hard to understand what you are asking, and therefore hard to help you :/ – Diego Martinoia Jan 07 '16 at 15:02
  • 3
    Specifically catching NPE is a code smell. Try to work on your design to either ensure nulls never enter, or if they do, you handle them in a much more graceful way. – cobaltduck Jan 07 '16 at 15:04
  • I'm not sure how the second block of code relates to the first. Can you post the contents (or at least, the key parts) of `prepareMantisTask`? I suspect that the problem is in there. Your `NullPointerException` should tell you which line the issue is on, can you edit your question to put a comment or something similar against the line mentioned in the stack trace? – DaveyDaveDave Jan 07 '16 at 15:10
  • I don't see how the first and second code are related. Otherwise I have to agree with @DiegoMartinoia. – lschuetze Jan 07 '16 at 15:10

1 Answers1

1

Why not just make a null check instead of waiting for an exception? Perhaps something like this:

public String getProjectNotNull(MantisTask mantisTask) {
    if (mantisTask == null){
        return null;
    }

    String project = "-";

    // check whatever may be null here
    if (mantisTask.getProject() != null
        && mantisApiCache.getProject(mantisTask.getProject()) != null)  {

        project = mantisApiCache.getProject(mantisTask.getProject()).getName();

    } else {
        log.info("TaskService.getProjectNotNull() throws controled null")
    }

    return project;
}

EDIT

In response to your edit, the rule still maintains though. If not all issues have that parameter, you must check it before, so that you never get exceptions because something is null.

Assuming the null pointer exception is due to issue.getDueData(), you likewise do:

    if (issue.getData() != null) {
        mantistask.setDueData = issue.getDueData()
    }

so that you'll never get a null pointer exception in the first case.

In short, there is no way to ignore nulls, you must check each one of them before using them (and never relying on exceptions to check for nulls).

lv.
  • 446
  • 6
  • 18
  • 3
    I would strongly encourage the use Optional class (either from Guava, either from Java 8) as a best practice in order not to deal with null ! – Louis F. Jan 07 '16 at 15:12
  • But i have to catch exception on first part of code couse it api class, so i have to catch it in prepare MantisTask. – qusqui21 Jan 07 '16 at 17:03
  • I solve it by creating class Wrapper and i convert everythink to String, so when i get null i return empty string. But, thank you for your solution. – qusqui21 Jan 11 '16 at 18:13