1

Keep getting this error when trying to run my project with Apache Tomcat 7(http://localhost:8080/MyProject/project/) and have no idea why it's still 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;
    }

}

1 Answer 1

3

Your private MyProjectRepository itemsRepo; has not been instantiated.

Sign up to request clarification or add additional context in comments.

1 Comment

What's doing the injecting? If this were a Spring project, I'd know all about the bean factory. What's your DI solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.