1

I have a java class where it reads some data from a text file using a buffered reader and returns that data as a hash map:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

public class FrequencyLoader {

     public FrequencyLoader() throws FileNotFoundException {
        }

        public HashMap<String, Double> loadUnigramFrequencies() throws FileNotFoundException, IOException {
            HashMap<String, Double> unigramFrequencies = new HashMap<String, Double>();
            String line;
            String[] splittedLine;

            BufferedReader bf = new BufferedReader(new FileReader("unigramFrequencies.txt"));

            while ((line = bf.readLine()) != null) {
                splittedLine = line.split("\\s");


                unigramFrequencies.put(splittedLine[0].trim(), Double.parseDouble(splittedLine[1].trim()));
            }

            return unigramFrequencies;
        }
}

I want to use that in my android application but when I create an instance of this class and try to execute the loadUnigramFrequencies() function in the android Activity class I am getting an error that the application has stopped unexpectedly. I am trying to run it on Samsung Galaxy S2. Should the file be placed somewhere in the android project rather than on the disk? if yes then where?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Radek
  • 1,403
  • 3
  • 25
  • 54
  • 1
    Could you provide the logcat to help locate the error ? (Window/Show View/Other/Android/LogCat in eclipse or "adb logcat" in the shell) – Laurent' Sep 27 '11 at 12:09

3 Answers3

2

without a bit of logcat it is a bit trivial.

 unigramFrequencies.put(splittedLine[0].trim(), Double.parseDouble(splittedLine[1].trim()))

here for instance could be raised a null pointer execption if splittedLine[0] or splittedLine[1] is null, or parseDouble could arise a number format execption

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
2

I think the error might well be there :

BufferedReader bf = new BufferedReader(new FileReader("unigramFrequencies.txt"));

You should provide an absolute path here and first make sure that the file exists before accessing it or handle the exception.

If this file is some final asset, you should place it in your project assets folder and get a filereader from there.

Example (from here):

AssetFileDescriptor descriptor = getAssets().openFd("unigramFrequencies.txt");
FileReader reader = new FileReader(descriptor.getFileDescriptor());

Note that your unigramFrequencies.txt file should be present in your <project>/assets/ directory

Community
  • 1
  • 1
Laurent'
  • 2,611
  • 1
  • 14
  • 22
  • I placed the unigramFrequencies.txt file under assets. I added the following code: AssetFileDescriptor descriptor = getAssets().openFd("unigramFrequencies.txt"); BufferedReader bf = new BufferedReader(new FileReader(descriptor.getFileDescriptor())); – Radek Sep 27 '11 at 12:44
  • The error I am getting 09-27 13:37:57.860: ERROR/AndroidRuntime(7208): Caused by: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed 09-27 13:37:57.860: ERROR/AndroidRuntime(7208): at android.content.res.AssetManager.openAssetFd(Native Method) 09-27 13:37:57.860: ERROR/AndroidRuntime(7208): at org.com.SentencesActivity.loadUnigramFrequencies(SentencesActivity.java:97) 09-27 13:37:57.860: ERROR/AndroidRuntime(7208): at org.com.SentencesActivity.onClick(SentencesActivity.java:54) – Radek Sep 27 '11 at 12:45
  • What's the size of your file? – Laurent' Sep 27 '11 at 13:50
  • 1
    I know it sounds crazy but try this: rename your file from .txt to .png and change your code appropriately. See [this](http://stackoverflow.com/questions/3028717/how-to-play-videos-in-android-from-assets-folder-or-raw-folder/3537114#3537114) for an explanation. – Laurent' Sep 27 '11 at 13:58
1

This is searching for a needle in the hay stack.

I recommend you to first learn how to use debugging in Android:
http://www.droidnova.com/debugging-in-android-using-eclipse,541.html

Also some exception handling wouldn't hurt:
http://en.wikibooks.org/wiki/Java_Programming/Throwing_and_Catching_Exceptions

The following line of code is very wrong, and it seems you don't understand file storage in android:

new FileReader("unigramFrequencies.txt")

Here it is explained:
http://developer.android.com/guide/topics/data/data-storage.html

Caner
  • 57,267
  • 35
  • 174
  • 180