Solved

Checking String length

Posted on 2004-04-19
13
617 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
  • 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
Independent Software Vendors: 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven project in eclipse 11 60
struts exact match support 3 59
Market Place not working in Eclipse Luna 2 129
collection output issue 9 58
Direct mail marketing is the act of mailing materials straight to prospective customers. This wide form of marketing is one of the oldest methods of communicating with a geographic based demographic. So is it useful in 2017 and beyond?
Adults who share images on social media aren’t the only ones who need to worry about their privacy. Our culture’s tendency to share every move and celebration affects the privacy of our children, too.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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