尝试使用 Apache Tomcat 7(http://localhost:8080/MyProject/project/) 运行我的项目时不断收到此错误,并且不知道为什么它仍然是 NullPointer:
java.lang.NullPointerException com.myproject.doGet(MyProjectListServlet.java:36) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
MyProjectListServlet.java:
package com.myproject;
import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/project/")
public class MyProjectListServlet extends HttpServlet {
@Inject
private MyProjectRepository itemsRepo;
public MyProjectListServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setAttribute("items", itemsRepo.listItems());
getServletContext().getRequestDispatcher("/WEB-INF/pages/item-list.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
MyProjectRepositoryImpl.java
package com.myproject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class MyProjectRepositoryImpl implements MyProjectRepository {
private int count;
private Map<String, MyItem> idToMyItemMap = new HashMap<String, MyItem>();
public MyProjectRepositoryImpl(){
synchronized(this){
items(item("aaa", "bbb", "111", "222", "zzz"),
item("ccc", "ddd", "333", "444", "xxx"));
}
}
private MyItem item(String str1, String str2, String str3, String str4, String str5){
return new MyItem("" + (count++), str1, str2, str3, str4, str5);
}
private void items(MyItem... items){
for(MyItem item : items){
doAddMyItem(item);
}
}
private void doAddMyItem(MyItem item){
synchronized(this){
this.idToMyItemMap.put(item.getId(), item);
}
}
@Override
public MyItem lookupMyItemById(String id) {
// TODO Auto-generated method stub
synchronized(this){
return this.idToMyItemMap.get(id).cloneMe();
}
}
@Override
public void addMyItem(String str1, String str2, String str3, String str4, String str5) {
// TODO Auto-generated method stub
doAddMyItem(item(str1, str2, str3, str4, str5));
}
@Override
public void updateMyITtem(String id, String str1, String str2, String str3, String str4, String str5) {
// TODO Auto-generated method stub
MyItem item = item(str1, str2, str3, str4, str5);
synchronized(this){
item.setId(id);
this.idToMyItemMap.put(id, item);
}
}
private List<MyItem> doListMyItems(){
List<MyItem> items;
synchronized(this){
items = new ArrayList<MyItem>(this.idToMyItemMap.size());
for(MyItem item : this.idToMyItemMap.values()){
items.add(item.cloneMe());
}
}
return items;
}
@Override
public void removeMyItem(String id) {
// TODO Auto-generated method stub
synchronized(this){
this.idToMyItemMap.remove(id);
}
}
@Override
public List<MyItem> listItems() {
// TODO Auto-generated method stub
List<MyItem> items = doListMyItems();
Collections.sort(items, new Comparator<MyItem>(){
public int compare(MyItem itemA, MyItem itemB){
return itemA.getId().compareTo(itemB.getId());
}
});
return items;
}
}