INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

The map in my form is not being passed to the controller

The map in my form is not being passed to the controller

(OP)
Hi, I'm a newbie in JAVAEE. The data inputted in the map(from the form toevoegen.jsp) is lost(not passed) to the controller.
Please help. Thanks.

I am getting this error:
aug 22, 2014 1:37:57 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [springDispatcher] in context with path [/CocktailBarApp] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at be.intecbrussel.web.CocktailsController.cocktailToegevoegd(CocktailsController.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)




CODE --> Cocktail.java

package be.intecbrussel.entities;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;


@Entity
@Table(name = "cocktails")
@NamedQueries({
		@NamedQuery(name = "Cocktails.findAll", query = "SELECT c FROM Cocktail c"),
		@NamedQuery(name = "Cocktails.findByNaam", query = "SELECT c FROM Cocktail c WHERE c.naam = :naam") })
public class Cocktail implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@Column(name = "id")
	@GeneratedValue
	private long id;

	@Column(name = "naam")
	private String naam;

	@Column(name = "prijs")
	private BigDecimal prijs;
	
	@OneToMany(mappedBy="cocktail", cascade=CascadeType.ALL)
	private List<Samenstelling> samenstellingList;
	
	public List<Samenstelling> getSamenstellingList() {
		return samenstellingList;
	}

	public void setSamenstellingList(List<Samenstelling> samenstellingList) {
		this.samenstellingList = samenstellingList;
	}

	public Cocktail() {
	}

	public Cocktail(long id) {
		this.id = id;
	}

	public Cocktail(long id, String naam, BigDecimal prijs){ 
		this(naam, prijs);
		this.id = id;
	}

	public Cocktail(String naam, BigDecimal prijs) {
		this.naam = naam;
		this.prijs = prijs;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getNaam() {
		return naam;
	}

	public void setNaam(String naam) {
		this.naam = naam;
	}

	public BigDecimal getPrijs() {
		return prijs;
	}

	public void setPrijs(BigDecimal prijs) {
		this.prijs = prijs;
	}

	@Override
	public String toString() {
		return "be.intecbrussel.entities.Cocktail[ id=" + id + " ]";
	}

	@Override
	public int hashCode() {
		int hash = 3;
		hash = 37 * hash + (int) (this.id ^ (this.id >>> 32));
		return hash;
	}

	@Override
	public boolean equals(Object object) {
		if (!(object instanceof Cocktail)) {
			return false;
		}
		Cocktail other = (Cocktail) object;
		if (this.id == (other.id)) {
			return false;
		}
		return true;
	}

} 

CODE --> Samenstelling.java

package be.intecbrussel.entities;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "samenstellingen")
@NamedQueries({
		@NamedQuery(name = "Samenstellingen.findAll", query = "SELECT s FROM Samenstelling s")})
public class Samenstelling implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
//	@Column(name = "cocktailid")
	@ManyToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="cocktailid")
	private Cocktail cocktail;

	@Id
	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="ingredientid")
	private Ingredient ingredient;
	
	@Column(name = "hoeveelheid")
	private long hoeveelheid;

	public Samenstelling() {
	}

	public Samenstelling(Cocktail cocktail, Ingredient ingredient, long hoveelheid) {
		this.cocktail = cocktail;
		this.ingredient = ingredient;
		this.hoeveelheid = hoeveelheid;
	}

	public Cocktail getCocktail() {
		return cocktail;
	}

	public void setCocktail(Cocktail cocktail) {
		this.cocktail = cocktail;
	}

	public Ingredient getIngredient() {
		return ingredient;
	}

	public void setIngredient(Ingredient ingredient) {
		this.ingredient = ingredient;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public long getHoeveelheid() {
		return hoeveelheid;
	}

	public void setHoeveelheid(long hoeveelheid) {
		this.hoeveelheid = hoeveelheid;
	}

} 

CODE --> Wrapper.java

package be.intecbrussel.entities;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class Wrapper implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private Cocktail cocktail;
	private Map<Ingredient, BigDecimal> ingredientMap = new HashMap<Ingredient, BigDecimal>();
	
	public Wrapper()
	{
	}

	public Wrapper(Cocktail cocktail, Map<Ingredient, BigDecimal> ingredientMap)
	{
		this.cocktail = cocktail;
		this.ingredientMap = ingredientMap;
	}

	public Cocktail getCocktail() {
		return cocktail;
	}

	public void setCocktail(Cocktail cocktail) {
		this.cocktail = cocktail;
	}

	
	public Map<Ingredient, BigDecimal> getIngredientMap() {
		return ingredientMap;
	}

	public void setIngredientMap(Map<Ingredient, BigDecimal> ingredientMap) {
		this.ingredientMap = ingredientMap;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
} 

CODE --> toevoegen.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 

<c:set var="contextPath"
	value="${pageContext.servletContext.contextPath}" />
<c:set var="pagina" value="Cocktails" />
<!DOCTYPE html>
<html>
<head>
<title>Cocktail Bar - ${pagina}</title>
<link rel="stylesheet" href="${contextPath}/styles/default.css" />
</head>
<body>
	<div class="header">Cocktail Bar</div>
	<c:import url="../navigatie.jsp" />
	<form:form modelAttribute="wrapper" commandName="wrapper" action="cocktailtoegevoegd.html">
		<h2>${pagina}</h2>
		<table>
			<tr>
				<td>Naam :</td>
				<td><form:input path="cocktail.naam" /> <font color="red"> <form:errors path="cocktail.naam" /></font></td>
			</tr>
			<tr>
				<td>Prijs :</td>
				<td><form:input path="cocktail.prijs" /> <font color="red"> <form:errors path="cocktail.prijs" /></font></td>
			</tr>
			
 			<tr>
 				
				<td>Ingredients :</td>
				<td>AlcoholPercentage :</td>
				<td>Hoeveelheid :</td>
			</tr>
			
		
			<%-- JACK'S <c:forEach items="${wrapper.ingredientMap.keySet()}" var="ingredient"> --%>
			<c:forEach var="ingredient" items="${wrapper.ingredientMap}" varStatus="status">
				<c:set var="ingredientSet" value="${ingredient.key}"/>
				<tr>
					<td>${ingredientSet.naam}</td>
					<td>${ingredientSet.alcoholpercentage}</td>
					
   					<td><input name="'hoeveelheid'${ingredientSet.id}" value="${ingredient.value}"/></td>
					
<%-- JACKS'S			<td><form:input path="ingredient.${ingredient.id}" 
 					value="${wrapper.ingredientMap.get(ingredient)}" /></td> --%>
					
				</tr>
				
			</c:forEach>
			<tr>
				<td colspan="2"><input type="submit" value="Toevoegen"
					name="action" /></td>
			</tr>
		</table>
		<br />
	</form:form>
	<br />
	<br />
	<table border="1">
		<th>ID</th>
		<th>Naam</th>
		<th>Prijs</th>
		<c:forEach items="${cocktailList}" var="cocktail">
			<tr>
				<td>${cocktail.id}</td>
				<td>${cocktail.naam}</td>
				<td>${cocktail.prijs}</td>
			</tr>
		</c:forEach>
	</table>
	<br />
	<br />
	<div class="footer">Copyright by Eloisa Ranches (Project
		Spring-MVC-Hibernate)</div>
</body>
</html> 

CODE --> CocktailController.java

package be.intecbrussel.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import be.intecbrussel.entities.Cocktail;
import be.intecbrussel.entities.Ingredient;
import be.intecbrussel.entities.Samenstelling;
import be.intecbrussel.entities.Wrapper;
import be.intecbrussel.services.CocktailService;
import be.intecbrussel.services.IngredientService;
import be.intecbrussel.services.SamenstellingService;

@Controller
public class CocktailsController {

	private final CocktailService cocktailService;
	
	@Resource
	private IngredientService ingredientService;
	
	@Resource
	private SamenstellingService samenstellingService;
	
	@Autowired
	public CocktailsController(CocktailService cocktailService) {
		this.cocktailService = cocktailService;
	}
	
	@RequestMapping(value = "/cocktailstonen")
	public ModelAndView showCocktails() {
		ModelAndView modelAndView = new ModelAndView("cocktails/cocktailstonen");
		modelAndView.addObject("cocktails", cocktailService.findAll());
		return modelAndView;
	}

	@RequestMapping(value = "/cocktailtoevoegen", method = RequestMethod.GET)
	public ModelAndView cocktailtoevoegen(@Valid Wrapper wrapper) {

		ModelAndView modelAndView = new ModelAndView("cocktails/toevoegen", "wrapper", new Wrapper());
		List<Ingredient> ingredientList = ingredientService.findAll();
		Map<Ingredient, BigDecimal> ingredientMap = new HashMap<Ingredient, BigDecimal>();

		for (Ingredient ingredient: ingredientList)
		{
			ingredientMap.put(ingredient, BigDecimal.ZERO);
		}
		
		wrapper = new Wrapper();
		wrapper.setIngredientMap(ingredientMap);
		
		modelAndView.addObject("wrapper", wrapper);
		return modelAndView;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/cocktailtoegevoegd")
	public ModelAndView cocktailToegevoegd(@Valid @ModelAttribute Wrapper wrapper, 
			BindingResult result, Map<String, Object> map){
		
		ModelAndView modelAndView = new ModelAndView("cocktails/toevoegen");
		System.out.println(wrapper.getIngredientMap().size());
		if (result.hasErrors()) {
			modelAndView.addObject(wrapper);
			return modelAndView;
		} else {
			Samenstelling samenstelling = new Samenstelling();
			Map<Ingredient, BigDecimal> ingredientMap = null;
			ingredientMap.putAll(wrapper.getIngredientMap());
			
			Iterator entries = ingredientMap.entrySet().iterator();
			while (entries.hasNext()) {
			    Map.Entry entry = (Map.Entry) entries.next();
			    Ingredient key = (Ingredient)entry.getKey();
			    BigDecimal value = (BigDecimal)entry.getValue();
			    
			    System.out.println("Key = " + key + ", Value = " + value);
			}
			
			samenstelling.setCocktail(wrapper.getCocktail());
			samenstellingService.create(samenstelling);
			Cocktail cocktail = new Cocktail();
			Cocktail cocktailResult = new Cocktail();
			cocktailResult = cocktail;
			map.put("cocktail", cocktail);
			map.put("cocktailList", cocktailService.findAll());
		}
		return new ModelAndView("redirect:/cocktailtoevoegen");
	}

	@RequestMapping("/cocktailVerwijderen")
	public ModelAndView cocktailVerwijderen(Map<String, Object> map) {
		Cocktail cocktail = new Cocktail();
		map.put("cocktail", cocktail);
		map.put("cocktailList", cocktailService.findAll());
		ModelAndView modelAndView = new ModelAndView("cocktails/verwijderen");
		modelAndView.addObject("cocktails", cocktailService.findAll());
		return modelAndView;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/cocktailVerwijderd")
	public ModelAndView cocktailVerwijderd(@ModelAttribute Cocktail cocktail,
			@RequestParam("id") long id, BindingResult result,
			Map<String, Object> map) {
		Cocktail cocktailResult = new Cocktail();
		cocktailService.delete(cocktail.getId());
		cocktailResult = cocktail;
		map.put("cocktail", cocktail);
		map.put("cocktailList", cocktailService.findAll());
		ModelAndView modelAndView = new ModelAndView("cocktails/verwijderen");
		return modelAndView;
	}
	
	@RequestMapping(value = "/cocktailsIngredients")
	public ModelAndView showIngredients() {
		ModelAndView modelAndView = new ModelAndView("cocktails/cocktailstonen");
		modelAndView.addObject("cocktails", cocktailService.findAll());
		return modelAndView;
	}


} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close