Nullpointerexception on a Servlet when using Datavoting method

Asked

Viewed 68 times

1

I’m working with a Servlet however, this giving error java.lang.NullPointerException when I use the object method DataVoting. Follows 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);
}
  • Where the mistake happens?

  • In dataVoting.Vote(1);

  • It is impossible to give this error in this location, it only tries to execute this method if it is not null.

  • So yesterday I showed the teacher, I even gave a system out inside the case and it worked the system out. Even letting everything commenting on the IDE point out the same error. Professor also failed to see reason for the error.

  • Code needs to be well done to always work. When it works in certain situations and others do not, the code needs to be rewritten. I’ll try to answer but I don’t know if it’ll help you since the past information doesn’t match.

  • Thank you, I just really wanted to understand this mistake, because I’ve never seen it that way. But as he said, he can show the return of a method in the system out, but he can’t do an operation that involves changing an attribute.

Show 1 more comment

2 answers

2


It’s hard to say for sure with so little information and uncompromised. You say the code is poorly done just to see if it works. The problem is there, do it well and it will work. All the code should be written differently.

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

If the past information is minimally true, clearly there is a situation where this object becomes 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 execution. You cannot attempt to execute in invalid state.

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

Codes need to be written thinking of all possible situations. They need to be tested in all circumstances that may occur. And testing when information is non-existent is basic. This code seems to do too much too.

It gets hard to help in code that makes little sense. Even if you fix your error, you will have to change the code, then other errors can be entered. Leave the code as good as it can. If he’s good, he won’t make mistakes, but if he is, at least it’ll be on something that makes sense, something that’s not hypothetical.

Learn how to run code on Debugger. Running step by step, seeing all the values, you will learn a lot and find out where and how to solve the error.

  • Okay, thanks I’ll redo the code ^^!

0

Thanks for all your help, finding out the problem. It was related to the IDE every time I had it compiled it couldn’t create a new one build, so that even when I simply put the commented code she still claimed error in the comment. Even cleaning did not work. So I created another class, copied my code from get() and put in this new class, I erased the build previous in hand and ended the process of the IDE and the server. With this we could better analyze the code without that strange error. And Maniero wiped the code. It was like this at the time, I will improve yet:

@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);
}
  • The code is much better. I don’t know if it does everything you expect but it can’t get much better than this. The only thing that may be exaggerated is a lot of variable declaration when using the die only once, or separate statement of assignment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.