Solved

Checking String length

Posted on 2004-04-19
13
614 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Often, people trade privacy and security for convenience. However in today's concrete jungle, this is an extremely foolish decision considering the vast amount of technologies being used against consumer interest. First off, I won't waste any time e…
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
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…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

8 Experts available now in Live!

Get 1:1 Help Now