Search code examples
javalinked-listimage-comparison

retrieving data from a link list of File and comparing the obtain image with the existing image


i am trying to obtain File from the link list of File and then compare the obtained image with the existing image on imageView but it is giving null pointer exception enter code here

files.stream().map((file) -> file.toPath().toString()).forEachOrdered((string) -> {
try {
    Image source =new Image(new FileInputStream(string));
    if(source==image){
        stringFilePath=string;
    }
} catch (FileNotFoundException ex) {
    Logger.getLogger(SecondFrameController.class.getName()).log(Level.SEVERE, null, ex);
}

i also tried it with for loop but the problem still exist

  for(File file:files)
            {
                 String string= file.toPath().toString();
                        try {
                            Image source =new Image(new FileInputStream(string));
                             if(source==image){
                                   stringFilePath=string;
                             }
                        } catch (FileNotFoundException ex) {
                            Logger.getLogger(SecondFrameController.class.getName()).log(Level.SEVERE, null, ex);
                        }
            }

Solution

  • In your case the == operator would never return true, as it is used to identify instance, not to compare images.
    See this answer for more on == on objects: http://stackoverflow.com/a/19966154/2284641

    Try using a method of the class Image, that could be used to compare it with another Image instance.