使用JdateChoosers在if语句中取消引用可能的空指针异常

时间:2016-05-19 16:32:35

标签: java nullpointerexception jdatechooser

我是编程的初学者,所以尽可能帮助我! 我正在做一个选择DataBase(MySQL)的程序,具体取决于用户选择要搜索的内容。 Programm有4种不同的搜索内容:Artigo,Cliente,Vendedor和拥有2个jDateChoosers的数据(它将在第一个日期和第二个日期之间进行查询)。 因此,如果条件能够捕捉到所有可能的组合,我将需要16个。

这里有关于一个事件的代码,jTextFieldClienteKeyReleased,考虑到代码在所有其他TextFieldsKeyReleaseds和jDateChoosersMouseClicked中重复等等。

        private void jTextFieldPorVendedorKeyReleased(java.awt.event.KeyEvent evt) {
// TODO add your handling code here
     // Se artigo, cliente, vendedor e data forem pesquisados fazer a seguinte consulta SQL

        SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial1 = sd1.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal1 = sd2.format(jDateChooserFinal.getDate());

    if(!(jTextFieldPorArtigo.getText().isEmpty()) && (!(jTextFieldPorCliente.getText().isEmpty())) && (!(jTextFieldPorVendedor.getText().isEmpty())) && (!(jDateChooserInicial.getDate().toString().isEmpty())) && (!(jDateChooserFinal.getDate().toString().isEmpty()))) {
        try {

                 PreparedStatement preparedStatement;   //1 SELECT * FROM fatura WHERE CodArt LIKE '%AES%' AND CodCli = 1 AND CodVendedor = 1 AND Data >= '2015/03/25' AND Data <= '2017/02/27'
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodArt LIKE '%"
                         + jTextFieldPorArtigo.getText()
                         + "%' AND "
                         + " CodCli = "
                         + jTextFieldPorCliente.getText()
                         + " AND "
                         + "CodVendedor = "
                         + jTextFieldPorVendedor.getText()
                         + " AND "
                         + "Data >= '"
                         + DataInicial1
                         + "' AND "
                         + " Data <= '"
                         + DataFinal1
                         + "'");
                 System.out.println(preparedStatement);                                  
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }
        //Se artigo, cliente e vendedor estiverem a ser pesquisados fazer a seguinte consulta SQL
 } 
        SimpleDateFormat sd3 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial2 = sd3.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd4 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal2 = sd4.format(jDateChooserFinal.getDate());

        if(!(jTextFieldPorArtigo.getText().isEmpty()) && (!(jTextFieldPorCliente.getText().isEmpty())) && (!(jTextFieldPorVendedor.getText().isEmpty())) && (jDateChooserInicial.getDate().toString().isEmpty()) && (jDateChooserFinal.getDate().toString().isEmpty()) || ((DataInicial2 == null )) || ((DataFinal2 == null ))) /*|| (jDateChooserInicial.getDateFormatString().contains("yyyy-MM-dd")) || (jDateChooserFinal.getDateFormatString().contains("yyyy-MM-dd")))*/ {
        try {
                 PreparedStatement preparedStatement;   //2 SELECT * FROM fatura WHERE CodArt LIKE '%39%' AND CodCli = 1 AND CodVendedor = 1
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodArt LIKE '%"
                         + jTextFieldPorArtigo.getText()
                         + "%' AND "
                         + "CodCli = "
                         + jTextFieldPorCliente.getText()
                         + " AND "
                         + "CodVendedor = "
                         + jTextFieldPorVendedor.getText()
                         + "");

                 System.out.println(preparedStatement);
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }

            // Se artigo, cliente e data estiverem a ser pesquisados fazer a seguinte consulta SQL
 } 
        SimpleDateFormat sd5 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial3 = sd5.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd6 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal3 = sd6.format(jDateChooserFinal.getDate());

            if(!(jTextFieldPorArtigo.getText().isEmpty()) && (!(jTextFieldPorCliente.getText().isEmpty())) && ((jTextFieldPorVendedor.getText().isEmpty())) && (!(jDateChooserInicial.getDate().toString().isEmpty())) && (!(jDateChooserFinal.getDate().toString().isEmpty()))) {
            try {

                 PreparedStatement preparedStatement;   //3 SELECT * FROM fatura WHERE CodArt = 2 AND CodCli = 1 AND Data >= '2015/03/25' AND Data <= '2017/02/27'
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodArt LIKE '%"
                         + jTextFieldPorArtigo.getText()
                         + "%' AND "
                         + " CodCli = "
                         + jTextFieldPorCliente.getText()
                         + " AND "
                         + " Data >= '"
                         + DataInicial3
                         + "' AND "
                         + " Data <= '"
                         + DataFinal3
                         + "'");
                 System.out.println(preparedStatement);                                             
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }

        // Se artigo e cliente estiverem a ser pesquisados fazer a seguinte consulta SQL
 } 
        SimpleDateFormat sd7 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial4 = sd7.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd8 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal4 = sd8.format(jDateChooserFinal.getDate());

            if(!(jTextFieldPorArtigo.getText().isEmpty()) && (!(jTextFieldPorCliente.getText().isEmpty())) && ((jTextFieldPorVendedor.getText().isEmpty())) && (jDateChooserInicial.getDate().toString().isEmpty()) && (jDateChooserFinal.getDate().toString().isEmpty()) || ((DataInicial4 == null )) || ((DataFinal4 == null ))) {
            try {
                 PreparedStatement preparedStatement;   //4 SELECT * FROM fatura WHERE CodArt = 1 AND CodCli = 1
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodArt LIKE '%"
                         + jTextFieldPorArtigo.getText()
                         + "%' AND "
                         + " CodCli = "
                         + jTextFieldPorCliente.getText()
                         + "");
                 System.out.println(preparedStatement);
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }

        // Se artigo, vendedor e data estiverem a ser pesquisados fazer a seguinte consulta SQL
 }          
        SimpleDateFormat sd9 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial5 = sd9.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd10 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal5 = sd10.format(jDateChooserFinal.getDate());

            if(!(jTextFieldPorArtigo.getText().isEmpty()) && ((jTextFieldPorCliente.getText().isEmpty())) && (!(jTextFieldPorVendedor.getText().isEmpty())) && (!(jDateChooserInicial.getDate().toString().isEmpty())) && (!(jDateChooserFinal.getDate().toString().isEmpty()))) {
        try {

                 PreparedStatement preparedStatement;   //5 SELECT * FROM fatura WHERE CodArt = 2 AND CodVendedor = 1 AND Data >= '2015/03/25' AND Data <= '2017/02/27'
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodArt LIKE '%"
                         + jTextFieldPorArtigo.getText()
                         + "%' AND "
                         + " CodVendedor = "
                         + jTextFieldPorVendedor.getText()
                         + " AND "
                         + " Data >= '"
                         + DataInicial5
                         + "' AND "
                         + " Data <= '"
                         + DataFinal5
                         + "'");
                 System.out.println(preparedStatement);                                  
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }

        // Se artigo e vendedor estiverem a ser pesquisados fazer a seguinte consulta SQL
 } 
        SimpleDateFormat sd11 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial6 = sd11.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd12 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal6 = sd12.format(jDateChooserFinal.getDate());

            if(!(jTextFieldPorArtigo.getText().isEmpty()) && ((jTextFieldPorCliente.getText().isEmpty())) && (!(jTextFieldPorVendedor.getText().isEmpty())) && (jDateChooserInicial.getDate().toString().isEmpty()) && (jDateChooserFinal.getDate().toString().isEmpty()) || ((DataInicial6 == null )) || ((DataFinal6 == null ))){
            try {
                 PreparedStatement preparedStatement;   //6 SELECT * FROM fatura WHERE CodArt = 1 AND CodVendedor = 2
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodArt LIKE '%"
                         + jTextFieldPorArtigo.getText()
                         + "%' AND "
                         + " CodVendedor = "
                         + jTextFieldPorVendedor.getText()
                         + "");
                 System.out.println(preparedStatement);                                  
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {     
                 System.out.println(ex);
                                      }

        // Se artigo e data estiverem a ser pesquisados fazer a seguinte consulta SQL
 }  
        SimpleDateFormat sd13 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial7 = sd13.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd14 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal7 = sd14.format(jDateChooserFinal.getDate());

        if(!(jTextFieldPorArtigo.getText().isEmpty()) && ((jTextFieldPorCliente.getText().isEmpty())) && ((jTextFieldPorVendedor.getText().isEmpty())) && (!(jDateChooserInicial.getDate().toString().isEmpty())) && (!(jDateChooserFinal.getDate().toString().isEmpty()))){
        try {

                 PreparedStatement preparedStatement;   //7 SELECT * FROM fatura WHERE CodArt = 2 AND Data >= '2015/03/25' AND Data <= '2017/02/27'
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodArt LIKE '%"
                         + jTextFieldPorArtigo.getText()
                         + "%' AND "
                         + " Data >= '"
                         + DataInicial7
                         + "' AND "
                         + " Data <= '"
                         + DataFinal7
                         + "'");
                 System.out.println(preparedStatement);                                  
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }

        // Se apenas artigo estiver a ser pesquisado fazer a seguinte consulta SQL
 } 
        SimpleDateFormat sd15 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial8 = sd15.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd16 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal8 = sd16.format(jDateChooserFinal.getDate());

        if(!(jTextFieldPorArtigo.getText().isEmpty()) && ((jTextFieldPorCliente.getText().isEmpty())) && ((jTextFieldPorVendedor.getText().isEmpty())) && (jDateChooserInicial.getDate().toString().isEmpty()) && (jDateChooserFinal.getDate().toString().isEmpty()) || ((DataInicial8 == null )) || ((DataFinal8 == null ))){
        try {
                 PreparedStatement preparedStatement;   //8 SELECT * FROM `fatura` WHERE CodArt LIKE '%AM%'
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodArt LIKE '%"
                         + jTextFieldPorArtigo.getText()
                         + "%'");
                 System.out.println(preparedStatement);                                  
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      } 

1 个答案:

答案 0 :(得分:0)

(这是代码的继续,我无法将所有代码放在同一个问题中,它有字符限制)

// Se cliente, vendedor e data estiverem a ser pesquisados fazer a seguinte consulta SQL
 } if((jTextFieldPorArtigo.getText().isEmpty()) && (!(jTextFieldPorCliente.getText().isEmpty())) && (!(jTextFieldPorVendedor.getText().isEmpty())) && (!(jDateChooserInicial.getDate().toString().isEmpty())) && (!(jDateChooserFinal.getDate().toString().isEmpty()))) {
        try {

        SimpleDateFormat sd17 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial9 = sd17.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd18 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal9 = sd18.format(jDateChooserFinal.getDate());

                 PreparedStatement preparedStatement;   //9 SELECT * FROM fatura WHERE CodCli = 1 AND CodVendedor = 2 Data >= '2015/03/25' AND Data <= '2017/02/27'
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodCli ="
                         + jTextFieldPorCliente.getText()
                         + " AND "
                         + " CodVendedor = "
                         + jTextFieldPorVendedor.getText()
                         + " AND "
                         + " Data >= '"
                         + DataInicial9
                         + "' AND "
                         + " Data <= '"
                         + DataFinal9
                         + "'");
                System.out.println(preparedStatement);
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }

        // Se cliente e vendedor estiverem a ser pesquisados fazer a seguinte consulta SQL
 } 
        SimpleDateFormat sd19 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial10 = sd19.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd20 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal10 = sd20.format(jDateChooserFinal.getDate());

        if((jTextFieldPorArtigo.getText().isEmpty()) && (!(jTextFieldPorCliente.getText().isEmpty())) && (!(jTextFieldPorVendedor.getText().isEmpty())) && (jDateChooserInicial.getDate().toString().isEmpty()) && (jDateChooserFinal.getDate().toString().isEmpty()) || ((DataInicial10 == null )) || ((DataFinal10 == null ))) {
        try {
                 PreparedStatement preparedStatement;   //10 SELECT * FROM fatura WHERE CodCli = 1 AND CodVendedor = 2
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodCli ="
                         + jTextFieldPorCliente.getText()
                         + " AND "
                         + " CodVendedor = "
                         + jTextFieldPorVendedor.getText()
                         + "");
                 System.out.println(preparedStatement);
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }

        // Se cliente e data estiverem a ser pesquisados fazer a seguinte consulta SQL
 } 
        SimpleDateFormat sd21 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial11 = sd21.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd22 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal11 = sd22.format(jDateChooserFinal.getDate());

        if((jTextFieldPorArtigo.getText().isEmpty()) && (!(jTextFieldPorCliente.getText().isEmpty())) && ((jTextFieldPorVendedor.getText().isEmpty())) && (!(jDateChooserInicial.getDate().toString().isEmpty())) && (!(jDateChooserFinal.getDate().toString().isEmpty()))) {
        try {

                 PreparedStatement preparedStatement;   //11 SELECT * FROM fatura WHERE CodCli = 1 AND Data >= '2015/03/25' AND Data <= '2017/02/27'
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodCli ="
                         + jTextFieldPorCliente.getText()
                         + " AND "
                         + " Data >= '"
                         + DataInicial11
                         + "' AND "
                         + " Data <= '"
                         + DataFinal11
                         + "'");
                 System.out.println(preparedStatement);
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }

        // Se apenas cliente estiver a ser pesquisado fazer a seguinte consulta SQL
 } 
        SimpleDateFormat sd23 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial12 = sd23.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd24 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal12 = sd24.format(jDateChooserFinal.getDate());

        if((jTextFieldPorArtigo.getText().isEmpty()) && (!(jTextFieldPorCliente.getText().isEmpty())) && ((jTextFieldPorVendedor.getText().isEmpty())) && (jDateChooserInicial.getDate().toString().isEmpty()) && (jDateChooserFinal.getDate().toString().isEmpty()) || ((DataInicial12 == null )) || ((DataFinal12 == null ))) {
        try {
                 PreparedStatement preparedStatement;   //12 SELECT * FROM fatura WHERE CodCli = 1
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodCli = "
                         + jTextFieldPorCliente.getText()
                         + "");                   
                 System.out.println(preparedStatement);
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }

        // Se vendedor e data estiverem a ser pesquisados fazer a seguinte consulta SQL
 } 
        SimpleDateFormat sd25 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial13 = sd25.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd26 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal13 = sd26.format(jDateChooserFinal.getDate());

        if((jTextFieldPorArtigo.getText().isEmpty()) && ((jTextFieldPorCliente.getText().isEmpty())) && (!(jTextFieldPorVendedor.getText().isEmpty())) && (!(jDateChooserInicial.getDate().toString().isEmpty())) && (!(jDateChooserFinal.getDate().toString().isEmpty()))) {
        try {

                 PreparedStatement preparedStatement;   //13 SELECT * FROM fatura WHERE CodVendedor = 2 AND Data >= '2015/03/25' AND Data <= '2017/02/27'
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodVendedor ="
                         + jTextFieldPorVendedor.getText()
                         + " AND "
                         + " Data >= '"
                         + DataInicial13
                         + "' AND "
                         + " Data <= '"
                         + DataFinal13
                         + "'");
                 System.out.println(preparedStatement);
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }

        // Se apenas vendedor estiver a ser pesquisado fazer a seguinte consulta SQL
 } 
        SimpleDateFormat sd27 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial14 = sd27.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd28 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal14 = sd28.format(jDateChooserFinal.getDate());

        if((jTextFieldPorArtigo.getText().isEmpty()) && ((jTextFieldPorCliente.getText().isEmpty())) && (!(jTextFieldPorVendedor.getText().isEmpty())) && (jDateChooserInicial.getDate().toString().isEmpty()) && (jDateChooserFinal.getDate().toString().isEmpty()) || ((DataInicial14 == null )) || ((DataFinal14 == null ))) {
        try {
                 PreparedStatement preparedStatement;   //14 SELECT * FROM fatura WHERE CodVendedor = 2
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE CodVendedor ="
                         + jTextFieldPorVendedor.getText()
                         + "");                    
                 System.out.println(preparedStatement);
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }

        // Se apenas data estiver a ser pesquisada fazer a seguinte consulta SQL
 }
        SimpleDateFormat sd29 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial15 = sd29.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd30 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal15 = sd30.format(jDateChooserFinal.getDate());

        if((jTextFieldPorArtigo.getText().isEmpty()) && ((jTextFieldPorCliente.getText().isEmpty())) && ((jTextFieldPorVendedor.getText().isEmpty())) && (!(jDateChooserInicial.getDate().toString().isEmpty())) && (!(jDateChooserFinal.getDate().toString().isEmpty()))) {
        try {

                 PreparedStatement preparedStatement;   //15 SELECT * FROM fatura WHERE Data >= '2015/03/25' AND Data <= '2017/02/27'
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura WHERE Data >= '"
                         + DataInicial15
                         + "' AND "
                         + " Data <= '"
                         + DataFinal15
                         + "'");
                 System.out.println(preparedStatement);
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }

  // Se nenhum campo estiver a ser pesquisado fazer a seguinte consulta SQL
 } 
        SimpleDateFormat sd31 = new SimpleDateFormat("yyyy-MM-dd");
        String DataInicial16 = sd31.format(jDateChooserInicial.getDate());

        SimpleDateFormat sd32 = new SimpleDateFormat("yyyy-MM-dd");
        String DataFinal16 = sd32.format(jDateChooserFinal.getDate());

        if((jTextFieldPorArtigo.getText().isEmpty()) && ((jTextFieldPorCliente.getText().isEmpty())) && ((jTextFieldPorVendedor.getText().isEmpty())) && (jDateChooserInicial.getDate().toString().isEmpty()) && (jDateChooserFinal.getDate().toString().isEmpty()) || ((DataInicial16 == null )) || ((DataFinal16 == null ))) {
        try {
                 PreparedStatement preparedStatement;   //16 SELECT * FROM fatura
                 preparedStatement = ligacao.prepareStatement("SELECT * FROM fatura");                     
                 System.out.println(preparedStatement);
                 ResultSet resultSetForFullTable = preparedStatement.executeQuery();
                 jTableVendas.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));

            } catch (SQLException ex) {
                 System.out.println(ex);
                                      }
 }

当用户搜索任何同时数据进展顺利的内容时,问题是当用户不需要查询数据并需要其他数据时,就会出错。

当我在jDateChooser中没有设置数据的情况下进行搜索时,错误是NullPointerException,我试图阅读很多帖子但是没有人帮助我!

这是错误:

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.util.Calendar.setTime(Calendar.java:1770)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:943)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:936)
    at java.text.DateFormat.format(DateFormat.java:345)
    at projeto_mitjavilla_ricardo.ListarVenda.jTextFieldPorArtigoKeyReleased(ListarVenda.java:2266)
    at projeto_mitjavilla_ricardo.ListarVenda.access$200(ListarVenda.java:28)
    at projeto_mitjavilla_ricardo.ListarVenda$4.keyReleased(ListarVenda.java:155)
    at java.awt.Component.processKeyEvent(Component.java:6496)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2832)
    at java.awt.Component.processEvent(Component.java:6312)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
     (...)

希望有人能够理解问题是什么并弄清楚,如果有人不明白我想做什么,请问我,我会尽力回答最好的方法。

p.s这是我第一次在论坛上发布任何内容,所以如果我在这个论坛上发帖做错了,请告诉我,我会尝试更好。