Java expected null, throws exception -
i've been trying find solution problem 2 hours, haven't found useful.
i have method gets issuedata mantisapi use later in code. catches exceptions thrown:
try {      issuedata issue = mantisapi.getissue(issueid, user);      task = preparemantistask(issue);     } catch (exception e) {      system.out.println(e.getmessage());     }     return task;   the problem when issuedata expect null because might empty field. when returns, exception caught in try/catch block. tried ignore using 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 looks stupid when have 20 or more records check. there other way ignore nulls? thank time.
i'm sorry i'm @ home now, , cannot copy code. preparemantistask looks like:
mantistask mantistask; mantistask.setid = issue.getid();   so example. if mantistask.setduedata = issue.getduedata(); null because not issues have parameter. when debugger point, returns to
} catch (exception e) {          system.out.println(e.getmessage());  }    and left preparemantistask task null.
this 2 pieces of code different parts of program, wanted show how works.
why not make null check instead of waiting exception? perhaps this:
public string getprojectnotnull(mantistask mantistask) {     if (mantistask == null){         return null;     }      string project = "-";      // check whatever may 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 edit, rule still maintains though. if not issues have parameter, must check before, never exceptions because null.
assuming null pointer exception due issue.getduedata(), likewise do:
    if (issue.getdata() != null) {         mantistask.setduedata = issue.getduedata()     }   so you'll never null pointer exception in first case.
in short, there no way ignore nulls, must check each 1 of them before using them (and never relying on exceptions check nulls).
Comments
Post a Comment