Jump to content

java.lang.NullPointerException


andrejrcarvalho

Recommended Posts

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 by apocsantos
geshi
Link to comment
Share on other sites

  • 2 weeks later...

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.