当更改变量无效时,如何修复NullPointerException?

时间:2019-05-14 00:22:06

标签: java nullpointerexception

我正在处理一些数据库代码,并且在gui的PanelEdit类上一直遇到问题。我的selectedTasks给了我一个NullPointerExpection,除了我基本上按照教授的例子逐字逐句地学习。如何修复变量?它在该代码的最后两行开始错误,并且IntelliJ引导我回到声明了selectedTasks的顶部。

我尝试像IntelliJ建议的那样使变量成为局部变量,但这没用。

public class PanelEdit
        extends JPanel
        implements ActionListener
{
   private static Logger logger = LogManager.getLogger(PanelEdit.class);
   private Task selectedTask;

   public void fillProductComboBox()
   {
      logger.debug(" !!!!    FILL THE TASK COMBO BOX !!!");
      TaskDao dao = new TaskDao("sqliteconfig.properties");

      Task filter = new Task();
      filter.taskName = "";
      ArrayList<Object> tasks;
      try
      {
         cboTasks.removeAllItems();
         tasks = dao.getManyObjects(filter);
         for(Object obj : tasks)
         {
            cboTasks.addItem(    (Task) obj      );
         }
      }
      catch(Exception ex)
      {
         logger.error(ex.toString());
      }
   }


   @Override
   public void actionPerformed(ActionEvent e)
   {

      if (e.getActionCommand().toLowerCase().equals("delete"))
      {
         logger.debug("PANELEDIT  !!!!    DELETE  BUTTON !!!");
         selectedTask = (Task) cboTasks.getSelectedItem();

        logger.debug("YOU SELECTED: " + selectedTask.toStringFull());

         try
         {
            TaskDao dao = new TaskDao("sqliteconfig.properties");
            dao.deleteSingleObject(selectedTask);
            fillProductComboBox();
         }
         catch(Exception ex)
         {
            logger.error(ex.toString());
         }
      }


         if (e.getActionCommand().toLowerCase().equals("select"))
      {
         logger.debug("PANELEDIT  !!!!    SELECT  BUTTON !!!");

         selectedTask = (Task) cboTasks.getSelectedItem();

        logger.debug("YOU SELECTED : " + selectedTask);

         datTaskName.setInputValue(selectedTask.taskName);
         datDescription.setInputValue(selectedTask.description);
         //datDueDate.setInputValue(String.valueOf( selectedTask.dueDate));

0 个答案:

没有答案