What's new

Closed Paano ma avoid ang "AWT-EventQueue-0 java.lang.NullPointerException" kapag field sa database table ay Longblob?

Status
Not open for further replies.

Mezta1988

Enthusiast
Joined
Jun 25, 2019
Posts
1
Reaction
0
Points
62
Hi,

Nais ko sanang humingi ng payo or tulong sa inyo kung paano ma iiwasan "AWT-EventQueue-0 java.lang.NullPointerException" kapag mag save ako ng record sa MySql Phpmyadmin.

Mayroon akong ginawang simple desktop application gamit ang JAVA NETBEANS, isa sa mga form na ginawa ko sa application na iyon ay employee delails na kong saan may isang button na "Add Photo" kapag pinindot and button na yan ay lalabas ang JFileChooser at doon mamimili ako ng file na e uupload ko pero dpat .png .jpg lang ang file extension. Sa kasalukuyan ay gumagana naman ung fom na yun, nkakapag insert ako ng record sa database table ko, Ang problima ko ay kapag hindi ako nag insert ng image file at pinindot ko ung save or add button para e save sa database ay nag eerror at yun nga ang "AWT-EventQueue-0 java.lang.NullPointerException".

Ang gusto ko sanang mangyari ay gawing optional ang pag aattach ng picture sa form na iyon at ma save ang file sa database table kahit null ang value.

Code for my add or save button.

Java:
    private void jButtonAddEmpActionPerformed(java.awt.event.ActionEvent evt) {                                             
      
        int InsertToDB = JOptionPane.showConfirmDialog(null, "Do you want to save this entry?", "SAVE",JOptionPane.YES_NO_OPTION);
        
        if (InsertToDB == 0)
        {   
            
            if(CheckInputs() == true)
            {
                String Gender = null;
            
                if(JRB_Male.isSelected())
                {
                    Gender = "Male";
                }
                else if(JRB_Female.isSelected())
                {
                    Gender = "Female";
                }
                
                try {
                    pst = con.prepareStatement("INSERT INTO employee_details (First_Name, Last_Name, Gender, Position_ID, Department_ID, "
                            + "Office_Location_ID, Employee_Status, Office_Number, Mobile_Number, Email_Address, Remarks, Image) "
                            + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
                    
                    pst.setString(1, jTextFieldFname.getText());
                    pst.setString(2, jTextFieldLname.getText());
                    pst.setString(3, Gender);
                    pst.setInt(4, ((PositionWrapper)Emp_Positions.getSelectedItem()).GetId());          // Stroring the FK ID to employee_details table intead of the String reference to the Position string.
                    pst.setInt(5, ((DepartmentWrapper)JCB_Department.getSelectedItem()).getDeptid());
                    pst.setInt(6, ((OfficeLocationWrapper)JCB_Location.getSelectedItem()).getLocId());
                    String EmpStatus = JCB_EmpStat.getSelectedItem().toString();
                    pst.setString(7, EmpStatus);
                    pst.setString(8, jTextFieldOfficeNumber.getText());
                    pst.setString(9, jTextFieldMobNumber.getText());
                    pst.setString(10, jTextFieldEmailAdd.getText());
                    pst.setString(11, jTextAreaRemarks.getText());                   
                    InputStream InsertImg = new FileInputStream(new File(ImagePath));
                    pst.setBlob(12, InsertImg);
                    
                    pst.executeUpdate();
                    JOptionPane.showMessageDialog(null, "Succesfully Saved");
                    
                } catch (SQLException ex) {
                    Logger.getLogger(Employee_Details.class.getName()).log(Level.SEVERE, null, ex);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(Employee_Details.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
          
            else{
            JOptionPane.showMessageDialog(null, "Check the mandatory fields");
            }
        }
        DBTable();
    }

Ito yung code ng pag import ko ng image gamit ang ImageIcon.

Java:
String ImagePath  = null;
    public ImageIcon ResizeImage(String ImagePath, byte[] pictures){
        ImageIcon Photo;
        
        if(ImagePath != null)
        {
            Photo = new ImageIcon(ImagePath);   
        }
        else
        {
            Photo = new ImageIcon(pictures);
        }
        Image pic = Photo.getImage();
        Image img = pic.getScaledInstance(jLabelPhoto.getWidth(), jLabelPhoto.getHeight(), Image.SCALE_SMOOTH);
        ImageIcon Img = new ImageIcon(img);
        return Img;
    }

Ito naman yung code ko for my Add Photo button.
Code:
private void jButtonAddPhotoActionPerformed(java.awt.event.ActionEvent evt) {                                               
        
        JFileChooser SelectImage = new JFileChooser();
        SelectImage.setCurrentDirectory(new File(System.getProperty("user.home")));
        
        FileNameExtensionFilter PicFormat = new FileNameExtensionFilter("*.image", "jpg","png");
        SelectImage.addChoosableFileFilter(PicFormat);
        int result = SelectImage.showSaveDialog(null);
        
        if(result == JFileChooser.APPROVE_OPTION)
        {
            File SelectedImage = SelectImage.getSelectedFile();
            String path = SelectedImage.getAbsolutePath();
            jLabelPhoto.setIcon(ResizeImage(path, null));
            ImagePath = path;
        }
        else
        {
            JOptionPane.showMessageDialog(null, "No file is selected");
        }
    }

Stack Trace or Error Code:


Java:
 private void jButtonAddPhotoActionPerformed(java.awt.event.ActionEvent evt) {                                               
        
       JFileChooser SelectImage = new JFileChooser();
       SelectImage.setCurrentDirectory(new File(System.getProperty("user.home")));
        
       FileNameExtensionFilter PicFormat = new FileNameExtensionFilter("*.image", "jpg","png");
       SelectImage.addChoosableFileFilter(PicFormat);
       int result = SelectImage.showSaveDialog(null);
        
       if(result == JFileChooser.APPROVE_OPTION)
       {
           File SelectedImage = SelectImage.getSelectedFile();
           String path = SelectedImage.getAbsolutePath();
           jLabelPhoto.setIcon(ResizeImage(path, null));
           ImagePath = path;
       }
       else
       {
           JOptionPane.showMessageDialog(null, "No file is selected");
            
 
       }
   }


Error Stack Trace.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.io.File.<init>(File.java:277)
    at iap_it_inventory_system.Employee_Details.jButtonAddEmpActionPerformed(Employee_Details.java:664)
    at iap_it_inventory_system.Employee_Details.access$100(Employee_Details.java:36)
    at iap_it_inventory_system.Employee_Details$3.actionPerformed(Employee_Details.java:373)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    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:4525)
    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:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    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:80)
    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)

Ito yung layout ng form ko.

635619


ito naman ung database table ko.

635620
 

Attachments

Status
Not open for further replies.
Back
Top