下午好!
当我在另一个搜索屏幕中选择所需记录后尝试将数据加载到注册屏幕中时,我遇到了 NullPointerException
问题。
我的代码:
搜索屏幕:
public int retornaSelecao() {
return (int) tablePesquisa.getValueAt(tablePesquisa.getSelectedRow(), 0);
}
private void tablePesquisaMouseClicked(java.awt.event.MouseEvent evt){
if (evt.getClickCount() == 2) {
CadastroCliente cliente = new CadastroCliente(this.retornaSelecao());
this.dispose();
}
}
这个屏幕我获取了JTable
中记录的id,并通过id参数调用屏幕寄存器
现在是注册屏幕
public CadastroCliente() {
initComponents();
this.desabilitaCampos();
btnAlterar.setEnabled(false);
btnExcluir.setEnabled(false);
btnCancelar.setEnabled(false);
btnSalvar.setEnabled(false);
btnNovo.setEnabled(true);
btnPesquisa.setEnabled(true);
btnSair.setEnabled(true);
}
public CadastroCliente(int codigo){
String sql = "SELECT * FROM CLIENTE WHERE CODIGO = " + codigo;
Conexao conexao = new Conexao();
ResultSet rst;
//System.out.println("" + codigo);
try {
pstm = conexao.conectar().prepareStatement("SELECT * FROM CLIENTE WHERE CODIGO = '" + codigo + "';");
rst = pstm.executeQuery();
while (rst.next()) {
txtCodigo.setText("" + rst.getString("codigo"));
System.out.println("" + rst.getString("codigo"));
txtDataCadastro.setText(rst.getDate("datacadastro").toString());
txtDataNascimento.setDate(rst.getDate("datanascimento"));
txtNome.setText(rst.getString("nome").toUpperCase());
txtApelido.setText(rst.getString("apelido").toUpperCase());
txtEndereco.setText(rst.getString("endereco").toUpperCase());
txtNumero.setText(rst.getString("numero"));
txtComplemento.setText(rst.getString("complemento").toUpperCase());
txtBairro.setText(rst.getString("bairro").toUpperCase());
txtCep.setText(rst.getString("cep"));
txtNomeCidade.setText(rst.getString("cidade").toUpperCase());
txtRg.setText(rst.getString("rg"));
txtCpf.setText(rst.getString("cpf"));
cbPagamento.setSelectedItem(rst.getInt("diapagamento"));
txtMensalidade.setText("" + rst.getFloat("mensalidade"));
txtTelefoneResidencial.setText(rst.getString("telres"));
txtTelefoneComercial.setText(rst.getString("telcom"));
txtTelefoneCelular.setText(rst.getString("celular"));
txtInformacoes.setText(rst.getString("informacoes"));
}
} catch (SQLException ex) {
Logger.getLogger(CadastroCliente.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new CadastroCliente().setVisible(true);
}
});
}
我创建了两个构造函数,在第一个构造函数完成后,在 JTextField
处给出了更多的 NullPointerException
。为什么会出现这个错误?谁能帮我?谢谢
最佳答案
发布堆栈跟踪和发生错误的行会有所帮助。
但是通过查看 2 个构造函数:
public CadastroCliente() {
initComponents();
this.desabilitaCampos();
btnAlterar.setEnabled(false);
btnExcluir.setEnabled(false);
btnCancelar.setEnabled(false);
btnSalvar.setEnabled(false);
btnNovo.setEnabled(true);
btnPesquisa.setEnabled(true);
btnSair.setEnabled(true);
}
public CadastroCliente(int codigo){
String sql = "SELECT * FROM CLIENTE WHERE CODIGO = " + codigo;
Conexao conexao = new Conexao();
ResultSet rst;
...
}
您不会在 CadastroCliente(int codigo)
构造函数中调用诸如 initComponents()
之类的方法,这些方法很可能会初始化您的 GUI(以及大多数 JTextField
等)。
应该是:
public CadastroCliente(int codigo){
initComponents();//initialize components
//if the buttons need to be set do that here
String sql = "SELECT * FROM CLIENTE WHERE CODIGO = " + codigo;
Conexao conexao = new Conexao();
ResultSet rst;
...
}
更新:
此外,您也不希望在 GUI 事件调度线程上执行长时间运行的任务,例如数据库查询,因为这可能会导致 UI卡住,并且仅在任务完成后才变为 Activity 状态,因此不会产生任何中间结果显示:
ResultSet rst;
//System.out.println("" + codigo);
try {
pstm = conexao.conectar().prepareStatement("SELECT * FROM CLIENTE WHERE CODIGO = '" + codigo + "';");
rst = pstm.executeQuery();
while (rst.next()) {
txtCodigo.setText("" + rst.getString("codigo"));//wont show until the while loop is done
...
}
} catch (SQLException ex) {
Logger.getLogger(CadastroCliente.class.getName()).log(Level.SEVERE, null, ex);
}
最好也将这项工作卸载到 Swing Worker并使用其 publish(..)
获得中间结果的方法。
SwingWorker provides a number of communication and control features:
The SwingWorker subclass can define a method, done, which is automatically invoked on the event dispatch thread when the background task is finished.
SwingWorker implements java.util.concurrent.Future. This interface allows the background task to provide a return value to the other thread. Other methods in this interface allow cancellation of the background task and discovering whether the background task has finished or been cancelled.
The background task can provide intermediate results by invoking SwingWorker.publish, causing SwingWorker.process to be invoked from the event dispatch thread.
The background task can define bound properties. Changes to these properties trigger events, causing event-handling methods to be invoked on the event dispatch thread.
另请阅读 Concurrency in Swing .
关于java - NullPointerException 通过搜索另一个屏幕来填充 JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14913978/