I'm testing an event on a JTable
of type tableMouseClicked(evt)
, but I'm having some errors returning the values in the respective JTextField and a JSpinner I've set up. Below is the Stack Trace for the given output when I click on a line to pass the quantity of type Integer
inside a JSpinner:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal value at javax.swing.SpinnerNumberModel.setValue(SpinnerNumberModel.java:443) at javax.swing.JSpinner.setValue(JSpinner.java:354) at com.exemplo.view.ViewPedidos.tableMouseClicked(ViewPedidos.java:410) at com.exemplo.view.ViewPedidos.access$100(ViewPedidos.java:22) at com.exemplo.view.ViewPedidos$2.mouseClicked(ViewPedidos.java:210) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6536) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) 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)
This error is also happening however this is when some column of the table is null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at com.exemplo.view.ViewPedidos.tableMouseClicked(ViewPedidos.java:382) at com.exemplo.view.ViewPedidos.access$100(ViewPedidos.java:22) at com.exemplo.view.ViewPedidos$2.mouseClicked(ViewPedidos.java:210) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6536) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) 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)
And here the implementation which I am trying to run, it is only taking the ID fields, Data Master, Product Name and Control Number and retrieves only these values, while the JTextFields Unit Value and Customer Code I can not even recover they are inserted in the bank:
private void tableMouseClicked(java.awt.event.MouseEvent evt) {
int index = table.getSelectedRow();
PedidoModel pm = (PedidoModel) table.getModel();
String id = pm.getValueAt(index, 0).toString();
String dataCadastro = pm.getValueAt(index, 1).toString();
String nomeProduto = pm.getValueAt(index, 2).toString();
String numControle = pm.getValueAt(index, 3).toString();
String quantidade = pm.getValueAt(index, 4).toString();
String valorUnitario = pm.getValueAt(index, 6).toString();
String codCliente = pm.getValueAt(index, 7).toString();
if(pm.getValueAt(index, 0) == null ){
tfPedido.setText("");
} else
if(pm.getValueAt(index, 1) == null){
tfDataCad.setText("");
} else
if(pm.getValueAt(index, 2) == null){
tfNomeProduto.setText("");
} else
if(pm.getValueAt(index, 3) == null){
tfNumControle.setText("");
} else
if(pm.getValueAt(index, 4) == null){
spQuantidade.setValue(0);
} else
if(pm.getValueAt(index, 6) == null){
tfValor.setText("");
} else
if(pm.getValueAt(index, 7) == null){
tfCodCliente.setText("");
} else {
tfPedido.setText(id);
tfDataCad.setText(dataCadastro);
tfNomeProduto.setText(nomeProduto);
tfNumControle.setText(numControle);
spQuantidade.setValue(quantidade);
tfValor.setText(valorUnitario);
tfCodCliente.setText(codCliente);
}
}
And below the DB:
+----+---------------------+-----------------------------------+-------------+------------+------------+---------------+------------+
| id | dataCadastro | nomeProduto | numControle | quantidade | valorTotal | valorUnitario | codCliente |
+----+---------------------+-----------------------------------+-------------+------------+------------+---------------+------------+
| 1 | 2017-04-05 00:00:00 | Lavadora 5Kg Brastemp | 14221 | 3 | 0 | 942.44 | NULL |
| 2 | 2017-04-05 00:00:00 | Computador Positivo Intel Core I5 | 41141 | 1 | 0 | 2044.44 | NULL |
| 4 | 2017-04-05 00:00:00 | Produto X | 434342 | 7 | 0 | 444.55 | 3443432 |
| 6 | 2017-04-05 00:00:00 | Mesa Tampo de Granito Bartira | 1333131 | 6 | 0 | 4444.44 | 44333 |
| 7 | 2017-04-05 00:00:00 | Produto X | 434342 | 7 | 0 | 444.55 | 3443432 |
| 8 | 2017-04-05 00:00:00 | Produto X | 434342 | 7 | 0 | 444.55 | 3443432 |
| 14 | 2017-04-06 00:00:00 | Produto A | 3244333 | 4 | 0 | 555.55 | 34334242 |
| 15 | 2017-04-06 00:00:00 | Produto C | 434343 | 2 | 0 | 444.44 | 4434334 |
+----+---------------------+-----------------------------------+-------------+------------+------------+---------------+------------+
Below the OrderModel template:
package com.exemplo.table;
import com.exemplo.conexao.ConnectionFactory;
import com.exemplo.controller.PedidoDAO;
import com.exemplo.exception.DAOException;
import com.exemplo.model.Pedido;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
/**
*
* @author Vickz
*/
public class PedidoModel extends AbstractTableModel {
private PedidoDAO pedidos;
private List<Pedido> listpedidos = new ArrayList<>();
public PedidoModel(PedidoDAO pedidos) throws DAOException {
this.pedidos = pedidos;
}
public void atualizarModel() throws DAOException {
listpedidos = pedidos.listar();
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "ID";
case 1:
return "Data de Cadastro";
case 2:
return "Nome do Produto";
case 3:
return "Num Controle";
case 4:
return "Quantidade";
case 5:
return "Valor Unitário";
case 6:
return "Cód Cliente";
default:
return "[no]";
}
}
@Override
public int getRowCount() {
return listpedidos.size();
}
@Override
public int getColumnCount() {
return 7;
}
@Override
public Object getValueAt(int line, int column) {
Pedido pedido = listpedidos.get(line);
switch (column) {
case 0:
return pedido.getId();
case 1:
return pedido.getDataCadastro();
case 2:
return pedido.getNomeProduto();
case 3:
return pedido.getNumControle();
case 4:
return pedido.getQuantidade();
case 5:
return pedido.getValorUnitario();
case 6:
return pedido.getCodCliente();
default:
return "";
}
}
I tried to insert each field by making a condition if the column of the database is null then the JTextField
receive empty text field or the JSpinner
set zero and even then the errors persist. How to fix this error?