null pointer exception - mixing java and javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • basm101
    New Member
    • Sep 2007
    • 12

    #1

    null pointer exception - mixing java and javascript

    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 currentObservat ion.getComment( ) (java) is not null then all is well. But if it is null, instead of just setting document.commen tForm.commentBo x.value to a blank string I get a java.lang.NullP ointerException .

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

    Code:
    <script language="JavaScript" type="text/javascript">
    var commentValue = "";
    <%if(currentObservation.getComment()!=null){%>
           document.commentForm.commentBox.value="<%=currentObservation.getComment()%>";
    <%}else{%>
            document.commentForm.commentBox.value = "";
    <%}%>
    document.commentForm.commentBox.value = "";
    document.commentForm.remLen.value = (1000 - commentValue.length);
                                        //-->
                                    </script>
    any help gratefully received
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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 currentObservat ion.getComment( ) (java) is not null then all is well. But if it is null, instead of just setting document.commen tForm.commentBo x.value to a blank string I get a java.lang.NullP ointerException .

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

    Code:
    <script language="JavaScript" type="text/javascript">
    var commentValue = "";
    <%if(currentObservation.getComment()!=null){%>
           document.commentForm.commentBox.value="<%=currentObservation.getComment()%>";
    <%}else{%>
            document.commentForm.commentBox.value = "";
    <%}%>
    document.commentForm.commentBox.value = "";
    document.commentForm.remLen.value = (1000 - commentValue.length);
                                        //-->
                                    </script>
    any help gratefully received
    I guess your 'currentObserva tion' itself is null. The rest of your code looks fine
    as far as I can tell.

    kind regards,

    Jos

    Comment

    Working...