title: using jfilechooser to save photo pathway and to load a photo
body: <p>I am using mySQL to store the directory of the photo that I select using jFileChooser. Once the file has been selected, I save it to the database. But when I retrieve the data and use the ImageIcon to set a label to the photo of the directory that i saved, it shows a nullpointer error.</p>
  
  <p>i am opening netbeans in administrator and since the user is selecting the file, the file must exist i.e cant point to a null.</p>
  
  <p>here is the code of the jFileChooser :</p>
  
  <pre><code>    JFileChooser file = new JFileChooser();
        file.setCurrentDirectory(new File(System.getProperty("user.home")));
        FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg","gif","png");
        file.addChoosableFileFilter(filter);
        int result = file.showSaveDialog(null);
        if(result == JFileChooser.APPROVE_OPTION){
            File selectedFile = file.getSelectedFile();
            String path = selectedFile.getAbsolutePath();
            GlobalDataStore.imagepath=path;//this stores pathway so that i can save it to the database
            photofield.setIcon(ResizeImage(path));
        }
  </code></pre>
  
  <p>and code that saves it to the database</p>
  
  <pre><code>    String imagepath = GlobalDataStore.imagepath;// retrieves image directory
      {
          Class.forName("java.sql.DriverManager");
          Connection con = (Connection)DriverManager.getConnection(conndata,username,password);
  
          Statement stmt = (Statement)con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
          String query ="SELECT name,admn_no,class,section,house,gender,sloganstore,picturepath FROM candidates WHERE post=\""+GlobalDataStore.selectedpost+"\"";
          ResultSet rs = stmt.executeQuery(query);
  
          int rowcount = jTable1.getSelectedRow();
          for(;rs.getRow()&lt;=rowcount;rs.next()){}
  
          rs.updateString("picturepath",imagepath);//saves it to the database
  
          rs.updateRow();
          JOptionPane.showMessageDialog(this, "data was successfully updated");
          con.close();
          deselect();
  
          DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
          int row = model.getRowCount();
          for(int i=0;i&lt;row;i++){
          model.removeRow(0);
      }
      }
  </code></pre>
  
  <p>and code to set the label to the image :</p>
  
  <pre><code>            String path=rs.getString("picturepath");
              con.close();
              System.out.println(path);
              ImageIcon photo=null;
              photo = new ImageIcon(getClass().getResource(path));
              photofield.setIcon(photo);
  </code></pre>