Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Null pointer exception while using cookie to remember the previous color

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Null pointer exception while using cookie to remember the previous color

    I'm trying to write a simple program to have the background on a html page changed by selecting the colour on radio buttons, this I managed to do successfully. As soon as I add a cookie to remember the colour and load the same background the next time I get null pointer exceptions all over the place.

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
     
    public class ColourSelect extends HttpServlet {
    	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
    			String pageColour = "Black";
    			String colour = req.getParameter("colour");
    			Cookie colourCookie = new Cookie("colour", colour);
    			if (colourCookie != null) {	
    				res.addCookie(colourCookie);
    				colourCookie.setMaxAge(100000);
    				Cookie[] cookies = req.getCookies();
    				pageColour = getLastColour(cookies);
    			} else {
    					pageColour = colour;
    			}
    			res.setContentType("text/html");	
    			ServletOutputStream out = res.getOutputStream();
    			out.println("<html>");
    			out.println("<body bgcolor=\"" + pageColour + "\">");
    			out.println("<font color=\"White\">");
    			out.println("<head><title>Change Colour</title></head>");
    			out.println("<h1><p>Background Colour</p></h1>");
    			out.println("<p>What colour do you want the background?</p>");
    			out.println("<form>");
    			out.println("Yellow</br><input type=\"radio\" name=\"colour\" value=\"Yellow\" /></br>");
    			out.println("Green</br><input type=\"radio\" name=\"colour\" value=\"Green\" /></br>");
    			out.println("Blue</br><input type=\"radio\" name=\"colour\" value=\"Blue\" /></br>");
    			out.println("Black</br><input type=\"radio\" name=\"colour\" value=\"Black\" /></br>");
    			out.println("<input type=submit value=\"Change Background\"/>");
    			out.println("</font>");
    			out.println("</form>");
    			out.println("</body>");
    			out.println("</html>");
    		}
     
    	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    			doPost(req, res);
    		}
     
    		String getLastColour(Cookie[] cookies){
    				String inColour="white";
    				for (int i = 0; i < cookies.length; i++) {
    						String cookieName = cookies[i].getName();
    						if (cookieName.equals("colour"))
    							inColour = cookies[i].getValue();
    					}
    					return inColour;
    				}
    }

    Can anyone help, I'm really stuck with this, no matter what I try it just doesn't seem to work.


  2. #2
    Junior Member
    Join Date
    Jun 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Struggling with cookies

    OK I've solved the null pointer problem, if there are no cookies sent to the server it returns null so I added an if statement in the right place. Now however when I first set the colour it turns the background white no matter which colour I choose, then I select the next colour and it turns the background the colour of the first one I chose etc etc. Once that's done it's not remembering the colours when I reload it. Here's the code test it out and you'll see what I mean. I'll continue working away on this myself but would appreciate some help.

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
     
    public class ColourSelect extends HttpServlet {
    	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
    			String pageColour;
    			String colour = req.getParameter("colour");
    			Cookie colourCookie = new Cookie("colour", colour);	
    			res.addCookie(colourCookie);
    			colourCookie.setMaxAge(100000);
    			Cookie[] cookies = req.getCookies();
    			if (cookies != null) {	
    				pageColour = getLastColour(cookies);
    			} else { pageColour = colour; }
    			res.setContentType("text/html");	
    			ServletOutputStream out = res.getOutputStream();
    			out.println("<html>");
    			out.println("<body bgcolor=\"" + pageColour + "\">");
    			out.println("<font color=\"White\">");
    			out.println("<head><title>Change Colour</title></head>");
    			out.println("<h1><p>Background Colour</p></h1>");
    			out.println("<p>What colour do you want the background?</p>");
    			out.println("<form>");
    			out.println("Yellow</br><input type=\"radio\" name=\"colour\" value=\"Yellow\" /></br>");
    			out.println("Green</br><input type=\"radio\" name=\"colour\" value=\"Green\" /></br>");
    			out.println("Blue</br><input type=\"radio\" name=\"colour\" value=\"Blue\" /></br>");
    			out.println("Black</br><input type=\"radio\" name=\"colour\" value=\"Black\" /></br>");
    			out.println("<input type=submit value=\"Change Background\"/>");
    			out.println("</font>");
    			out.println("</form>");
    			out.println("</body>");
    			out.println("</html>");
    		}
     
    	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    			doPost(req, res);
    		}
     
    		String getLastColour(Cookie[] cookies){
    				String inColour="Black";
    				for (int i = 0; i < cookies.length; i++) {
    						String cookieName = cookies[i].getName();
    						if (cookieName.equals("colour"))
    							inColour = cookies[i].getValue();
    					}
    				return inColour;
    			}
    }

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Struggling with cookies

    Ah I see the problem now. Is there anyway of reloading a cookie only when the page is loaded and not everytime when the submit button is clicked?

  4. #4
    Junior Member
    Join Date
    Jun 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Struggling with cookies

    OK it displays ok and changes colour ok it just doesn't do what it's supposed to do and remember the colour for the next time the page is loaded.

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
     
    public class ColourSelect extends HttpServlet {
    	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
    			String pageColour;
    			String colour = req.getParameter("colour");
    			Cookie colourCookie = new Cookie("colour", colour);	
    			colourCookie.setMaxAge(100000);
    			Cookie[] cookies = req.getCookies();
    			if (cookies != null) {	
    				res.addCookie(colourCookie);
    				pageColour = getLastColour(cookies);
    			} else { pageColour = colour; }
    			res.setContentType("text/html");	
    			ServletOutputStream out = res.getOutputStream();
    			out.println("<html>");
    			out.println("<body bgcolor=\"" + pageColour + "\">");
    			out.println("<font color=\"White\">");
    			out.println("<head><title>Change Colour</title></head>");
    			out.println("<h1><p>Background Colour</p></h1>");
    			out.println("<p>What colour do you want the background?</p>");
    			out.println("<form>");
    			out.println("Yellow</br><input type=\"radio\" name=\"colour\" value=\"Yellow\" /></br>");
    			out.println("Green</br><input type=\"radio\" name=\"colour\" value=\"Green\" /></br>");
    			out.println("Blue</br><input type=\"radio\" name=\"colour\" value=\"Blue\" /></br>");
    			out.println("Black</br><input type=\"radio\" name=\"colour\" value=\"Black\" /></br>");
    			out.println("<input type=submit value=\"Change Background\"/>");
    			out.println("</font>");
    			out.println("</form>");
    			out.println("</body>");
    			out.println("</html>");
    		}
     
    	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    			doPost(req, res);
    		}
     
    		String getLastColour(Cookie[] cookies){
    				String inColour="Black";
    				for (int i = 0; i < cookies.length; i++) {
    						String cookieName = cookies[i].getName();
    						if (cookieName.equals("colour"))
    							inColour = cookies[i].getValue();
    					}
    				return inColour;
    			}
    }

Similar Threads

  1. Details about 'CopyTo' of Arrays in Java
    By Fendaril in forum Collections and Generics
    Replies: 18
    Last Post: November 13th, 2008, 07:31 AM