SHOW:
|
|
- or go back to the newest paste.
| 1 | Getting on the line java.lang.NullPointerException error on code line Query q = pm.newQuery(Employee.class); | |
| 2 | ||
| 3 | public String getEmployee() {
| |
| 4 | ||
| 5 | String returnRes = null; | |
| 6 | ||
| 7 | Query q = pm.newQuery(Employee.class); | |
| 8 | q.setFilter("lang == langParam");
| |
| 9 | q.setOrdering("name");
| |
| 10 | q.declareParameters("String langParam");
| |
| 11 | ||
| 12 | try {
| |
| 13 | List <Employee> results = (List<Employee>) q.execute("java");
| |
| 14 | if (! results.isEmpty()) {
| |
| 15 | for (Employee employee : results) {
| |
| 16 | returnRes = employee.getLang(); | |
| 17 | ||
| 18 | } | |
| 19 | } else {
| |
| 20 | returnRes = "nothing to return"; | |
| 21 | } | |
| 22 | return returnRes; | |
| 23 | ||
| 24 | } finally {
| |
| 25 | // TODO: handle exception | |
| 26 | q.closeAll(); | |
| 27 | pm.close(); | |
| 28 | } | |
| 29 | ||
| 30 | } | |
| 31 | ||
| 32 | ||
| 33 | //Employee.java | |
| 34 | ||
| 35 | package com.versefeed.setup; | |
| 36 | ||
| 37 | import javax.jdo.annotations.IdGeneratorStrategy; | |
| 38 | import javax.jdo.annotations.NotPersistent; | |
| 39 | import javax.jdo.annotations.PersistenceCapable; | |
| 40 | import javax.jdo.annotations.Persistent; | |
| 41 | import javax.jdo.annotations.PrimaryKey; | |
| 42 | ||
| 43 | import com.google.appengine.api.datastore.Key; | |
| 44 | ||
| 45 | @PersistenceCapable | |
| 46 | public class Employee {
| |
| 47 | ||
| 48 | @PrimaryKey | |
| 49 | @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) | |
| 50 | private Key key; | |
| 51 | ||
| 52 | @Persistent private String name = "david"; | |
| 53 | @Persistent private String lang = "java"; | |
| 54 | @NotPersistent private int age = 26; | |
| 55 | ||
| 56 | //@Persistent private ContactInfo contactInfo; | |
| 57 | ||
| 58 | public Employee(String name, String lang) {
| |
| 59 | this.name = name; | |
| 60 | this.lang = lang; | |
| 61 | } | |
| 62 | ||
| 63 | ||
| 64 | public String getName() {
| |
| 65 | return name; | |
| 66 | } | |
| 67 | ||
| 68 | public void setName(String name) {
| |
| 69 | this.name = name; | |
| 70 | } | |
| 71 | ||
| 72 | public String getLang() {
| |
| 73 | return lang; | |
| 74 | } | |
| 75 | ||
| 76 | public void setLang(String lang) {
| |
| 77 | this.lang = lang; | |
| 78 | } | |
| 79 | ||
| 80 | public int getAge() {
| |
| 81 | return age; | |
| 82 | } | |
| 83 | ||
| 84 | public void setAge(int age) {
| |
| 85 | this.age = age; | |
| 86 | } | |
| 87 | ||
| 88 | public Key getKey() {
| |
| 89 | return key; | |
| 90 | } | |
| 91 | ||
| 92 | ||
| 93 | ||
| 94 | } | |
| 95 | ||
| 96 | //stacktrace | |
| 97 | WARNING: /persistpojo | |
| 98 | java.lang.NullPointerException | |
| 99 | at com.versefeed.setup.PersistPojo.getEmployee(PersistPojo.java:45) | |
| 100 | at com.versefeed.setup.PersistPojo.doGet(PersistPojo.java:78) | |
| 101 | at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) | |
| 102 | at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) |