I'm working with a servlet but this is giving% error of% when I use the method of object java.lang.NullPointerException
. Here is the code:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DataVoting dataVoting;
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
request.getSession().invalidate();
dataVoting = new DataVoting();
if(request.getSession() == null){
request.getSession(true);
} else {
dataVoting = (DataVoting)request.getSession().getAttribute("DataVoting");
}
request.getSession().setAttribute("DataVoting", dataVoting);
Gson gson = new Gson();
String discipline = request.getParameter("discipline");
if(discipline != null) {
switch(discipline) {
case "Desenvolvimento de Sistema Web3":
if(dataVoting != null) {
dataVoting.Vote(1);
}
break;
case "Sistema Operacional":
dataVoting.Vote(2);
break;
case "Engenharia de Software":
dataVoting.Vote(3);
break;
case "Inglês Aplicado":
dataVoting.Vote(4);
break;
default:
System.out.println("Nenhum case!");
break;
}
}
request.getSession().setAttribute("DataVoting", dataVoting);
String json = gson.toJson(dataVoting);
response.getWriter().print(json);
}