3

I've searched for this but can't find anything. Please correct my question if it's incorrect english.

This is my code: EDIT: The code is within my .jsp file!

    function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Beoordeling', 'Stage Opleider', 'Student'],

        ['1', '1', '4'],

        <% ArrayList < Stelling > alleStellingenLijst2 = new ArrayList < Stelling > ();
        alleStellingenLijst2 = (ArrayList < Stelling > ) request.getAttribute("stellingen");
        for (Stelling s: alleStellingenLijst2) {
            out.println("['1', '" + s.getDeStelling() + "' , '" + s.getDeWaarde() + "'],");
        } %> ]);
    var options = {
        title: 'Laatste competenties',
        hAxis: {
            title: 'Score',
            titleTextStyle: {
                color: 'green'
            }
        },
        vAxis: {
            title: 'Beoordeling nummer',
            titleTextStyle: {
                color: 'green'
            }
        },
        // Allow multiple simultaneous selections.
        selectionMode: 'multiple',
        colors: ['#BEF781', 'green']
    };
    var chart = new      google.visualization.BarChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}

For some reason, it wont execute the code between the <% %> (from the jsp).

This page online: http://project-omega.appspot.com/grafieken.jsp The google app engine logs say the error is on the last line of my page. It's a nullpointerexception.

I have no idea what it means and I really hope someone can help me.

Thanks a lot and sorry for my english.

EDIT The rendered output looks as follows

  function drawChart() {
    var data = google.visualization.arrayToDataTable([                                               
      ['Beoordeling', 'Stage Opleider', 'Student'],
      for (Stelling s : alleStellingenLijst2) {
        out.println("['1', '" + s.getDeStelling() + "' , '" + s.getDeWaarde() + "'],");       
    }  
  ]);

NEW CODE:

 function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Beoordeling', 'Stage Opleider', 'Student'],

          ['1', 1, 4],

          <%

            ArrayList<Stelling> alleStellingenLijst2 =(ArrayList<Stelling>) getServletContext().getAttribute("stellingen");
            for (Stelling s : alleStellingenLijst2) {
                out.println("['1', " + s.getDeStelling() + " , " + s.getDeWaarde() + "],");       
            }   
        %> 
        ['2', 2, 2]
        ]);

2 Answers 2

4

These are JSP markups, you cannot use them in JavaScript!

That's because JSP files are compiled to the .java classes during compilation, and JavaScript is executed on the client side.

You could do the opposite - generate a JavaScript code in the JSP file, that way you could pass some data you want to the JS variables.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer, what would you suggest me to do? I need to have data from the Objects from the array in the data var.
@colin Is that function defined inside the .jsp file or in a separate .js file loaded with a <script> tag? If it's the former then there shouldn't be an issue, other than your Java scriptlet not working correctly.
@Anthony The function is defined in the .jsp file.
@colin Then ignore this answer entirely, it would only be relevant in the latter case.
Ok, I've updated my code now, could you maybe take a look at it? Thank you
1

I suppose you haven't set the stellingen request attribute. You usually set the request attributes in a servlet, before forwarding the request to jsp:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    ArrayList<Stelling> list = ...;
    req.setAttribute("stellingen", list);
    req.getRequestDispatcher("/grafieken.jsp").forward(req, resp);
}

Also make sure the attribute is set in the JSP code:

<%
List<Stelling> stellingen = (List<Stelling>) getServletContext().getAttribute("stellingen");
if(stellingen == null) {
    out.println("stellingen attribute not set!");
}else{
    for (Stelling s : stellingen) {
        out.println("['1', " + s.getDeStelling() + " , " + s.getDeWaarde() + "],");       
    }   
}
%> 

5 Comments

Thanks for the answer, maybe i'm getting the arraylist (as attribute) wrong? I have updated the code in the original question Eclipse gives a warning saying: Type safety unchecked cast from Object to ArrayList<Stelling>
The warrning is expected, because req.getAttribure() returns an Object.
How and when are you setting ServletContext.setAttribure("stellingen",...)?
I set the Attribute in the UserContextListener Servlet. sce.getServletContext().setAttribute("stellingen", alleStellingen);
That would be ok. Double check if it's not null when you set it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.