SHOW:
|
|
- or go back to the newest paste.
1 | package com.versefeed.crud; | |
2 | ||
3 | import java.io.IOException; | |
4 | import java.util.List; | |
5 | ||
6 | import javax.servlet.http.HttpServlet; | |
7 | import javax.servlet.http.HttpServletRequest; | |
8 | import javax.servlet.http.HttpServletResponse; | |
9 | import javax.swing.text.Utilities; | |
10 | ||
11 | import com.google.appengine.api.datastore.DatastoreService; | |
12 | import com.google.appengine.api.datastore.DatastoreServiceFactory; | |
13 | import com.google.appengine.api.datastore.Entity; | |
14 | import com.google.appengine.api.datastore.Text; | |
15 | ||
16 | /* | |
17 | i declare and instantiate class variable "private CrudDsUtilities utilities" in the servlet constructor, but it's not seen in other functions of the servlet. it is throwing java.lang.NullPointerException | |
18 | ||
19 | */ | |
20 | ||
21 | public class CrudServlet extends HttpServlet { | |
22 | ||
23 | private DatastoreService ds; | |
24 | private String entityKind; | |
25 | private CrudDsUtilities utilities; | |
26 | ||
27 | private void CrudServlet() { | |
28 | ds = DatastoreServiceFactory.getDatastoreService(); | |
29 | utilities = new CrudDsUtilities(); | |
30 | ||
31 | } | |
32 | ||
33 | public void doGet(HttpServletRequest req, HttpServletResponse resp) | |
34 | throws IOException { | |
35 | ||
36 | String operation = req.getParameter("op"); | |
37 | String verseTitle, verseRef, verseBody; | |
38 | long verseId; | |
39 | switch (operation) { | |
40 | ||
41 | case "view": | |
42 | verseId = Long.getLong(req.getParameter("verseId")); | |
43 | entityKind = req.getParameter("entityKind"); | |
44 | ||
45 | break; | |
46 | ||
47 | case "viewverses": | |
48 | entityKind = req.getParameter("entitykind"); | |
49 | System.out.println("entityKind: "+ entityKind); | |
50 | this.viewVerses(req, resp); | |
51 | break; | |
52 | ||
53 | default: | |
54 | break; | |
55 | } | |
56 | ||
57 | } | |
58 | ||
59 | public void viewVerse(long verseId) { | |
60 | ||
61 | } | |
62 | ||
63 | public void viewVerses(HttpServletRequest req, HttpServletResponse resp) { | |
64 | ||
65 | //throwing java.lang.NullPointerException on this line! it doesn't see "utilities" as already instantiated in the constructor. | |
66 | List<Entity> verses = utilities.getEntitiesAsList(entityKind); | |
67 | ||
68 | try { | |
69 | // Set the attribute and Forward to hello.jsp | |
70 | req.setAttribute ("verses", verses); // to save your temporary calculations. | |
71 | req.getRequestDispatcher("/verses.jsp").forward(req, resp); | |
72 | } catch (Exception ex) { | |
73 | ex.printStackTrace (); | |
74 | } | |
75 | ||
76 | } | |
77 | ||
78 | }// close class | |
79 | ||
80 | //traceback | |
81 | ||
82 | java.lang.NullPointerException | |
83 | at com.versefeed.crud.CrudServlet.viewVerses(CrudServlet.java:109) | |
84 | at com.versefeed.crud.CrudServlet.doGet(CrudServlet.java:68) | |
85 | at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) | |
86 | at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) |