Java 从文件的链接列表中检索数据,并将获取的图像与现有图像进行比较
我试图从文件的链接列表中获取文件,然后将获取的图像与imageView上的现有图像进行比较,但它给出了空指针异常,请在此处输入代码Java 从文件的链接列表中检索数据,并将获取的图像与现有图像进行比较,java,linked-list,image-comparison,Java,Linked List,Image Comparison,我试图从文件的链接列表中获取文件,然后将获取的图像与imageView上的现有图像进行比较,但它给出了空指针异常,请在此处输入代码 files.stream().map((file) -> file.toPath().toString()).forEachOrdered((string) -> { try { Image source =new Image(new FileInputStream(string)); if(source==image){
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);
}
我也尝试了for循环,但问题仍然存在
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);
}
}
在您的情况下,
=
运算符永远不会返回true,因为它用于标识实例,而不是比较图像。有关对象的更多信息,请参见此答案: 尝试使用类
Image
的方法,该方法可用于将其与另一个Image
实例进行比较