By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,213 Members | 2,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,213 IT Pros & Developers. It's quick & easy.

null pointer exception - mixing java and javascript

P: 12
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
Nov 27 '07 #1
Share this Question
Share on Google+
1 Reply


Expert 10K+
P: 11,448
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
Nov 27 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.