Session tracking does not persist

39 Views Asked by At

Here is my code. The HttpSession we used was also used in class and it worked but doesn't work now. And I cannot print anything after the for loop. I use doGet() method. We need to store the stringToPhone in a session object and the carrierNum to a cookie.

    String carrier = request.getParameter("carrier");
    String carrierNum = request.getParameter("carrierNum");
    String stringToPhone = request.getParameter("phoneNumber");
    String equiPhoNum = "";

    PrintWriter out = response.getWriter();
    HttpSession s1 = request.getSession();

    out.println("Carrier: " + carrier);
    out.println("Inputted phone number: " + stringToPhone);

    String[] x = stringToPhone.split("");       

    int i = 0;
    out.print("PHONE NUMBER: " + carrierNum + "-");

    for(i = 0; i <= 7; i++){
        if(x[i].equals("A") || x[i].equals("B") || x[i].equals("C") || x[i].equals("a") || x[i].equals("b") || x[i].equals("c")){
            x[i] = "2";
        }
        if(x[i].equals("D") || x[i].equals("E") || x[i].equals("F") || x[i].equals("d") || x[i].equals("e") || x[i].equals("f")){
            x[i] = "3";
        }
        if(x[i].equals("G") || x[i].equals("H") || x[i].equals("I") || x[i].equals("g") || x[i].equals("h") || x[i].equals("i")){
            x[i] = "4";
        }
        if(x[i].equals("J") || x[i].equals("K") || x[i].equals("L") || x[i].equals("j") || x[i].equals("k") || x[i].equals("l")){
            x[i] = "5";
        }
        if(x[i].equals("M") || x[i].equals("N") || x[i].equals("O") || x[i].equals("m") || x[i].equals("n") || x[i].equals("o")){
            x[i] = "6";
        }
        if(x[i].equals("P") || x[i].equals("Q") || x[i].equals("R") || x[i].equals("S") || x[i].equals("p") || x[i].equals("q") || x[i].equals("r") || x[i].equals("s")){
            x[i] = "7";
        }
        if(x[i].equals("T") || x[i].equals("U") || x[i].equals("V") || x[i].equals("t") || x[i].equals("u") || x[i].equals("v")){
            x[i] = "8";
        }
        if(x[i].equals("W") || x[i].equals("X") || x[i].equals("Y") || x[i].equals("Z") || x[i].equals("W") || x[i].equals("x") || x[i].equals("y") || x[i].equals("z")){
            x[i] = "9";
        }
        equiPhoNum = x[i] + "";
        if(equiPhoNum != " " && equiPhoNum != null){
            s1.setAttribute("Value1", equiPhoNum); 
        }
        out.print(s1.getAttribute("Value1"));
    }
1

There are 1 best solutions below

0
Michael Woyo On

I would suggest you create the equipPhoNum string before you check for the condition. As it stands now, for each iteration, it will check

if(equiPhoNum != " " && equiPhoNum != null)

and remember, you are using the default delimiter which is split("") so the first element will be a blank space " ". So the compiler skips

s1.setAttribute("Value1", equiPhoNum);

Then after that, you want to print a value that has not been set. You will surely get a NullPointerException or something similar. So move the condition outside of the for loop and try it once more.

Let us know what the result is.