2

I'm trying to write a translator App for Android and want to read a txt file. However I get always a NullpointerException by trying to read the file. I have put the txt file into the assets folder but I think that the app can't find it :( My code:

(This is the code of my class VocabTrainer from line 160: (vocsE, vocsG are LinkedLists and vocMapE, vocMapG are HasMaps as fields of VocabTrainer))

try{
        AssetManager manager;
        manager = currentContext.getAssets();
        InputStream input = manager.open("vocabs.txt");
        InputStreamReader inputReader = new InputStreamReader(input);

        BufferedReader reader = new BufferedReader(inputReader);
        String word = null;

        while ((word = reader.readLine()) != null) {
            //Splitts the given String when there is given " " 
            String[] wordSplit = word.split(" ");

            vocsE.add(wordSplit[0]);
            vocsG.add(wordSplit[1]);

            vocMapE.put(wordSplit[0], wordSplit[1]);
            vocMapG.put(wordSplit[1], wordSplit[0]);
        }
        reader.close();
    }
    catch (IOException e) {
        e.printStackTrace();
    }

And here is the logcat trace:

04-18 17:24:27.204: E/AndroidRuntime(1736): java.lang.RuntimeException: Unable to    
instantiate activity  
ComponentInfo{de.dbgeppelheim.dbgvocab/de.dbgeppelheim.dbgvocab.TrainingActivity}: 
java.lang.NullPointerException
04-18 17:24:27.204: E/AndroidRuntime(1736):     at  
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
android.app.ActivityThread.access$600(ActivityThread.java:141)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at    
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
android.os.Handler.dispatchMessage(Handler.java:99)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
android.os.Looper.loop(Looper.java:137)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at    
android.app.ActivityThread.main(ActivityThread.java:5039)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
java.lang.reflect.Method.invokeNative(Native Method)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at  
java.lang.reflect.Method.invoke(Method.java:511)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at    
dalvik.system.NativeStart.main(Native Method)
04-18 17:24:27.204: E/AndroidRuntime(1736): Caused by: java.lang.NullPointerException
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
android.content.ContextWrapper.getResources(ContextWrapper.java:89)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
de.dbgeppelheim.functionality.VocabTrainer.initialise(VocabTrainer.java:167)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
de.dbgeppelheim.functionality.VocabTrainer.<init>(VocabTrainer.java:40)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
de.dbgeppelheim.dbgvocab.TrainingActivity.<init>(TrainingActivity.java:25)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at  
java.lang.Class.newInstanceImpl(Native Method)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
java.lang.Class.newInstance(Class.java:1319)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
android.app.Instrumentation.newActivity(Instrumentation.java:1054)
04-18 17:24:27.204: E/AndroidRuntime(1736):     at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
04-18 17:24:27.204: E/AndroidRuntime(1736):     ... 11 more

2 Answers2

0

have a try to put it on the res folder. And rerun. If it still doesn't work, debug if the problem is caused by the file not found error.

Lance
  • 551
  • 4
  • 5
0

This question explains how to do what you're trying to do. Follow those steps, then come back if it still doesn't work.

Reading a simple text file

Community
  • 1
  • 1
NathanTempelman
  • 1,301
  • 9
  • 34
  • Okay, then I'll try to help. Where is this code section located? Is it in the currently running activity? The current context is needed to get to the textfile, and that's only available in an activity. If this code is in a non activity library file or something that doesn't extend Activity, then you'll have to pass the context when you call it from an activity. – NathanTempelman Apr 18 '13 at 17:55
  • Yes it's not in an Activity but I pass it to the contructor of another class which use it as an argument for a method (which should read the file) – user2296122 Apr 18 '13 at 18:26
  • Somewhere in there is probably your problem. Android is really annoying for that sort of thing. Try putting that method into an activity just to test it. If it works in there, there's something screwwy with your context passing. Let me know how that goes. – NathanTempelman Apr 18 '13 at 18:58
  • Try this method instead: Put the file in the /res/raw directory, where the file will be indexed and is accessible by an id in the R file: InputStream is = getResources().openRawResource(R.raw.vocabs); Then see if you can read from that input stream. – NathanTempelman Apr 18 '13 at 19:10
  • Try it in your main activity. Just to see if it will work. If it doesn't, then something is horribly wrong with your project, or you're doing something wrong. Try to do the exact steps in the question I linked you. – NathanTempelman Apr 18 '13 at 20:03
  • 1
    Thanks! I just tried it with a new project and it works. But I have no idea why the same code doesn't work in my other project... – user2296122 Apr 18 '13 at 20:46
  • Yeah, that's the something horribly wrong with your android project option. Try going File-> Android Tools -> Fix project properties. That sometimes fixes it. – NathanTempelman Apr 18 '13 at 20:51
  • Ok now I have changed the concept of my classes and now it works...crazy :D Thank you all for the support :) – user2296122 Apr 18 '13 at 21:12