2012-06-05 131 views
0

我有兩個類。 MetaDataExtractor(GUI)和MetaData。設置字段文本,Java

MetaData具有從圖像中提取元數據的方法。 MetaDataExtractor用於在JTextPane中顯示數據。 (請原諒班級名稱,我知道這有點令人困惑,我對Java相當陌生)。

MetaDataExtractor:

LongitudeField.setText("" + MetaDataTags.getLongitude()); 

的元數據:

public String getLongitude() { 
    try { 
     Metadata metadata = ImageMetadataReader.readMetadata(jpegFile); 
     if (metadata.containsDirectory(GpsDirectory.class)) { 
      GpsDirectory gpsDir = (GpsDirectory) metadata.getDirectory(GpsDirectory.class); 
      GpsDescriptor gpsDesc = new GpsDescriptor(gpsDir); 
      String Longitude = "" + gpsDesc.getGpsLongitudeDescription(); 

     } 
    } catch (ImageProcessingException ex) { 
     Logger.getLogger(MetaData.class.getName()).log(Level.SEVERE, null, ex); 
     System.out.println("Error 1"); 
    } catch (IOException ex) { 
     Logger.getLogger(MetaData.class.getName()).log(Level.SEVERE, null, ex); 
     System.out.println("Error 2"); 
    }   
    return longitude; 
} 

如果我設置要顯示在JTextPane的經度,則返回 「空」。但是,如果我將它設置爲在命令行上打印出來,它會打印出經度好嗎?

請原諒,如果它是一個簡單的解決方案。我仍然在與Java握手。

回答

0

Java是區分大小寫的,並首先聲明您的變量超出try語句的catch &。

使用像Eclipse這樣的IDE來減少像這樣的語法錯誤。

,所以你應該有:

public String getLongitude() { 
    String longitudeDesc =""; 
    try { 
     Metadata metadata = ImageMetadataReader.readMetadata(jpegFile); 
     if (metadata.containsDirectory(GpsDirectory.class)) { 
      GpsDirectory gpsDir = (GpsDirectory) metadata.getDirectory(GpsDirectory.class); 
      GpsDescriptor gpsDesc = new GpsDescriptor(gpsDir); 
      longitudeDesc = "" + gpsDesc.getGpsLongitudeDescription(); 

     } 
    } catch (ImageProcessingException ex) { 
     Logger.getLogger(MetaData.class.getName()).log(Level.SEVERE, null, ex); 
     System.out.println("Error 1"); 
    } catch (IOException ex) { 
     Logger.getLogger(MetaData.class.getName()).log(Level.SEVERE, null, ex); 
     System.out.println("Error 2"); 
    }   
    return longitudeDesc ; 
} 
+0

嗨Tchike,沒什麼區別。如果我要添加該行,「System.out.println(gpsDesc.getGpsLongitudeDescription());」進入MetaData它打印經度。只要我將它添加到一個字符串中,它就會顯示爲「null」。 – user1437260

+0

雖然可能不是這樣做的正確方法,但我已經設法通過將此行添加到MetaData中來修復它。 「字符串經度= gpsDesc.getGpsLongitudeDescription(); 經度=經度;」 – user1437260

+0

您是否嘗試用「經度」替換「經度」?因爲Java是區分大小寫的。 – tchike