I have an error when declaring a class. First, I tried with:
LogApplication la = new LogApplication();
for run the LogApplication method (insertLogProcess method). This result had an error of java: null pointer exception
.
but when I tried with:
private LogApplication la;
the code is error and not showing the class process. What is the difference between these two declarations? Thanks.
My code:
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);
}
}