Solved

Checking String length

Posted on 2004-04-19
13
615 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can't access image with tomcat/html 6 97
eclipse luna javeEE perspecive missing 5 110
countXY challenge 28 153
login jsp example 24 50
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
Adding Computers to AD groups through an SCCM Task Sequence
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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