Connecting Tech Pros Worldwide Help | Site Map

null pointer exception - mixing java and javascript

Newbie
 
Join Date: Sep 2007
Posts: 12
#1: Nov 27 '07
Hello,

Firstly, apologies if this should be in the javascript forum - I wasnt sure which was most appropriate to post this question in...

I am not sure if my problem is caused by the way I am mixing java and javascript in my jsp and if it can be fixed.

If currentObservation.getComment() (java) is not null then all is well. But if it is null, instead of just setting document.commentForm.commentBox.value to a blank string I get a java.lang.NullPointerException.

Am I testing for null incorrectly, or is it impossible to mix java and javascript quite like this?!

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript">
  2. var commentValue = "";
  3. <%if(currentObservation.getComment()!=null){%>
  4.        document.commentForm.commentBox.value="<%=currentObservation.getComment()%>";
  5. <%}else{%>
  6.         document.commentForm.commentBox.value = "";
  7. <%}%>
  8. document.commentForm.commentBox.value = "";
  9. document.commentForm.remLen.value = (1000 - commentValue.length);
  10.                                     //-->
  11.                                 </script>
any help gratefully received
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Nov 27 '07

re: null pointer exception - mixing java and javascript


Quote:

Originally Posted by basm101

Hello,

Firstly, apologies if this should be in the javascript forum - I wasnt sure which was most appropriate to post this question in...

I am not sure if my problem is caused by the way I am mixing java and javascript in my jsp and if it can be fixed.

If currentObservation.getComment() (java) is not null then all is well. But if it is null, instead of just setting document.commentForm.commentBox.value to a blank string I get a java.lang.NullPointerException.

Am I testing for null incorrectly, or is it impossible to mix java and javascript quite like this?!

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript">
  2. var commentValue = "";
  3. <%if(currentObservation.getComment()!=null){%>
  4.        document.commentForm.commentBox.value="<%=currentObservation.getComment()%>";
  5. <%}else{%>
  6.         document.commentForm.commentBox.value = "";
  7. <%}%>
  8. document.commentForm.commentBox.value = "";
  9. document.commentForm.remLen.value = (1000 - commentValue.length);
  10.                                     //-->
  11.                                 </script>
any help gratefully received

I guess your 'currentObservation' itself is null. The rest of your code looks fine
as far as I can tell.

kind regards,

Jos
Reply