Problema con ServletContextListener
Publicado por tanGoO (1 intervención) el 22/01/2015 18:04:15
Buenas tengo el siguiente problema, estoy haciendo un ejercicio y tengo creado un arraylist en una clase que implemeta ServletContextListener y lo tengo que llamar desde otro servlet para almacenar objetos dentro del arraylist adjunto el codigo:
Clase con ServletContextListener
Clase donde llamo a el arraylist:
Y el error que me devuelve:
Clase con ServletContextListener
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.ArrayList;
import javax.servlet.*;
/**
*
* @author tanGoO-Droid
*/
public class InitContext implements ServletContextListener {
/**
* Default constructor.
*/
public InitContext() {
// TODO Auto-generated constructor stub
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent e) {
ServletContext cntxt = e.getServletContext();
ArrayList<ObservacioMeteorologica> observacions = new ArrayList<ObservacioMeteorologica>();
cntxt.setAttribute("arr1", observacions);
// Creem un atribut de context, en el moment en què s'inicialitza l'aplicació,
// que conté un valor de tipus Long inicialitzat a 0.
//e.getServletContext().setAttribute("comptador", new Long(0));
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
}
Clase donde llamo a el arraylist:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class RegistrarObservacio extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
// TODO Auto-generated method stub
// obtiene los valores del formulario
String mun = req.getParameter("Municipi");
String data = req.getParameter("Dobservacio");
Double TempMax = Double.parseDouble(req.getParameter("TMAX"));
Double TempMin = Double.parseDouble(req.getParameter("TMIN"));
Double Prec = Double.parseDouble(req.getParameter("Precipitacions"));
Date fecha = null;
SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yyyy");
try {
fecha = sdf.parse(data);
} catch (ParseException ex) {
}
if (mun == null | data == null) {
resp.sendRedirect("entrarObservacio.html");
} else {
ArrayList<ObservacioMeteorologica> observacions;
observacions = (ArrayList<ObservacioMeteorologica>) getServletContext().getAttribute("arr1");
ObservacioMeteorologica obmet = new ObservacioMeteorologica(mun, fecha, TempMax, TempMin, Prec);
observacions.add(obmet);
req.setAttribute("d", obmet);
req.getSession().setAttribute("observacio", observacions);
req.getRequestDispatcher("DadesOK.jsp").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
/*public void service(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
}*/
}
Y el error que me devuelve:
1
2
3
4
5
java.lang.NullPointerException
appmeteo.RegistrarObservacio.doGet(RegistrarObservacio.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Valora esta pregunta


0