2014-11-17 2 views
1

데이터베이스 테이블에서 값을 가져 오는 동안 NULL 포인터 예외가 계속 발생합니다 ..... 왜 그 포인터가 널 포인터 예외입니까? 잘못자바의 테이블 열에 null 값을 처리하는 방법은 무엇입니까?

private HashSet getPlayerList() { 
     HashSet hs = new HashSet(); 
     String soccername = ""; 
     try { 
      conn = ConnectionProvider.getConnection(); 
      rs = null; 
      pstmt = null; 
      String sql = "Select * from Players where deleted = false"; 

      if (conn != null) { 
       pstmt = conn.prepareStatement(sql); 
       rs = pstmt.executeQuery(); 

       while (rs.next()) { 
        soccername = rs.getString("soccername").trim();// this line giving exception 
        if (soccername == null || soccername.isEmpty()) { 
         soccername = rs.getString("name").trim(); 
        } 
        hs.add(soccername); 
       } 
      } 
     } catch (NamingException ex) { 
      System.out.println(ex); 
     } catch (SQLException ex) { 
      System.out.println(ex); 
     } finally { 
      try { 
       pstmt.close(); 
       rs.close(); 
       conn.close(); 
      } catch (SQLException ex) { 
       System.out.println(ex); 
      } 
     } 
     return hs; 
    } 
+1

가능한 중복 (HTTP [널 (null) 포인터 예외이며, 내가 그것을 어떻게 해결합니까 무엇?] : // 유래. com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Deltharis

답변

0

:

soccername = rs.getString("soccername").trim();// this line giving exception 
if (soccername == null || soccername.isEmpty()) { 

rs.getString("soccername") 경우 반환이 기능 trim() 다음에 있기 때문에 그것은 NPE에 리드를해야 널 (null)

여기 내 코드입니다.

더 나은입니다

soccername = rs.getString("soccername"); 
if (soccername == null || soccername.trim().isEmpty()) { 
+1

답변 해 주셔서 감사합니다 ....! –

0

그것은해야한다 :의

soccername = rs.getString("soccername"); 
if (soccername == null || soccername.isEmpty()) { 
    soccername = rs.getString("name").trim(); 
} else { 
    //now you are sure that soccername is not null 
    soccername = soccername.trim(); 
} 
+0

감사합니다 @blackSlash –

관련 문제