2013-04-16 53 views
0

我遇到了傳遞jtree節點值的問題。我將節點值轉換爲字符串,然後我將字符串傳遞給另一個方法,該方法從數據庫返回字符串以顯示在文本窗格中,但它是錯誤的,輸出消息是「java.lang.nullpointerexception」 任何人都可以幫助我嗎?
這裏是我的代碼無法傳遞jtree值節點

public class Selector implements TreeSelectionListener { 

@Override 
public void valueChanged(TreeSelectionEvent event) { 
    // TODO Auto-generated method stub 
    Object obj = event.getNewLeadSelectionPath().getLastPathComponent(); 
    String node = obj.toString(); 

    try{ 
    resultt = treelistener.SelectionListener(node); 


    txtpane.setText(resultt); 
    } 
    catch (NullPointerException ne){ 
     System.out.print(ne); 
    } 
} 
} 

,這是treelistener代碼

  public class TreeListener { 

DbConnection connection; 
Statement st = null; 
ResultSet rs = null; 
String result = null; 


public String SelectionListener(String node) { 
    // TODO Auto-generated method stub 
    Connection con = connection.openConnection(); 
    System.out.print(node); 

    String sql = "Select penjelasan from tArtikel where judul = '%" + node +"%'" ; 
    try{ 


     st = con.createStatement(); 
     rs = st.executeQuery(sql); 
      while (rs.next()) { 
       result = rs.getString(1); 
       } 
     rs.close(); 
     st.close(); 
     con.close(); 
     System.out.print(result); 
    } 
    catch (Exception e){ 
     System.out.println(e); 
    } 

    return result; 
} 




    } 

謝謝

+0

無關:請學習java命名約定並堅持使用它們。 – kleopatra

回答

0

你從類TreeListener初始化對象?請在valueChanged對象中創建如下代碼:

TreeListener treelistener=new TreeListener(); 

請再次告訴我們添加代碼後的情況。

+0

我已經添加了TreeListener treelistener = new TreeListener(); ,但它仍然是錯誤的 – user2285571