Please if needed, you really need to do a complete reading so you can really understand my problem. I will be most greatful and thankful for that. i get this:
java.io.FileNotFoundException: photo.png (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at com.nationalid.NationalIDModel.getPicture(NationalIDModel.java:150)
at com.nationalid.NationalViewController.goToPrint(NationalViewController.java:560)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
when I run my app.
This line:
at com.nationalid.NationalIDModel.getPicture(NationalIDModel.java:150)
correspond to the line 6 of this code:
public FileInputStream getPicture(PreparedStatement preparedStatement)
{
File file = new File("photo.png");
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
preparedStatement.setBinaryStream(7, (InputStream) fileInputStream, (int) file.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return (FileInputStream) Picture.getBean();
}
And this one:
at com.nationalid.NationalViewController.goToPrint(NationalViewController.java:560)
corresponds to the line where i set the binaryStream at index 7 (image File):
public void goToPrint(ActionEvent event) {
myConnection = SqliteConnection.connector();
try {
NationalIDModel national = (NationalIDModel)nationalTable.getSelectionModel().getSelectedItem();
String query = "SELECT * FROM nationalid_table " + "WHERE unique_number = ? AND passportID = ? AND firstname = ?" + "AND lastname = ? AND issuedate = ? AND expirydate = ? AND " + "picture = ? AND nationality = ? AND dateofbirth = ? AND " + "sex = ? AND address = ? AND height = ? AND " + "id_number = ? AND number_of_id = ? AND age = ?";
preparedStatement = myConnection.prepareStatement(query);
preparedStatement.setString(1, national.getUniqueNumber());
preparedStatement.setString(2, national.getPassportID());
preparedStatement.setString(3, national.getFirstname());
preparedStatement.setString(4, national.getLastname());
preparedStatement.setString(5, national.getIssueDate());
preparedStatement.setString(6, national.getExpiryDate());
preparedStatement.setBinaryStream(7, national.getPicture(preparedStatement));
preparedStatement.setString(8, national.getNationality());
preparedStatement.setString(9, national.getDateOfBirth());
preparedStatement.setString(10, national.getGender());
preparedStatement.setString(11, national.getAddress());
preparedStatement.setString(12, national.getHeight());
preparedStatement.setString(13, national.getBirthID());
preparedStatement.setString(14, national.getIDNumber());
preparedStatement.setString(15, national.getAge());
myResultSet = preparedStatement.executeQuery();
if (myResultSet.next())
{
//((Node)event.getSource()).getScene().getWindow().hide();
Stage window = new Stage();
FXMLLoader loader = new FXMLLoader();
Pane root = loader.load(getClass().getResource("../nationalid/PersonsDetailsView.fxml").openStream());
PersonsDetailsController personsDetailsController = (PersonsDetailsController) loader.getController();
personsDetailsController.getUsers(
(FileInputStream) national.getPicture(preparedStatement),
national.getFirstname(),
national.getLastname(),
national.getGender(),
national.getHeight(),
national.getDateOfBirth(),
national.getNationality(),
national.getAddress(),
national.getIDNumber(),
national.getIssueDate(),
national.getExpiryDate()
);
Scene scene = new Scene(root);
window.setTitle("Hello " + txtfirstname.getText() + ", this is your menu page");
window.getIcons().add(new Image("file:users.png"));
window.setScene(scene);
window.show();
NotificationType type = NotificationType.SUCCESS;
TrayNotification trayNotification = new TrayNotification();
trayNotification.setTitle("User Details");
trayNotification.setMessage("User Details Displayed!");
trayNotification.setNotificationType(type);
trayNotification.showAndDismiss(Duration.seconds(4));
}
preparedStatement.close();
} catch (SQLException ep) {
//Logger.getLogger(AddUserController.class.getName()).log(SEVERE, null, ep);
System.out.println(ep);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
But first This is the context of my problem:
I am building a desktop application using javafx in intelliJ idea. I am using the MVC pattern. I have build one module already: display all birth with all almost all possible data manipulation including statistical estimation and representation of the database values.
Now i am building the second module: National Identification that will pull the data from Birth table, do possible manipulation and store it in the ID table. The aim of the project is actually to get Genuine tracking of each individual...
Now in the birth module i had a view and print button to view and print birth, so i have to pull data from database. each windows is an fxml file with a specific controller associated with it.
In the present module(Identification), i want to do the same but this time around i also have to pull image from the database too.
so i was able to pull all information and display them including the image of an individual correctly in the imageview.
Now in my ID module package i have an a model for database manipulation:
package com.nationalid;
public class NationalIDModel {
private final SimpleStringProperty UniqueNumber;
private final SimpleStringProperty PassportID;
private final SimpleStringProperty Firstname;
private final SimpleStringProperty Lastname;
private final SimpleStringProperty IssueDate;
private final SimpleStringProperty ExpiryDate;
private final SimpleStringProperty Picture;
private final SimpleStringProperty Nationality;
private final SimpleStringProperty DateOfBirth;
private final SimpleStringProperty Gender;
private final SimpleStringProperty Address;
private final SimpleStringProperty Height;
private final SimpleStringProperty BirthID;
private final SimpleStringProperty IDNumber;
private final SimpleStringProperty NumberOfID;
private final SimpleStringProperty Age;
private final SimpleStringProperty Status;
public NationalIDModel(String unique_number,
String passportID,
String firstname,
String lastname,
String issuedate,
String expirydate,
String picture,
String nationality,
String dateofbirth,
String gender,
String address,
String height,
String birthid,
String id_number,
String number_of_id,
String age,
String status
)
{
this.UniqueNumber = new SimpleStringProperty(unique_number);
this.PassportID = new SimpleStringProperty(passportID);
this.Firstname = new SimpleStringProperty(firstname);
this.Lastname = new SimpleStringProperty(lastname);
this.IssueDate = new SimpleStringProperty(issuedate);
this.ExpiryDate = new SimpleStringProperty(expirydate);
this.Picture = new SimpleStringProperty(picture);
this.Nationality = new SimpleStringProperty(nationality);
this.DateOfBirth = new SimpleStringProperty(dateofbirth);
this.Gender = new SimpleStringProperty(gender);
this.Address = new SimpleStringProperty(address);
this.Height = new SimpleStringProperty(height);
this.BirthID = new SimpleStringProperty(birthid);
this.IDNumber = new SimpleStringProperty(id_number);
this.NumberOfID = new SimpleStringProperty(number_of_id);
this.Age = new SimpleStringProperty(age);
this.Status = new SimpleStringProperty(status);
}
public String getUniqueNumber() {
return UniqueNumber.get();
}
public SimpleStringProperty uniqueNumberProperty() {
return UniqueNumber;
}
public void setUniqueNumber(String uniqueNumber) {
this.UniqueNumber.set(uniqueNumber);
}
public String getPassportID() {
return PassportID.get();
}
public SimpleStringProperty passportIDProperty() {
return PassportID;
}
public void setPassportID(String passportID) {
this.PassportID.set(passportID);
}
public String getFirstname() {
return Firstname.get();
}
public SimpleStringProperty firstnameProperty() {
return Firstname;
}
public void setFirstname(String firstname) {
this.Firstname.set(firstname);
}
public String getLastname() {
return Lastname.get();
}
public SimpleStringProperty lastnameProperty() {
return Lastname;
}
public void setLastname(String lastname) {
this.Lastname.set(lastname);
}
public String getIssueDate() {
return IssueDate.get();
}
public SimpleStringProperty issueDateProperty() {
return IssueDate;
}
public void setIssueDate(String issueDate) {
this.IssueDate.set(issueDate);
}
public String getExpiryDate() {
return ExpiryDate.get();
}
public SimpleStringProperty expiryDateProperty() {
return ExpiryDate;
}
public void setExpiryDate(String expiryDate) {
this.ExpiryDate.set(expiryDate);
}
public String getPicture()
{
return Picture.get();
}
public SimpleStringProperty pictureProperty() {
return Picture;
}
public void setPicture(String picture) {
this.Picture.set(picture);
}
public String getNationality() {
return Nationality.get();
}
public SimpleStringProperty nationalityProperty() {
return Nationality;
}
public void setNationality(String nationality) {
this.Nationality.set(nationality);
}
public String getDateOfBirth() {
return DateOfBirth.get();
}
public SimpleStringProperty dateOfBirthProperty() {
return DateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.DateOfBirth.set(dateOfBirth);
}
public String getGender() {
return Gender.get();
}
public SimpleStringProperty genderProperty() {
return Gender;
}
public void setGender(String gender) {
this.Gender.set(gender);
}
public String getAddress() {
return Address.get();
}
public SimpleStringProperty addressProperty() {
return Address;
}
public void setAddress(String address) {this.Address.set(address);}
public String getHeight() {
return Height.get();
}
public SimpleStringProperty heightProperty() {
return Height;
}
public void setHeight(String height) {
this.Height.set(height);
}
public String getBirthID() {
return BirthID.get();
}
public SimpleStringProperty birthIDProperty() {
return BirthID;
}
public void setBirthID(String birthID) {
this.BirthID.set(birthID);
}
public String getIDNumber() {
return IDNumber.get();
}
public SimpleStringProperty IDNumberProperty() {
return IDNumber;
}
public void setIDNumber(String IDNumber) {
this.IDNumber.set(IDNumber);
}
public String getNumberOfID() {
return NumberOfID.get();
}
public SimpleStringProperty numberOfIDProperty() {
return NumberOfID;
}
public void setNumberOfID(String numberOfID) {
this.NumberOfID.set(numberOfID);
}
public String getAge() {
return Age.get();
}
public SimpleStringProperty ageProperty() {
return Age;
}
public void setAge(String age) {
this.Age.set(age);
}
public String getStatus() {
return Status.get();
}
public SimpleStringProperty statusProperty() {
return Status;
}
public void setStatus(String status) {
this.Status.set(status);
}
}
i purposely try an attempt to modify the getPicture() method from this:
public String getPicture() {
return Picture.get();
}
to this:
public FileInputStream getPicture(PreparedStatement preparedStatement)
{
File file = new File("photo.png");
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
preparedStatement.setBinaryStream(7, (InputStream) fileInputStream, (int) file.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return (FileInputStream) Picture.getBean();
}
so that it can match with:
preparedStatement.setBinaryStream(7, national.getPicture(preparedStatement));
of the method: goToPrint(Mentioned above) of the class NationalViewController:
package com.nationalid;
public class NationalViewController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
Image image;
image = new Image(getClass().getResourceAsStream("download(1).png"));
imageViewGoDown.setImage(image);
//Image imageSave = new Image(getClass().getResourceAsStream("plus.png"));
//btnAdd.getGraphic().equals(imageSave);
brithidcolCS.setCellValueFactory(new PropertyValueFactory<>("BirthID"));
firstnamecolCS.setCellValueFactory(new PropertyValueFactory<>("Firstname"));
lasnamecolCS.setCellValueFactory(new PropertyValueFactory<>("Lastname"));
fathersnamecolCS.setCellValueFactory(new PropertyValueFactory<>("FathersName"));
mothersnamecolCS.setCellValueFactory(new PropertyValueFactory<>("MothersName"));
weightcolCS.setCellValueFactory(new PropertyValueFactory<>("Weight"));
heigthcolCS.setCellValueFactory(new PropertyValueFactory<>("Height"));
placeofbirthcolCS.setCellValueFactory(new PropertyValueFactory<>("PlaceOfBirth"));
nationalitycolCS.setCellValueFactory(new PropertyValueFactory<>("Nationality"));
gendercolCS.setCellValueFactory(new PropertyValueFactory<>("Gender"));
birthIDCertcolCS.setCellValueFactory(new PropertyValueFactory<>("BirthCertificateID"));
dateissuedcolCS.setCellValueFactory(new PropertyValueFactory<>("DateIssued"));
dateofbirthCS.setCellValueFactory(new PropertyValueFactory<>("DateOfBirth"));
agecolCS.setCellValueFactory(new PropertyValueFactory<>("Age"));
uniqueIDcolNI.setCellValueFactory(new PropertyValueFactory<>("UniqueNumber"));
firstnamecolNI.setCellValueFactory(new PropertyValueFactory<>("Firstname"));
lastnamecolNI.setCellValueFactory(new PropertyValueFactory<>("Lastname"));
dateofbirthcolNI.setCellValueFactory(new PropertyValueFactory<>("DateOfBirth"));
nationalitycolNI.setCellValueFactory(new PropertyValueFactory<>("Nationality"));
gendercolNI.setCellValueFactory(new PropertyValueFactory<>("Gender"));
heightcolNI.setCellValueFactory(new PropertyValueFactory<>("Height"));
addresscolNI.setCellValueFactory(new PropertyValueFactory<>("Address"));
cardIDcolNI.setCellValueFactory(new PropertyValueFactory<>("IDNumber"));
issuedatecolNI.setCellValueFactory(new PropertyValueFactory<>("IssueDate"));
expirydatecolNI.setCellValueFactory(new PropertyValueFactory<>("ExpiryDate"));
numberofIDcolNI.setCellValueFactory(new PropertyValueFactory<>("NumberOfID"));
statuscolNI.setCellValueFactory(new PropertyValueFactory<>("Status"));
birthIDcolNI.setCellValueFactory(new PropertyValueFactory<>("BirthID"));
passwordIDcolNI.setCellValueFactory(new PropertyValueFactory<>("PassportID"));
picturecolNI.setCellValueFactory(new PropertyValueFactory<>("Picture"));
agecolNI.setCellValueFactory(new PropertyValueFactory<>("Age"));
}
public void goToPrint(ActionEvent event) {
myConnection = SqliteConnection.connector();
try {
NationalIDModel national = (NationalIDModel)nationalTable.getSelectionModel().getSelectedItem();
String query = "SELECT * FROM nationalid_table " + "WHERE unique_number = ? AND passportID = ? AND firstname = ?" + "AND lastname = ? AND issuedate = ? AND expirydate = ? AND " + "picture = ? AND nationality = ? AND dateofbirth = ? AND " + "sex = ? AND address = ? AND height = ? AND " + "id_number = ? AND number_of_id = ? AND age = ?";
preparedStatement = myConnection.prepareStatement(query);
preparedStatement.setString(1, national.getUniqueNumber());
preparedStatement.setString(2, national.getPassportID());
preparedStatement.setString(3, national.getFirstname());
preparedStatement.setString(4, national.getLastname());
preparedStatement.setString(5, national.getIssueDate());
preparedStatement.setString(6, national.getExpiryDate());
preparedStatement.setBinaryStream(7, national.getPicture(preparedStatement));
preparedStatement.setString(8, national.getNationality());
preparedStatement.setString(9, national.getDateOfBirth());
preparedStatement.setString(10, national.getGender());
preparedStatement.setString(11, national.getAddress());
preparedStatement.setString(12, national.getHeight());
preparedStatement.setString(13, national.getBirthID());
preparedStatement.setString(14, national.getIDNumber());
preparedStatement.setString(15, national.getAge());
myResultSet = preparedStatement.executeQuery();
if (myResultSet.next())
{
//((Node)event.getSource()).getScene().getWindow().hide();
Stage window = new Stage();
FXMLLoader loader = new FXMLLoader();
Pane root = loader.load(getClass().getResource("../nationalid/PersonsDetailsView.fxml").openStream());
PersonsDetailsController personsDetailsController = (PersonsDetailsController) loader.getController();
personsDetailsController.getUsers(
(FileInputStream) national.getPicture(preparedStatement),
national.getFirstname(),
national.getLastname(),
national.getGender(),
national.getHeight(),
national.getDateOfBirth(),
national.getNationality(),
national.getAddress(),
national.getIDNumber(),
national.getIssueDate(),
national.getExpiryDate()
);
Scene scene = new Scene(root);
window.setTitle("Hello " + txtfirstname.getText() + ", this is your menu page");
window.getIcons().add(new Image("file:users.png"));
window.setScene(scene);
window.show();
}
preparedStatement.close();
} catch (SQLException ep) {
//Logger.getLogger(AddUserController.class.getName()).log(SEVERE, null, ep);
System.out.println(ep);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML private JFXButton btnbrowse;
public File file;
public FileChooser fileChooser;
private Desktop desktop = Desktop.getDesktop();
public Image image;
private FileInputStream fileInputStream;
public void browseToSelect(ActionEvent event) {
Stage window = new Stage();
fileChooser = new FileChooser();
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"),
new FileChooser.ExtensionFilter("Text Files", "*txt"),
new FileChooser.ExtensionFilter("Audio Files", "*wav", "*.mp3", "*.aac"),
new FileChooser.ExtensionFilter("All Files", "*.*")
);
file = fileChooser.showOpenDialog(window);
if(file != null)
{
imageView.setVisible(true);
image = new Image(file.toURI().toString(), 247, 232, true, true);
imageView.setImage(image);
//try {desktop.open(file);} catch (IOException e) {e.printStackTrace();}
}
}
public void pullIDDataOnClick() {
clearFields();
imageView.setVisible(true);
try
{
myConnection = SqliteConnection.connector();
NationalIDModel nationalIDModel = (NationalIDModel)nationalTable.getSelectionModel().getSelectedItem();
String query = "SELECT * FROM nationalid_table WHERE birthid = ?";
preparedStatement = myConnection.prepareStatement(query);
preparedStatement.setString(1, String.valueOf(nationalIDModel.getBirthID()));
myResultSet = preparedStatement.executeQuery();
while(myResultSet.next())
{
txtUniqueNumber.setText(myResultSet.getString("unique_number"));
txtPassportID.setText(myResultSet.getString("passportID"));
txtfirstname.setText(myResultSet.getString("firstname"));
txtlastname.setText(myResultSet.getString("lastname"));
(issuedatepicker.getEditor()).setText(myResultSet.getString("issuedate"));
(expirydatepicker.getEditor()).setText(myResultSet.getString("expirydate"));
InputStream inputStream = myResultSet.getBinaryStream("picture");
try (OutputStream outputStream = new FileOutputStream(new File("photo.jpg"))) {
byte[] content = new byte[1024];
int size;
while((size = inputStream.read(content)) != -1)
{
outputStream.write(content, 0, size);
}
outputStream.close();
inputStream.close();
image = new Image("file:photo.jpg", 247, 232, true, true);
imageView.setImage(image);
} catch (IOException e) {
e.printStackTrace();
}
txtnationality.setText(myResultSet.getString("nationality"));
(birthdatepicker.getEditor()).setText(myResultSet.getString("dateofbirth"));
if(null != myResultSet.getString("sex"))
switch (myResultSet.getString("sex")) {
case "Male":
rdmale.setSelected(true);
break;
case "Female":
rdfemale.setSelected(true);
break;
default:
rdmale.setSelected(false);
rdfemale.setSelected(false);
break;
}
else
{
rdmale.setSelected(false);
rdfemale.setSelected(false);
}
txtplaceofbirth.setText(myResultSet.getString("address"));
txtHeight.setText(myResultSet.getString("height"));
txtbirthid.setText(myResultSet.getString("birthid"));
txtIDNumber.setText(myResultSet.getString("id_number"));
txtNumberOfIDCard.setText(myResultSet.getString("number_of_id"));
txtage.setText(myResultSet.getString("age"));
statuslbl.setText(myResultSet.getString("status"));
}
//goToPrint();
preparedStatement.close();
myResultSet.close();
}
catch (SQLException ex)
{
System.out.println(ex);
Logger.getLogger(NationalViewController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
that is supposed to take me to this interface PersonsDetailsController:
package com.nationalid;
public class PersonsDetailsController implements Initializable{
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void getUsers(FileInputStream inputeStream, String userinfo1, String userinfo2,
String userinfo3, String userinfo4,
String userinfo5, String userinfo6,
String userinfo7, String userinfo8,
String userinfo9, String userinfo10)
{
userlabel.setText("View " + userinfo2 + "'s" + " Details");
try (OutputStream outputStream = new FileOutputStream(new File("photo.jpg"))) {
byte[] content = new byte[1024];
int size;
while((size = inputeStream.read(content)) != -1)
{
outputStream.write(content, 0, size);
}
outputStream.close();
inputeStream.close();
Image image = new Image("file:photo.jpg", 247, 232, true, true);
imageViewCapture.setImage(image);
} catch (IOException e) {
e.printStackTrace();
}
txtfirstname.setText(userinfo1);
txtlastname.setText(userinfo2);
txtgender.setText(userinfo3);
txtheight.setText(userinfo4);
txtdateofbirth.setText(userinfo5);
txtnationality.setText(userinfo6);
txtaddress.setText(userinfo7);
txtidnumber.setText(userinfo8);
txtissuedate.setText(userinfo9);
txtexpirydate.setText(userinfo10);
}
}
where I can print.
My aim is to pass the image from NationalViewController to PersonsDetailsController where i can print.
I have succeeded in pulling the image from the database to NationalViewController in its method pullIDDataOnclick() and displayed it in the imageview.
But i have the error mentionned above whenever i click on view button to go to PersonsDetailsController interface.
Please help! thanks.