Solved

Checking String length

Posted on 2004-04-19
13
613 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
A Short Story about the Best File Recovery Software – Acronis True Image 2017
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now