[Android] Prevent NPE

Geoffrey Métais git at videolan.org
Tue Feb 17 17:43:46 CET 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Feb 17 12:12:16 2015 +0100| [6c9a0bc0f39fdbd64fb759dfd487787a24cd3ad7] | committer: Geoffrey Métais

Prevent NPE

CID #1270674, + method refactoring

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=6c9a0bc0f39fdbd64fb759dfd487787a24cd3ad7
---

 libvlc/src/org/videolan/libvlc/LibVlcUtil.java |   24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/libvlc/src/org/videolan/libvlc/LibVlcUtil.java b/libvlc/src/org/videolan/libvlc/LibVlcUtil.java
index 0ddf780..d815cd1 100644
--- a/libvlc/src/org/videolan/libvlc/LibVlcUtil.java
+++ b/libvlc/src/org/videolan/libvlc/LibVlcUtil.java
@@ -253,20 +253,24 @@ public class LibVlcUtil {
         }
 
         float frequency = -1;
+        FileReader fileReader = null;
+        String line = "";
         try {
-            FileReader fileReader = new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
+            fileReader = new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
             BufferedReader br = new BufferedReader(fileReader);
-            String line = "";
-            try {
-                line = br.readLine();
-                frequency = Float.parseFloat(line) / 1000.f; /* Convert to MHz */
-            } catch(NumberFormatException e) {
-                Log.w(TAG, "Could not parse maximum CPU frequency!");
-                Log.w(TAG, "Failed to parse: " + line);
-            }
-            fileReader.close();
+            line = br.readLine();
+            frequency = Float.parseFloat(line) / 1000.f; /* Convert to MHz */
         } catch(IOException ex) {
             Log.w(TAG, "Could not find maximum CPU frequency!");
+        } catch(NumberFormatException e) {
+            Log.w(TAG, "Could not parse maximum CPU frequency!");
+            Log.w(TAG, "Failed to parse: " + line);
+        } finally {
+            if (fileReader != null) {
+                try {
+                    fileReader.close();
+                } catch (IOException e) {}
+            }
         }
 
         errorMsg = null;



More information about the Android mailing list