index.jsp
classe XMLManager:codice:<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Nome del sito</title> </head> <body> <link href="CSS/style.css" rel="stylesheet" type="text/css" /> </head> <body> <%@include file="CSS/header.html" %> <div id="container"> <%@ include file ="CSS/left.jsp" %> </div><!--#navigation--> <div id="center"> contenuto della home </div> </div><!--#container--> </body> </html>
left.jspcodice:package it.unirc.bd2.sito.util; import java.io.File; import java.io.IOException; import java.util.Iterator; import org.jdom2.Content; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; public class XMLManager { // si potrebbe rendere più efficiente evitando il parsin ogni volta che si chiede il testo public static String getText(String key, String country){ //potrei passare la variabile path direttamente qui, non uso l'indirizzo!!! devo usare la variabile path!! //se country = it fai così (Gestione country in base alla lingua) //gestione country //File file=new File("src/it/unirc/bd2/xml/Data_"+country+".xml"); // creo l'oggetto di tipo file che è l'input File file =new File("Data_"+country+".xml"); //if(!file.exists()) // se non esiste creo file //file= new File("c:/Data_IT.xml"); //file = new File("src/it/unirc/bd2/xml/Data_"+country+".xml"); SAXBuilder builder= new SAXBuilder(); // oggetto che si occuperà del parsing del documento creato Document document =null; //l'output try { document=builder.build(file); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Element root=document.getRootElement(); Content c; Iterator<Content> discendenti=document.getDescendants(); //document.getDescendants(); // mi restituisce un 'iteratore di contenuti while (discendenti.hasNext()){ // serve a navigare sul documento xml quando non conosciamo la struttura c=discendenti.next(); if (c.getCType().equals(Content.CType.Element)){ //verifico che ciò che cerco è un element Element e=(Element) c; if (e.getName().equals(key)) return (e.getText()); } } return ""; } }
Data_IT.xmlcodice:<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="it.unirc.bd2.sito.util.XMLManager"%> <div id="left"> <% //String path=request.getRealPath(""); //la risorsa di cui voglio il path fisico passando il path relativo XMLManager.getText("item1","IT"); //out.print(path); %> <br/> voce2<br/> <a href="Login.jsp">Login</
codice:<?xml version="1.0" encoding="UTF-8"?> <root> <item1>Gestione Studenti</item1> <item2>Ciao</item2> </root>pare che il problema sia nel "Element root=document.getElementRoot();"Grave: Servlet.service() for servlet [jsp] in context with path [/sito] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at it.unirc.bd2.sito.util.XMLManager.getText(XMLManag er.java:48)
at org.apache.jsp.index_jsp._jspService(index_jsp.jav a:96)
at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:727)
at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:727)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilt er(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBas e.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.p rocess(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnect ionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProce ssor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker( Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (Unknown Source)
at java.lang.Thread.run(Unknown Source)
cosa consigliate?