Solved

Checking String length

Posted on 2004-04-19
13
618 Views
Last Modified: 2010-04-01
Hi,

I just basically want to see if my varibles have a value or not, So I use .Length.  I'm just not sure if this is the best way to do this, as I keep getting a null pointer error.

<%
String varManifest = (String)request.getParameter("Rec_Manifest");
%>
<tr><td>Manifest&nbsp;&nbsp;</td>  <td><%if (varManifest.length() >0 ){ %><input type='checkbox' checked DISABLED><%} else {%><input type='checkbox' unchecked DISABLED><%}%></TD> <td nowrap><%=varManifest%></td></tr>

All I want to show is a ticked checkbox if there is a value else  show it unticked.
0
Comment
Question by:Pigdogmonster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
13 Comments
 
LVL 7

Expert Comment

by:searlas
ID: 10858362
If you check for null and set to the empty string you'll avoid the NullPointerException's.
Are you using Struts at all?  If so, you may want to look at the logic:present/notPresent and logic:empty/notEmpty tags.
<%
String varManifest = (String)request.getParameter("Rec_Manifest");
if (varManifest == null) {
  varManifest = "";
}
%>
<tr>
  <td>Manifest&nbsp;&nbsp;</td>
  <td><input type='checkbox' <% if (varManifest.length() > 0) %>checked<% } %> DISABLED></td>
  <td nowrap><%=varManifest%></td>
</tr>
0
 
LVL 6

Accepted Solution

by:
jarasa earned 20 total points
ID: 10860497
Searlas what is this for?? <% } %>

Anyway could probably be easier, at least shorter this:

<tr>
  <td>Manifest&nbsp;&nbsp;</td>
  <td><input type='checkbox' <% (String)request.getParameter("Rec_Manifest")==null?%><%:((String)request.getParameter("Rec_Manifest")).length>0?%>checked<%:%> DISABLED></td>
  <td nowrap><%=(String)request.getParameter("Rec_Manifest")%></td>
</tr>

Javier
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10860753
:c)

Thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:searlas
ID: 10860903
Dagnamnit, missed out on 20 points for missing a curly:
<tr>
  <td>Manifest&nbsp;&nbsp;</td>
  <td><input type='checkbox' <% if (varManifest.length() > 0) { %>checked<% } %> DISABLED></td>
  <td nowrap><%=varManifest%></td>
</tr>
0
 
LVL 7

Expert Comment

by:searlas
ID: 10860964
Hey jarasa,

Doesn't your answer behave differently to pigdogmonster's original code (and I'm not talking about not throwing a NPE)

i.e. if the parameter is missing, the checkbox is not disabled.  But in the original question the checkbox is always disabled.

Ignoring the syntax errors, must say I've never seen tertiary operators split up and used like that in a JSP.  That's one sick trick... (IMHO, of course :-)




0
 
LVL 6

Expert Comment

by:jarasa
ID: 10860967
To tell you the truth searlas, I thought pigdog was goin to give you the points.

I shouldn't opened my mouth.

Sorry, your answer was fine.

Javier


0
 
LVL 6

Expert Comment

by:jarasa
ID: 10861061
I haven't tryed but I think that the DISABLED will get printed always

is like doing this in just Java Code

StringBuffer Print = "";
Print.append("<tr><td>Manifest&nbsp;&nbsp;</td><td><input type='checkbox'");
(String)request.getParameter("Rec_Manifest")==null?Print.append(""):((String)request.getParameter("Rec_Manifest")).length>0?Print.append("checked"):Print.append(" DISABLED></td><td nowrap>);
Print.append((String)request.getParameter("Rec_Manifest")+"</td></tr>");

out.println(Print.toString());

Isn't it??

Javier
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10861096
Sorry I ment this:

StringBuffer Print = "";
Print.append("<tr><td>Manifest&nbsp;&nbsp;</td><td><input type='checkbox'");
(String)request.getParameter("Rec_Manifest")==null?Print.append(""):((String)request.getParameter("Rec_Manifest")).length>0?Print.append("checked"):Print.append("");
Print.append(" DISABLED></td><td nowrap>");
Print.append((String)request.getParameter("Rec_Manifest")+"</td></tr>");

out.println(Print.toString());

Javier
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10861127
>Ignoring the syntax errors, must say I've never seen tertiary operators split up and used like that in a JSP.  That's one sick > trick... (IMHO, of course :-)

To tell you the truth me either, but look wild isn't it??

I'm a one line developer :c)) That's make the hackers think twice to ge my code ;cP

Javier
0
 

Author Comment

by:Pigdogmonster
ID: 10866615
Thanks for all the help Guys, I really appreciate it! :-)

I did actually mean to give the point to Searls as that is the code I've used!,  :-/
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10866630
Well then I should refound it to him, if he wants of course. Do you Searlas?
Javier
0
 
LVL 7

Expert Comment

by:searlas
ID: 10866829
Thanks for the offer 'n' honesty.  I'll live without them...

Cheers
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10866957
:c)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xjc and jaxb 2 99
grep code 4 217
print map entry 34 79
spring maven example issues 3 97
This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
Check out this step-by-step guide for asking an anonymous question on Experts Exchange.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question