NullPointerException on a servlet when using DataVoting method

1

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);
}
    
asked by anonymous 29.08.2015 / 13:08

2 answers

2

It is difficult to say with certainty with so little information and mismatch. You say the code is badly made just to see if it works. The problem is there. Do it well done and it will work. All code should be written in another way.

In this specific case in 3 case s accesses the object dataVoting which can be null without checking.

If the last information is truly true, there is clearly a situation where this object is null, and in this case the problem is more serious and this whole method is compromised. Either arrange a way to get the value or it should abort the execution. You can not try to run in invalid state.

This may not even be the problem, but this is a potential visible problem. Other problems not visible only with what was posted may exist.

Codes must be written with all possible situations in mind. They need to be tested in all circumstances that can occur. And testing when information is non-existent is basic. This code seems to do too many things too.

It's hard to help in code that makes little sense. Even if it solves the error of it, you will have to change the code, then other errors can be introduced. Leave the code as good as it gets. If it is good, it will have no errors, but if it does, at least it will be on top of something that makes sense, something that is not hypothetical.

Learn to run the code in debugger . Go running step-by-step, seeing all the values, you will learn a lot and find out where and how to solve the error.

    
29.08.2015 / 13:38
0

Thanks for everyone's help, you can figure out the problem. It was related to IDE every time I send it compiles it could not create a new build, so even when I simply put the commented code it still claimed error in the comment. Even cleaning did not work. Then I created another class, copied my get code and put it into that new class, deleted the previous build in my hand, and shut down the IDE and server process. With this it gave to better analyze the code without that strange error. And bigown I wiped the code. It was like this at the moment, I will improve even more:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Gson gson = new Gson();   
    DataVoting dataVoting;
    dataVoting = new DataVoting();
    if(request.getSession() == null){
        request.getSession(true);
    } else {
        if(request.getSession().getAttribute("DataVoting") != null) {;
            dataVoting = gson.fromJson((String)request.getSession().getAttribute("DataVoting"), DataVoting.class);
        }
    }
    String discipline = request.getParameter("discipline");
    dataVoting.Vote(discipline);
    String json = gson.toJson(dataVoting);
    request.getSession().setAttribute("DataVoting", json);
    response.setContentType("application/json");
    response.getWriter().write(json);
}
    
30.08.2015 / 14:42