Nullpointer exception in doPost - java

I have de following Form:
<form action="/AppStore/publish" method="post" accept-charset="ISO-8859-1">
<fieldset>
<legend>Do you have an Account already?</legend>
<input type="radio" name="registred" value="yes"> Yes
<input type="radio" name="registred" value="no"> No
</fieldset>
<fieldset>
<legend>About your App</legend>
<table>
<tr>
<td><label for="AppDesc">Describe it:</label></td>
<td><input type="text" name="AppDesc" /></td>
</tr>
<tr>
<td><label for="AppName">Name:</label></td>
<td><input type="text" name="AppName" /></td>
</tr>
</table>
</fieldset>
<input type="submit" value="Submit" />
</form>
I pass this data to a Java Servlet, but every time I get a Nullpointer Exception at getParameter("AppDesc"), instead getParameter("AppName") works fine, what do I wrong?
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
RequestDispatcher dispetcher = context.getRequestDispatcher("/publishForm.jsp");
List<String> errorMessages = new ArrayList<String>();
//Validating form input...
if(request.getParameter("AppName").toString().isEmpty())
{
errorMessages.add("Please type a valid Name for your App.");
}
if(request.getParameter("AppDesc").toString().isEmpty())
{
errorMessages.add("The Description of your App should contain at least 160 Characters.");
}

You're calling request.getParameter("...").toString().
request.getParameter() already returns a string reference, so you don't actually need to call toString() to get the value as a string, but it will return a null reference if the parameter is missing - in which case calling toString() will throw an exception. You need to check whether the value is null or empty. For example:
String description = request.getParameter("AppDesc");
if (description == null || description.isEmpty())
...
Of course, there are libraries around to check for "null or empty" - for example, in Guava you could use:
if (Strings.isNullOrEmpty(description))

If request.getParameter("AppDesc") is null, then
request.getParameter("AppDesc").toString().isEmpty() will throw a NullPointerException.
Why not change the condition to:
if(request.getParameter("AppDesc") == null ||
request.getParameter("AppDesc").toString().isEmpty()))
{

<td><input type="text" name="AppDescr" /></td>
You've named the actual field AppDescr (notice the trailing "r"), but you're calling getParameter for AppDesc (sans "r").
EDIT: Or not... you edited your post and fixed it. Was this not the problem?

It must be the case that request.getParameter("AppDesc") returns a null value, causing the chained toString() to generate a NullPointerException.
This parameter was never set; the name specified in the html was "AppDesr" (note the trailing 'r').

Your question title says doGet, your code says doPost. The difference between those two might explain your problem. :-)

Related

Is it possible to have an input path for variables in Thymeleaf

I know the title might be misleading but here my question: In Thymeleaf we set request params with the input (in HTML). Is it possible to have an input field that sets the path variable. For example I have an method like this:
#PostMapping("/house/{id}/rent")
public String rentHouse(#RequestParam Date startDate, #PathVariable("id") long id, Model model) {
House h = new House();
h.setId(id);
r.setStartDate(startDate);
Rents rents = rentsService.createNewRent(h, id);
model.addAttribute("rent", rents);
return "House";
}
And in House.html I want something like this:
<form th:action="#{/house/${id}/rent/}" method="post">
<label for="startDate">start Date:</label><br>
<input type="datetime-local" id="startDate" th:name="startDate" placeholder="startDate"><br>
<label for="id">house id:</label><br>
<input type="number" id="id" th:name="id" placeholder="id"><br>
<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
So that when I input something then the result url should be looking like this (I know start Date has false format):
localhost:8080/House/12/rents?startDate=02.21.22
And is it also possible to pass request body in Thymeleaf, I searched for similar questions but they all solved it by manually putting the path variable in the url.
Thanks in advance

Retrieve multiple checkbox values from jsp in the spring controller class

In my table, I've a row with checkbox which tells the controller whether that particular row of the table has to be included or no. The check box does not have relation with other rows. I tried adding it in the following way :
<form:form id="fee" method="post" modelAttribute="clientForm" commandName = "clientForm"
action="<%= request.getContextPath().toString()%>/addFee.do">
<TABLE>
<tr>
<c:forEach var="type" items="${clientInfo}" varStatus="status">
<td><form:checkbox class="editable${ifeeCount}" path="includeFeeValue" value="false"/> </td>
<td>feeType<c:out value = "${status.index}"/></td>
<td>Source Fee<c:out value = "${status.index}"/></td>
<td><form:input class="editable${ifeeCount}" disabled="true" path="overriddenFee" /></td>
<td><form:errors path="overriddenFee" cssClass="error" /></td>
</c:forEach>
</tr>
</TABLE>
And in my form, I've a list private ArrayList<String> includeFeeValue;
And i'm trying to retrieve this in the spring controller class as follows :
#RequestMapping(value="/addFee.do",method = RequestMethod.POST)
protected #ResponseBody ModelAndView selectValues(#ModelAttribute("clientForm") PaswFeeMaintenanceForm MyMaintForm ) throws Exception {
for(int i=0;i<MyMaintForm.getIncludeFeeValue().size();i++){
System.out.println("Checkbox : "+MyMaintForm.getIncludeFeeValue().get(i)+ " of "+i);
}
}
Once I submit my form, it throws null pointer exception in here : MyMaintForm.getIncludeFeeValue().size() .
Could you tell me what's missing here?
Remove disabled='true' and it will work. I faced the same problem with my textfield with property disabled as true.
And also use private String[] includeFeeValue instead of List.
As your checkbox is disabled, ans disabled elements values never flow to controller or Servlet. I advise you to remove a disabled attribute. I hope so it will work.

Unable to catching multiple table rows with JSTL forEach in Spring MVC

The goal: adding multiple table rows dynamically based on user inputs and catch the data through controller.
What I have so far(all simplified):
POJO:
public class Item(){
String price;
String weight;
getters and setters...
}
public class ItemForm(){
List<Item> items;
getter and setter...
}
JSP:
<form:form action="/create" method="POST" modelAttribute="itemForm">
<table>
<tr>
<td><input type='text' name='price'/></td>
<td><input type='text' name='weight'/></td>
</tr>
</table>
<c:forEach items="${itemForm.items}" var="item" varStatus="status">
<tr>
<td align="center">${status.count}</td>
<td><input name="items[${status.index}].price" value="${item.price}" /></td>
<td><input name="items[${status.index}].weight" value="${item.weight}" /></td>
</tr>
</c:forEach>
</form:form>
Controller:
private List<Item> items = new ArrayList<>();
#RequestMapping(value = "/create", method = RequestMethod.POST)
public String saveMultipleRows(#ModelAttribute("itemForm") ItemForm itemForm) {
items = itemForm.getItems();
if(items != null && items.size() > 0){
System.out.println("The list is not null!");
}
System.out.println("didn't get into the if statement");
return null;
}
I skipped the Javascript on adding table rows, if you think that have anything to do with this question, I will update my post and put the Javascript code.
The idea is to create a ItemForm class that contains a list of Item object, and in the JSP using JSTL c:foreach to save all the data from users to the list. And in my controller, if the list is not empty, I simply want to print out a message so that I know the list is not empty. But now if I run the program, it prints out "didn't get into the if statement".
So the problem I am currently having is the list is empty, that means I am not able to save the user input data to the list. Can anyone help me and let me know where I did wrong?
Below is the corrected code
since you have item in scope you can directly access the props of Item,You dont need to explicitly declare the index
<input name="${item.price}" value="${item.price}" />
<input name ="${item.weight}" value="${item.weight}" />

How to: Struts 2 validating dynamic forms array

I read through this
Struts2 Validation for an array
and it makes sense, but wished person (Quaternion) would explain how to
"rewrite the above to specifically name fields (with indexes) in which case you can use , and you'll use the addFieldError method. For details on these tags see http://struts.apache.org/2.3.1.2/docs/tag-reference.html"
This is what I have:
<s:form action="saveOrUpdateAction" method="get">
<s:token/>
<table>
<tr>
<td> Fund </td>
<td> Award Code </td>
</tr>
<s:iterator value="gfeListWithEmptyCode">
<tr>
<td> <s:property value="sfafund "/> </td>
<td> <s:property value="awardcode"/>
<input type="text" name="codeArray">
</td>
</tr>
</s:iterator>
<s:token />
<s:submit key="Submit2"/>
</table>
</s:form>
Part of my Action:
public void validate()
{
if (fund == null || fund.trim().length() != 5 )
{
System.out.println("testing+++++++++++++++++++1");
addFieldError("fund","Fund requires 5 characters.");
}
if (code == null || code.trim().length() != 3 )
{
System.out.println("testing+++++++++++++++++++2");
addFieldError("code","Fund requires 3 characters.");
}
if (gfeListWithEmptyCode !=null)
{
int index = 0;
for (GiftFundEntity giftFundEntity : gfeListWithEmptyCode)
{
if ( codeArray[index]!=null && codeArray[index].length() < 3 )
{
System.out.println("testing+++++++++++++++++++3");
// technically, this is not possible to do because it requires codeArray[index] and not a string.
addFieldError("codeArray","Code requires 3 characters.");
index++;
}
}
}
try
{
this.execute();
} catch (Exception e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
During validation, the red error message does not show up on the jsp page for obvious reasons because codeArray isn't listed by the indexes. How do I get this to work? * Please note* the array is dynamic.
I looked through the struts documentation and searched through stackoverflow, but I don't see how it can be done.
Thanks for your time.
The answer is Struts 2 treats the codeArray variable inside the Action class as an Array: String [] codeArray; This is not documented.

Working with <div> tag in JSP for making the contents hidden or visible

I have written a javascript function.
function wellChecked(state) {
if (state)
{
wellDropDown.style.visibility = 'visible';
}
else
{
wellDropDown.style.visibility = 'hidden';
}
}
I have a checkbox after the Well Modification <td> as given below,
<tr>
<td>On Call</td>
<td><html:checkbox property="onCall"/></td>
<td>Well Modification</td>
<td><input type="checkbox" onclick="wellChecked(this.checked)" /></td>
</tr>
When that checkbox is clicked I want the drop down list given under the div id=wellDropDown to be displayed. Defaultly, if the check box is not clicked, the drop down should not be displayed.
<tr>
<td>Active</td>
<td><html:checkbox property="active"/></td>
<div id="wellDropDown" style="visibility:hidden;">
<td>
<html:select property="wellFormatId">
<html:option value="">(Select)</html:option>
<bean:define id="wellFormatColl" name="wellFormats" scope="request"/>
<logic:iterate id="wellFormats" name="wellFormatColl" indexId="index" type="com.astrazeneca.compis.data.WellFormatVO">
<% Long wellId = wellFormats.getWellFormatId();%>
<% String wellIdNo = wellId.toString(); %>
<html:option value="<%=wellIdNo%>">
<bean:write name="wellFormats" property="wellFormatName"/>
</html:option>
</logic:iterate>
</html:select>
</td>
</div>
</tr>
When I tried executing this code, I could see the drop down list getting displayed irrespective of the checkbox checked or not.
Where I have went wrong in this scenario? Please give ur suggestions or ways to implement my requirement.
Your HTML is invalid. You may not have a div enclose a td like this. Either make the td itself visible or invisible, or put the div inside the td, instead of putting it around the td.
Also, unless wellDropDown is a global JS variable, the code should be
document.getElementById("wellDropDown").style.visibility = 'visible';
with jquery you could do this :
<tr>
<td>On Call</td>
<td><html:checkbox property="onCall"/></td>
<td>Well Modification</td>
<td><input type="checkbox" id="myCheckBox" /></td>
</tr>
...
<script>
$('#myDropDown').click(
function () {
$("#wellDropDown").toggle();
});
);
</script>

Categories

Resources