Returning a method

I have a method that returns true or false

String projDest = null;

public boolean projDestCheck() {
      projDest = EdiSystemData.getDestinationPrefix(currentProject.getDestinationPrefixId()).getName();
     
      if (projDest.equals("MATRIX_ONE")){
          return false;
      }
      else {
         return true;
      }
  }

Open in new window


The problem I am having is the method can run before there is a value for
 projDest = EdiSystemData.getDestinationPrefix(currentProject.getDestinationPrefixId()).getName();

So I am getting null pointer error.  How do I fix that method so that if there is not a value for projDest, it does not throw a null pointer and sets the method to true
jkteaterAsked:
Who is Participating?
 
for_yanCommented:
Then before this call put such line:

If(currentProject == null) return true;
0
 
for_yanCommented:
Check if projDest == null befoire you do anuy orher checks and return nill befiore yoiu go to anyhtinhg ese
0
 
jkteaterAuthor Commented:
Like this?

If (projDest == null) {
     return true;
}
else {
      
projDest = EdiSystemData.getDestinationPrefix(currentProject.getDestinationPrefixId()).getName();
     
      if (projDest.equals("MATRIX_ONE")){
          return false;
      }
      else {
         return true;
      }
}

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
for_yanCommented:
No.
Like this :

public boolean projDestCheck() {
      projDest = EdiSystemData.getDestinationPrefix(currentProject.getDestinationPrefixId()).getName();
     if(projDest == null) return true;
      if (projDest.equals("MATRIX_ONE")){
          return false;
      }
      else {
         return true;
      }
  }
0
 
jkteaterAuthor Commented:
The problem is that the assignment

 projDest = EdiSystemData.getDestinationPrefix(currentProject.getDestinationPrefixId()).getName();

will cause a null pointer, because until the user selects a project this is not a valid call

 EdiSystemData.getDestinationPrefix(currentProject.getDestinationPrefixId()).getName();

but once the user selects a project it is fine.  

I am calling that method before the user can assign a project.  If you see the other questions - it explains why
0
 
jkteaterAuthor Commented:
nice catch
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.