简体   繁体   English

BufferedReader中的NullPointerException

[英]NullPointerException in BufferedReader

I'm trying to use BufferedReader to import strings from a .txt file into an Arraylist , then using a random method to randomly pick a string inside the Arraylist . 我正在尝试使用BufferedReader将.txt文件中的字符串导入到Arraylist ,然后使用随机方法随机选择Arraylist的字符串。

But whenever I run this code, it gives me a java.lang.NullPointerException . 但每当我运行此代码时,它都会给我一个java.lang.NullPointerException

What should I do to fix this problem? 我该怎么做才能解决这个问题? Thank you in advance. 先感谢您。

java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

the .txt file in question consists of a few lines of words. 有问题的.txt文件由几行单词组成。

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;

public class WordList{

    private static ArrayList<String> words =new ArrayList<String>();


    public void main(String[] args) throws IOException {
        ArrayListCon("Majors.txt");
        System.out.println(words);
    }

    private void ArrayListCon(String filename) throws IOException{
        String line;
        BufferedReader br = null; 
                 br = new BufferedReader(new FileReader(filename));

        while (( line = br.readLine()) != null){
            words.add(line);      
        }
        br.close();
    }

    public static String getRandomWord(){
        Random r = new Random();
        String randomWord = words.get(r.nextInt(words.size()));
        return randomWord;
    }
}

After making the following changes, your code worked perfectly for me, and i never saw the null pointer exception error. 进行以下更改后,您的代码对我来说非常合适,而且我从未看到空指针异常错误。

1 ) I first made the method main static, as I was getting an error that there was no main method found: 1)我首先使方法main为static,因为我收到的错误是找不到主方法:

public static void main(String[] args) throws IOException {

2 ) I also made the ArrayListCon method static 2)我还将ArrayListCon方法设为静态

private static void ArrayListCon(String filename) throws IOException{

3 ) I made a file called Majors.txt with the contents: 3)我制作了一个名为Majors.txt的文件,内容如下:

hello
hi
there
my
words
are
cool

4 ) Finally, I just compiled and ran the program, with the following output: 4)最后,我刚编译并运行程序,输出如下:

javac WordList.java
java WordList
[hello, hi, there, my, words, are, cool]

I believe the issue is coming up with how you are running the code (edu.rice.cs.drjava.model.compiler.JavacCompiler) 我相信问题出现在你如何运行代码(edu.rice.cs.drjava.model.compiler.JavacCompiler)

The exception is arising as a result of a bug in both your code and in the DrJava code. 由于代码和DrJava代码中的错误而导致异常。

In your code, you need to make your main method static. 在您的代码中,您需要使main方法保持静态。

In the DrJava code, they need to add a check for Modifier.isStatic(m.getModifiers()) in the JavacCompiler.runCommand method. 在DrJava代码中,他们需要在JavacCompiler.runCommand方法中添加对Modifier.isStatic(m.getModifiers())的检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NullPointerException 和 BufferedReader - NullPointerException and BufferedReader Java:BufferedReader和NullPointerException - Java: BufferedReader and NullPointerException 如何在 Android 上的 Bufferedreader 中修复 NullPointerException? - How to fix NullPointerException in Bufferedreader on Android? 使用ObjectInputStream而不是BufferedReader时获取NullPointerException - Get NullPointerException when using ObjectInputStream instead of BufferedReader NullPointerException,虽然它不为空,但读取BufferedReader的内容时 - NullPointerException while reading content of BufferedReader although it's not empty 从BufferedReader读取时发生NullPointerException,仅当以jar运行时 - NullPointerException when reading from BufferedReader, only when ran as a jar 使用 BufferedReader 从抛出 NullPointerException 的二维数组中获取值 - Using BufferedReader to grab values from a 2D array throwing NullPointerException 在while循环中使用bufferedreader时获取NullPointerException - Getting NullPointerException while using bufferedreader inside while loop BufferedReader / AsynchTask- java.lang.NullPointerException:锁== null - BufferedReader / AsynchTask- java.lang.NullPointerException: lock == null 使用BufferedReader(readLine())检查更多值时如何停止NullPointerException - How to stop a NullPointerException when using BufferedReader (readLine()) to check for any more values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM