0

I'm trying to pass arrays to a GET request in a servlet, but it keeps turning up with a 500 error. Where did I go wrong? I console.log'ed all the arrays (types, lengths etc.) and they all turn up fine. The Types.length in the doGet gives a NullPointerExecption.

JS:

function updatePrice(types, lengths, heights, grits, cuts, quantities) {
console.log(types);
$.get("GetPrice", {
    types: types,
    lengths: lengths,
    heights: heights,
    grits: grits,
    cuts: cuts,
    quantities: quantities },
    function(responseText) {
        $("#createOrderCost").empty().append(responseText);
    });
}

doGet (Located using @WebServlet("/GetPrice")):

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String Types[] = request.getParameterValues("types"); 
    String Lengths[] = request.getParameterValues("lengths"); 
    String Heights[] = request.getParameterValues("heights"); 
    String Grits[] = request.getParameterValues("grits"); 
    String Cuts[] = request.getParameterValues("cuts"); 
    String Quantities[] = request.getParameterValues("quantities");
    double totalPrice = 0;
    int lengthConvert = 0;
    double lengthPrice = 0;
    for (int i = 0; i < Types.length; i++) {
        Product currProduct = ProductDAO.getProduct(Types[i], Integer.parseInt(Heights[i]), Integer.parseInt(Grits[i]), Integer.parseInt(Cuts[i]));
        lengthConvert = Integer.parseInt(Lengths[i])/1000;
        lengthPrice = lengthConvert*currProduct.getPrice();
        totalPrice += lengthPrice*Integer.parseInt(Quantities[i]);
    }
        String text = Double.toString(totalPrice);
        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8"); 
        response.getWriter().write(text);       
}

500 error:

jul. 31, 2017 3:54:22 PM org.apache.catalina.core.StandardWrapperValve 
invoke
SEVERE: Servlet.service() for servlet [webservices.GetPrice] in context with 
path [/Nordic_Abrasive_Online] threw exception
java.lang.NullPointerException
at webservices.GetPrice.doGet(GetPrice.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Showing your 500 error would help. And 500 type of errors should output code line that's throwing in (server logs) – CrazySabbath Jul 31 '17 at 07:50
  • Ah sorry, it gives me an error at the for loop in the doGet - Problem with Types.length – Jeppe Gelardi Madsen Jul 31 '17 at 07:51
  • 1
    Can you show the output of `console.log(types);`? – CrazySabbath Jul 31 '17 at 07:52
  • I added four lines at it gives: (4) ["Wood", "Brush", "Wood", "Wood"] just as it should. – Jeppe Gelardi Madsen Jul 31 '17 at 07:54
  • Are you absolutely sure this `String Types[] = request.getParameterValues("types"); ` is not null? – CrazySabbath Jul 31 '17 at 07:59
  • `currProduct` is null. thats the problem. your DAO is broken. – user2914191 Jul 31 '17 at 08:07
  • Not at all, I'm pretty sure it is null.. But I have no idea why it would be. I can't print it to system.out either as that will also just give me a NullPointer – Jeppe Gelardi Madsen Jul 31 '17 at 08:08
  • run in debug mode and trace it down into ProductDAO see where it's screwing up – user2914191 Jul 31 '17 at 08:09
  • My DAO is fine I'm pretty sure, I use the exact same method in other servlets without any problems.. – Jeppe Gelardi Madsen Jul 31 '17 at 08:09
  • Same in debug mode.. Gives NullPointerException at line "for (int i = 0; i < Types.length; i++) {" – Jeppe Gelardi Madsen Jul 31 '17 at 08:14
  • 1
    @user2914191 DAO is not broken, did you even read the question? Types.length gives the error because Types is null. – CrazySabbath Jul 31 '17 at 08:14
  • Okay I finally got it to print that Types is Null, but how come? When I console.log types in JS inside the ajax call it returns the correct values.. – Jeppe Gelardi Madsen Jul 31 '17 at 08:16
  • GetPrice?types%5B%5D=Wood&lengths%5B%5D=100&heights%5B%5D=46&grits%5B%5D=120&cuts%5B%5D=3&quantities%5B%5D=3 Failed to load resource: the server responded with a status of 500 (Internal Server Error) – Jeppe Gelardi Madsen Jul 31 '17 at 08:22
  • @Yazan how is that a possible duplicate? – CrazySabbath Jul 31 '17 at 08:27
  • From java docs: getParameterValues: `Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.` Check if HttpServletRequest contains types parameter. – CrazySabbath Jul 31 '17 at 08:28
  • Yazan that's not really relevant.. Problem is parsing an array from a JS ajax GET request to a servlet. – Jeppe Gelardi Madsen Jul 31 '17 at 08:28
  • @JeppeGelardiMadsen yes, that's true, i retracted the close vote – Yazan Jul 31 '17 at 08:39
  • @CrazySabbath Can't find anything that says it shouldn't be doable. I have other servlets using POST method, passing multiple selects with the same name with no problems using getParameterValues.. The URL when passing an array larger than 1 value looks like this: http://localhost:8080/Nordic_Abrasive_Online/GetPrice?types%5B%5D=Brush&types%5B%5D=Sealer&lengths%5B%5D=100&lengths%5B%5D=100&heights%5B%5D=46&heights%5B%5D=46&grits%5B%5D=240&cuts%5B%5D=3&quantities%5B%5D=1&quantities%5B%5D=1 It looks alright I think? – Jeppe Gelardi Madsen Jul 31 '17 at 08:42
  • have you checked this https://stackoverflow.com/questions/3061273/send-an-array-with-an-http-get pass them with the same name ex, `abc?types=wood&types=brush&types=...` – Yazan Jul 31 '17 at 08:48
  • @Yazan I think this solved it! Have to play around with it a bit, but now with just adding [] to getParameterValues("Types[]") I can return values! – Jeppe Gelardi Madsen Jul 31 '17 at 09:01
  • 1
    good to hear that, as mentioned in the answer, there is no really standard way to do this, it's good you figured it out. – Yazan Jul 31 '17 at 09:17
  • 1
    @Yazan it works perfectly now, it was the solution! Thanks! – Jeppe Gelardi Madsen Jul 31 '17 at 09:36
  • @BalusC this has nothing to do with the mentioned question as duplicate **at all** ! how is this a duplicate ? – Yazan Aug 01 '17 at 12:58

0 Answers0