本帖最后由 TPOF314 于 2012-08-16 08:21:11 编辑

解决方案 »

  1.   

    java.lang.NullPointerException
    realEstate.DataSource.searchBySuburb(DataSource.java:71)
    realEstate.Search.search(Search.java:55)
    org.apache.jsp.searchResult_jsp._jspService(searchResult_jsp.java:112)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    这些是错误提示~~
    请各位高手过目。
      

  2.   

    DataSource.java:71
    这个类的 71行出现空指针,你可以debug看下是什么参数为空
      

  3.   

    db为空吧,你打印下db看看有值没有?
      

  4.   

    我查了一下db和my_search,都不是空的.....
      

  5.   

    71行出现空指针
    这个问题对初学者蛮常见的
    尤其是想在用同一个jsp文件同时发送和接收数据的时候有几个地方楼主可以注意一下
    1 在servlet里有没有设置request.setAttribute
    2 javaBean文件有没有设置初始值(单纯的提取数据可能没事,但是如果牵扯到逻辑业务没有初始值很容易空指针
    3 如果楼主在javaBean文件里设置了有一个或多个参数的构造函数,注意在调用的时候哪怕是写一个没有内容的构造函数也一定要补上,否则也容易出现错误希望对你有所帮助
    以上
      

  6.   

    这是DataSource类
    ...前面import部分省略...
    /**
     * @author TPOF314
     */
    public class DataSource {    private Document domTree;
        private ArrayList<Property> properties;    public DataSource() throws ParseException {
            try {
                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = builderFactory.newDocumentBuilder();
                FileInputStream fStream = new FileInputStream("/home/wesley/rent.xml");            domTree = (Document) builder.parse(fStream);
                properties = new ArrayList<Property>();
                
                // Fill data
                Node root = domTree.getFirstChild();
                NodeList children = root.getChildNodes();
                for (int i = 0; i < children.getLength(); i++) {
                    Node propNode = children.item(i);
                    properties.add(DataSource.getProperty(propNode));
                }        } catch (FileNotFoundException ex) {
                Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SAXException ex) {
                Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        private boolean checkSuburb(Property prop, String suburbs[]) {
            for (int i=0; i<suburbs.length; i++) {
                if (prop.getSuburb().equals(suburbs[i])) {
                    return true;
                }
            }
            return false;
        }
        
        public ArrayList<Property> searchBySuburb(String suburb) {
            System.out.println("Suburb: " + suburb);
            if (suburb.equals("Any") || suburb.equals("")) {
                return (ArrayList)properties.clone();
            }
            else {
                ArrayList<Property> resultSet = new ArrayList<Property>();
                String suburbs[] = suburb.split("; ");
                for (int i=0; i<properties.size(); i++) {
                    if (checkSuburb(properties.get(i), suburbs)) {
                        resultSet.add(properties.get(i));
                    }
                }
                return resultSet;
            }
        }    public void filterByType(ArrayList<Property> propList, String type) {
            if (!type.equals("Any")) {
                int len = propList.size();
                for (int i=len-1; i>=0; i--) {
                    Property prop = propList.get(i);
                    if (!prop.getType().equals(type)) {
                        propList.remove(i);
                    }
                }
            }
        }
        
        public void filterByBedroom(ArrayList<Property> propList, int bedroom) {
            ....中间代码和前面类似,省略....
        }
        
        public void filterByRent(ArrayList<Property> propList, int rent) {
            ....中间代码和前面类似,省略....
        }
        
        public void filterByDate(ArrayList<Property> propList) {
            ....中间代码和前面类似,省略....
        }
        
        public Document getDomTree() {
            return this.domTree;
        }    public ArrayList<Property> getProperty() {
            return (ArrayList)this.properties.clone();
        }    private static Property getProperty(Node node) throws ParseException {
            ....中间代码省略...;
        }
    }
      

  7.   


    谢谢了,第2点和第3点我都注意到了,也检查过了。其中第3点应该不是什么大问题,因为我的全部类都只有一个构造函数。
    第二点,我已经在构造函数里面把初始值设过了,应该也不是什么大问题。
    第一点有点不太明白。能帮我具体讲一下request.setAttribute的用处吗?是不是一开始要加这么两句:<% request.setAttribute("db", db); %>
    <% request.setAttribute("my_search", my_search); %>我试了一下,貌似没什么用~~
      

  8.   

                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = builderFactory.newDocumentBuilder();
                FileInputStream fStream = new FileInputStream("/home/wesley/rent.xml");还是用这样的吧Configuration().configure().buildSessionFactory();
    新的貌似不稳定,有问题
      

  9.   

    我自己找到原因了,是DataSource对象里面有一个String变量的值没有传进去,不过还是要谢谢各位帮忙!