2012-11-08 6 views
1

이미지를 데이터베이스에 저장할 때 큰 문제가 있습니다. Jlabel에 아이콘이 있는지 확인하십시오.

가 나는 JLabelpersonImage라는 사용자가 이미지를 삽입하고자 할 때, 그는 personImage 클릭하는이, 다음 JFileChooser가 나타나고 사용자가 이미지를 선택할 수 있습니다. 선택한 이미지가 personImage에로드됩니다.

사용자가 이미지를 선택하여 저장하면 제대로 작동하지만 사용자가 이미지를 선택하지 않고 세부 정보를 저장하면 NullPointerException이 표시됩니다. 그게 파일에 이미지를 얻을 경로가 없기 때문에 그것이라고 생각합니다. JLabel에 이미지가 있는지 여부를 어떻게 알 수 있습니까? 이미지가 있는지 없는지 확인하고 싶습니다.

try { 
    String fname = txt_Fname.getText(); 
    String lname = txt_Lname.getText(); 
    String mobile = txt_mobile.getText(); 
    String home = txt_home.getText(); 
    String work = txt_work.getText(); 
    String fax = txt_fax.getText(); 
    byte[] image_detail; 

    PersonDAO perDAO = new PersonDAO(); //create person object 
    if (status == 1) // used status for check whether Jlabed is clicked 
    { 
     File image = new File(path); 
     FileInputStream fis = new FileInputStream(image); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     byte[] buf = new byte[1024]; 

     for (int readNum; (readNum = fis.read(buf)) != -1;) 
     { 
      baos.write(buf, 0,readNum); 
     } 
     image_detail = baos.toByteArray(); 

     Person person1 = new Person(fname, lname, mobile, home, work, fax, image_detail); // call the person 
     // constructer when there is an image. I did validate with status variable 
      perDAO.InsertPerson(person1); // call the personDAO to insert the Person to database 
     } 
     else 
     { 
      Person person2 = new Person(lname, lname, mobile, home, work, fax); // if there is not an image call this constructer . 
      perDAO.InsertPerson(person2); // then call to personDAO object to insert the person to databasee 
     } 
    } 
    catch (Exception exc) 
    { 
     System.out.println(exc + "sssssss"); 
    } 


    // >>> when click on the JLabel, the JFileChooser appears 
    int i = jFileChooser2.showOpenDialog(this); 
    try { 
     f = jFileChooser2.getSelectedFile(); 
     path = f.getAbsolutePath(); 
     ImageIcon image = new ImageIcon(path); 
     status = 1; 
     personImage.setIcon(image); 
    } 
    catch (Exception exc) 
    { 
     System.out.println(exc); 
    } 
+0

의를? –

+1

할 일은로드 된 이미지 컬렉션을 저장하는 것입니다. –

+0

예 이미지가있는 경우 이미지를 저장하고 싶습니다. –

답변

5

당신이 아이콘이나하지 못한 경우가 JLabel label 다음이 확인할 않는 경우 : 스택 트레이스가

if (label.getIcon() == null) { 
    // this means there is no icon 
} 
관련 문제