关于对象工厂的错误 [英] Error regarding object factory

查看:52
本文介绍了关于对象工厂的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static EmployeeResponse searchByEmployeeid(String key)
            throws SQLException, ClassNotFoundException {
        if (null != key) {
            System.out.println("Key in retrieval" + key);
            System.out.println("SearchRetrieval Dao");

            EmployeeResponse response = objectFactory.createEmployeeResponse();

            List<HashMap<String, String>> list = Searchdao
                    .selectDbEmployeeId(key);
            System.out.println("coming back to retreival");

            List<EmployeeResponseElement> empResponses = response
                    .getEmployeeResponseElement();

            System.out.println("coming back to retreival222");
            if (null != list) {
                EmployeeResponseElement retrievedResponse = null;
                HashMap<String, String> hashMap = null;
                System.out.println("coming back to retreival333");
                Iterator<HashMap<String, String>> itr = list.iterator();

                while (itr.hasNext()) {
                    hashMap = (HashMap<String, String>) itr.next();
                    System.out.println("coming back to retreival444");
                    retrievedResponse = objectFactory
                            .createEmployeeResponseEmployeeResponseElement();
                    System.out.println("coming back to retreival555");
                    retrievedResponse.setEmployeeID(hashMap.get("employeeID"));
                    retrievedResponse.setFirstName(hashMap.get("firstName"));
                    System.out.println(hashMap.get("firstName"));
                    retrievedResponse.setLastName(hashMap.get("lastName"));
                    retrievedResponse.setFatherName(hashMap.get("fatherName"));
                    retrievedResponse.setDOB(hashMap.get("DOB"));
                    retrievedResponse.setAddress(hashMap.get("Address"));

                    empResponses.add(retrievedResponse);

                }

            }

            return response;
        }
        

    }


EmployeeResponse响应= objectFactory.createEmployeeResponse();

System.out.println(检索中的键" +键);
System.out.println("SearchRetrieval Dao");
编译器打印这些sysout,但之后不会继续

编译器不执行此操作,我不知道那是什么错误.
请帮帮我.


EmployeeResponse response = objectFactory.createEmployeeResponse();

System.out.println("Key in retrieval" + key);
System.out.println("SearchRetrieval Dao");
Compiler printing these sysout but after that its not going

Compiler not going after this,i dont know what''s the error in that.
Please help me out.

推荐答案

您使用的是Eclipse还是Netbeans?如果没有,请这样做.
调试您的代码.在该行上设置一个断点,让应用程序向其运行.
当到达该行时,程序将停止.您的IDE会切换到调试"模式,您将有机会检查发生了什么.然后,您可以继续前进(F6),输入功能(F5),然后让程序在(F8)上进一步运行.

我想在这里
EmployeeResponse response = objectFactory.createEmployeeResponse();
稍后将填充无效的内容.
您为什么仍然要激励那个人呢?设置
EmployeeResponse response = null
然后填充它.接收方法应该能够处理object == null;
Do you use Eclipse or Netbeans? if not please do so.
Debug your code. Set a breakpoint on that line, let the application run towards it.
When it reaches the line, the program will stop. Your IDE switches into Debug mode and you will get the opportunity to check what happens. You can then move on(F6), enter functions(F5) and let the program run further on (F8).

I guess here that the
EmployeeResponse response = objectFactory.createEmployeeResponse();
is filled later with invalid stuff.
Why are you initalizing that one anyway? set it
EmployeeResponse response = null
and fill it afterwards. The receiving method should be able to handle a object == null;


我四肢外出并建议objectFactory.createEmployeeResponse()可以返回空值.如果是这种情况,则必须对其进行处理.

顺便说一句,尝试按合同围绕设计编写代码.每个方法都定义了它可以接受的[前提条件]以及如何响应[后置条件].
如果不满足前提条件,则该方法应引发异常.例如,db必须是可用的,这可能是一个前提条件,因此在关闭时抛出异常. ID的有效性不是.如果ID无效,则方法searchByEmployeeid(String)将返回null,并且仅当ID具有指定格式时,才能将如何处理空字符串或空字符串视为前提条件.
I''m going out on a limb and suggesting that objectFactory.createEmployeeResponse() can return a null. If that is the case then, you must deal with it.

As an aside, try to write your code around design by contract. Each method defines what it can accept [precondition] and how it will respond [postcondition].
If the precondition is not met, then the method should raise an exception. For example, the db must be available may be a precondition, so throw an exception when it''s down. The validity of the ID is not. If the ID is invalid then the method searchByEmployeeid(String) will return null, how you deal with a empty or null String can be considered to be a precondition if and only if the ID has a specified format.


这篇关于关于对象工厂的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆