Android:读取txt文件时出现NullPointerException

时间:2013-04-18 17:06:55

标签: java android file nullpointerexception bufferedreader

我正在尝试为Android编写一个翻译App并想要读取一个txt文件。 但是,通过尝试读取文件,我总是得到NullpointerException。 我已将txt文件放入assets文件夹,但我认为应用程序无法找到它:( 我的代码:

(这是我的第60行的VocabTrainer类的代码: (vocsE,vocsG是LinkedLists和vocMapE,vocMapG是HasMaps作为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();
    }

这是logcat跟踪:

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 个答案:

答案 0 :(得分:0)

尝试将其放在res文件夹中。然后重新运行。如果它仍然不起作用,请调试问题是否由找不到文件错误引起。

答案 1 :(得分:0)

这个问题解释了如何做你想做的事情。按照这些步骤,如果仍然无效,请返回。

Reading a simple text file