如何在我的数据库中的jlabel上存储默认的imageicon路径

时间:2018-03-15 11:42:22

标签: java mysql netbeans nullpointerexception

我正在使用Java注册表单,该表单上有一个标签,用于显示从JFileChooser上传的图像。

我的问题是,如果用户没有点击上传图片的按钮,我想使用jlable上默认图片的绝对路径(因为这是文件追踪者来玩的唯一点)

但即使我使用getImageIcon我仍然会 -

  

空指针异常。

这可能是因为我将jfilechooser返回的路径存储在输入流变量中,然后将其存储在MySQL的blob字段中,我不知道如何解决这个问题。

我使用net-beans作为我的IDE。非常感谢您的帮助。提前谢谢。

这是代码提取

String imgPath = new ImageIcon(ApplicationClass.class.getResource(“defaultImg.png”))。toString(); imgPath = imgPath.replace(“/”,“\”);

InputStream is = new FileInputStream(new File(imgPath));

这是实施方法

private void registerBtnActionPerformed(java.awt.event.ActionEvent evt) {                                            

        try{
            String userName  = "root";
            String passWord = "";
            String url = "jdbc:mysql://localhost:3306/aofm_db";
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection) DriverManager.getConnection(url,userName,passWord);         

                InputStream is = null;
                if(imgPath.isEmpty() || imgPath ==null){
                    is = MainActivity.class.getResourceAsStream("defaultImg.png");
                }else{
                    is = new FileInputStream(new File(imgPath));
                }
                //InputStream is = new FileInputStream(new File(imgPath));
                PreparedStatement pstmt = con.prepareStatement("insert into register(fname,mname,lname,dob,phone,area,status,image) values(?,?,?,?,?,?,?,?)");
                pstmt.setString(1, fnameFld.getText());
                pstmt.setString(2, mNameFld.getText());
                pstmt.setString(3, lNameFld.getText());
                pstmt.setString(4,((JTextField)dobChooser.getDateEditor().getUiComponent()).getText());
                pstmt.setString(5, phoneFld.getText());
                pstmt.setString(6, areaFld.getText());
                pstmt.setString(7, (String) statusCombo.getSelectedItem());
                pstmt.setBlob(8, is);
                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(null, "New member successfully added", "Registration Message", 1);
                fnameFld.setText("");
                mNameFld.setText("");
                lNameFld.setText("");
                dobChooser.setDate(null);
                phoneFld.setText("");
                areaFld.setText("");
                statusCombo.setSelectedItem("");
                imageLbl.setIcon(new javax.swing.ImageIcon(getClass().getResource("defaultImg.png")));
                imgPath = null;

          }catch(Exception e){
                System.err.println(e);
                JOptionPane.showMessageDialog(null, "Registration Error. Make sure to upload an image. Contact developer if problem persists afterwards "+e, "Registration Message", 0);
          }

    }

//When user uploads an image
private void uploadBtnActionPerformed(java.awt.event.ActionEvent evt) {      
        JFileChooser filechooser = new JFileChooser();

        filechooser.setCurrentDirectory(new File(System.getProperty("user.home")));
        FileNameExtensionFilter filter = new FileNameExtensionFilter("*.IMAGE","jpg","gif","png");
        filechooser.addChoosableFileFilter(filter);
        filechooser.setDialogTitle("Choose an image");
        filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int returnval = filechooser.showOpenDialog(this);
        if(returnval==JFileChooser.APPROVE_OPTION)
        {    File file = filechooser.getSelectedFile();
             String path=file.getAbsolutePath();
             imageLbl.setIcon(ResizeImage(path));
             imgPath = path;
             System.err.println("path "+path);


        }
    }

0 个答案:

没有答案