2015-02-06 83 views
0

所以我從數據庫中提取信息並將它們放入我的GUI中的字段/框中。JTextArea沒有設置,拋出錯誤

houseText和addressText都是文本字段,它們設置正常,但oInfo是JTextArea,當我嘗試設置它時會導致錯誤。我也嘗試使用'追加',但它沒有改變任何東西。任何解決方案

infoHolder = dBStatement.executeQuery("SELECT * FROM House"); 
infoHolder.next(); 

hName = infoHolder.getString("Name"); 
hAddress = infoHolder.getString("Address"); 
numRooms = infoHolder.getInt("numRooms"); 
Type = infoHolder.getString("Type"); 
charge = infoHolder.getString("roomCharge"); 
Access = infoHolder.getString("dAccess"); 
Info = infoHolder.getString("oInfo"); 

// the below print prints all the details to prove that the database has been read correctly. 
System.out.println(hName + " " + hAddress + " " + numRooms + " " + Type + " " + charge + " " + Access + " " + Info); 

houseText.setText(" " + hName); 
addressText.setText(" " + hAddress); 
//roomNumber.setSelectedItem(numRooms); 
//roomType.setSelectedItem(" " + Type); 
//chargeRoom.setSelectedItem(" " + charge); 
//access.setSelectedItem(" " + Access); 
oInfo.setText(Info); 

堆棧跟蹤:爲了完整起見,和其他一些次要的信息

java.lang.NullPointerException at FinalBAndB.initialiseDatabase(FinalBAndB.java:124) 
    at FinalBAndB$1.run(FinalBAndB.java:274) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$400(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 
+0

你得到了什麼錯誤,你從哪裏調用這段代碼?它是一個事件處理程序,一些其他線程? – npinti 2015-02-06 13:30:11

+0

@npinti我打電話從我的公共靜態無效的主要代碼。上面的代碼在它自己的方法裏面,名爲initialiseDatabase(); textfields設置完美,但textarea(oinfo)是問題。我在打印堆棧跟蹤的方法上有一個異常處理程序,但它沒有任何啓發性。 infoHolder是一個結果集。 – user3822332 2015-02-06 13:34:16

+0

什麼不啓發喬可能會啓發傑克。堆棧跟蹤不是FYEO。 – laune 2015-02-06 13:53:21

回答

0

添加答案。

堆棧跟蹤:

java.lang.NullPointerException at FinalBAndB.initialiseDatabase(FinalBAndB.java:124) 
    at FinalBAndB$1.run(FinalBAndB.java:274) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$400(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

表明您試圖訪問一個非初始化的對象。事實證明情況就是如此。

附加信息: 與許多其他語言一樣,Java使用特定線程來執行其所有UI操作。在Java中,這被稱爲Event Dispatcher ThreadEDT)。由於您正在更新UI組件的文本,因此您需要確保操作通過EDT的操作環境進行。要做到這一點,通常會回到使用SwingUtilities.invokeLater(Runnable runnable)。請確保您遵循此模式以避免任何跨線程操作的潛在問題。