声明java类的不同方法

时间:2018-02-06 04:37:17

标签: java

声明课程时出错。首先,我尝试了:

LogApplication la = new LogApplication(); 

用于运行LogApplication方法(insertLogProcess方法)。此结果的错误为java: null pointer exception

但是当我尝试使用时:

private LogApplication la; 

代码错误,没有显示类进程。这两个声明有什么区别?感谢。

我的代码:

public class MsExchangeProcess {

    private MsExchangeProcessor msExchangeProcessor;
    // private LogApplication la; 

    LogApplication la = new LogApplication();

    public Map<String, Object> retrieveEmailIsNotRead(String requestRetrieveListEmailTO) {
        Map<String, Object> response = null;        

        try {
            RequestEmailTo requestEmailTo = (RequestEmailTo) JsonUtil.toObject(requestRetrieveListEmailTO, RequestEmailTo.class);
            RequestEmailTo requestEmailTo2 = (RequestEmailTo) JsonUtil.toObject(requestRetrieveListEmailTO, RequestEmailTo.class);

            Map<String, Object> filter =  requestEmailTo.getFilter();
            if(filter.get("isRead") instanceof java.lang.String){
                filter.put("isRead", false);   
                requestEmailTo.setFilter(filter);
            }

            la.insertLogProcess();

            response = msExchangeProcessor.getEmailRequest(requestEmailTo);
            response = msExchangeProcessor.getEmailRequest(requestEmailTo2);

            return response;

        } catch (Exception e) { 
            System.out.println("catch error retrive email : " + e.getMessage());
            response = new HashMap<String, Object>();
            response.put(ApplicationConstanta.MsExchange.RESPONSE_CODE, ResponseCodeConstanta.EXCEPTION);

            return response;

        } finally {
            response = null;
        }
    }


    public MsExchangeProcessor getMsExchangeProcessor() {
        return msExchangeProcessor;
    }

    public void setMsExchangeProcessor(MsExchangeProcessor msExchangeProcessor) {
        this.msExchangeProcessor = msExchangeProcessor;
    }

    public LogApplication getLa() {
        return la;
    }

    public void setLa(LogApplication la) {
        this.la = la;
    }
}

LogApplication:

public class LogApplication extends BaseDao {

    private MySpringBootRouterConfig config;

    public void insertLogProcess(){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String msg = "ini message dengan id 10";
        String query = "INSERT INTO pru_util.testing_log (id, message) VALUES ('"+10+"','"+msg+"')";

        try {
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement(query);
            pstmt.executeUpdate();
        } catch (Exception ex) {
            System.out.println("error insert-> cause-> "+ex);
        } finally {
            dataSource.close(conn, pstmt, rs);
        }
    }

    public MySpringBootRouterConfig getConfig() {
        return config;
    }

    public void setConfig(MySpringBootRouterConfig config) {
        this.config = config;
    }
}

BaseDao:

public class BaseDao {

    protected DataSourceConnector dataSource;


    public DataSourceConnector getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSourceConnector dataSource) {
        this.dataSource = dataSource;
    }

    public static void destroy(Connection conn){
         try
            {
                if (conn != null)
                {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
    }
}

DataSourceConnector:

public class DataSourceConnector extends BasicDataSource
{
    @Override
    public void setPassword(final String pwd) {
        super.setPassword(EncrptionUtility.decrypt(pwd));
    }

    public static void close(Connection conn)
    {

        try
        {
            if (conn != null)
            {
                conn.close();
                conn = null;
            }
        } catch (SQLException e)
        {}
    }

    public static void close(Statement pstmt)
    {

        try
        {
            if (pstmt != null)
            {
                pstmt.close();
                pstmt = null;
            }
        } catch (SQLException e)
        {}
    }

    public static void close(ResultSet rs)
    {

        try
        {
            if (rs != null)
            {
                rs.close();
                rs = null;
            }
        } catch (SQLException e)
        {}
    }

    public static void close(Connection conn, Statement pstmt, ResultSet rs)
    {
        close(rs);
        close(pstmt);
        close(conn);
    }
}

1 个答案:

答案 0 :(得分:0)

您正在尝试使用变量而不初始化它。在LogApplicationprivate MySpringBootRouterConfig config;中永远不会初始化。

私有变量和公共变量之间的差异是变量的范围。除非创建MsExchangeProcess方法,否则只能在getter类中访问第二个变量。第一个变量可以由MsExchangeProcess类以及具有MsExchangeProcess对象的其他类访问。​​

相关问题