andrejrcarvalho Posted May 4, 2015 at 07:48 PM Report Share #582463 Posted May 4, 2015 at 07:48 PM (edited) ola pessoal sou novo nisto e estava aqui a criar um programa simples para exportar o conteodo de uma jTable para um ficheiro xls mas esta-me a surgir aqui um erro típico e irritante: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at app.funcoes.toExcel(funcoes.java:34) at app.Teste.jButton1ActionPerformed(Teste.java:103) at app.Teste.access$000(Teste.java:13) at app.Teste$1.actionPerformed(Teste.java:68) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6525) at javax.swing.JComponent.processMouseEvent(JComponent.java:3322) at java.awt.Component.processEvent(Component.java:6290) at java.awt.Container.processEvent(Container.java:2234) at java.awt.Component.dispatchEventImpl(Component.java:4881) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) at java.awt.Container.dispatchEventImpl(Container.java:2278) at java.awt.window.dispatchEventImpl(window.java:2739) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:702) at java.awt.EventQueue$3.run(EventQueue.java:696) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:724) at java.awt.EventQueue$4.run(EventQueue.java:722) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:721) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) Teste private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { funcoes funcoes = new funcoes(); File fich = new File("alo.txt"); funcoes.toExcel(tab, fich); } funcoes public class funcoes { public void toExcel(JTable table, File file){ try{ TableModel model = table.getModel(); FileWriter excel = new FileWriter(file); for(int i = 0; i < model.getColumnCount(); i++){ excel.write(model.getColumnName(i) + "\t"); } excel.write("\n"); for(int i=0; i< model.getRowCount(); i++) { for(int j=0; j < model.getColumnCount(); j++) { excel.write(model.getValueAt(i,j).toString()+"\t"); } excel.write("\n"); } excel.close(); }catch(IOException e){ JOptionPane.showMessageDialog(null, e); } } } Edited May 4, 2015 at 08:55 PM by apocsantos geshi Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted May 4, 2015 at 09:22 PM Report Share #582466 Posted May 4, 2015 at 09:22 PM qual é a linha : at app.funcoes.toExcel(funcoes.java:34) IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
andrejrcarvalho Posted May 5, 2015 at 05:55 PM Author Report Share #582514 Posted May 5, 2015 at 05:55 PM excel.write(model.getValueAt(i,j).toString()+"\t"); Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted May 5, 2015 at 06:35 PM Report Share #582519 Posted May 5, 2015 at 06:35 PM de duas uma - a variável model tem o valor de null - o retorno da função excel.write retorna o valor de null agora testa IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
andrejrcarvalho Posted May 5, 2015 at 07:26 PM Author Report Share #582523 Posted May 5, 2015 at 07:26 PM Entao e como e que eu posso testar isso? Link to comment Share on other sites More sharing options...
siul72 Posted May 15, 2015 at 06:20 PM Report Share #583242 Posted May 15, 2015 at 06:20 PM if(bllaaa == null) ..... Link to comment Share on other sites More sharing options...
KTachyon Posted May 15, 2015 at 09:23 PM Report Share #583256 Posted May 15, 2015 at 09:23 PM O retorno do excel.write pode ser null, não iria causar um NullPointerException. Tendo em conta o ponto do código em que isso acontece: - Já foram executadas funções da variável excel, não pode ser null; - Já foram executadas funções da variável model, não pode ser null; - Se model.getValueAt(i,j).toString() fosse null, não haveria um NullPointerException; Portanto, o teu null é o model.getValueAt(i,j). “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now