Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NullPointerException但为什么_Java_Nullpointerexception - Fatal编程技术网

Java NullPointerException但为什么

Java NullPointerException但为什么,java,nullpointerexception,Java,Nullpointerexception,我知道,当您尝试为0提供它没有的属性时,会得到NullPointerException。但是为什么我会在这里得到一个nullpointerexception呢?它说应该在代码的这一部分: class Oblig3A{ public static void main(String[]args){ OrdAnalyse oa = new OrdAnalyse(); String filArgs=args[0]; oa.analyseMetode(filArg

我知道,当您尝试为0提供它没有的属性时,会得到NullPointerException。但是为什么我会在这里得到一个nullpointerexception呢?它说应该在代码的这一部分:

    class Oblig3A{
    public static void main(String[]args){

    OrdAnalyse oa = new OrdAnalyse();
    String filArgs=args[0];
    oa.analyseMetode(filArgs);
    }
}

class OrdAnalyse{
    void analyseMetode(String filArgs){

    //Begynner med aa opprette alle variabler som trengs, disse deklareres rett under. De ligger her oppe bare for at jeg skal ha oversikten over de.
    Scanner input, innfil;
    String[] ord, fortelling;
    int[] antall;
    int antUnikeOrd;
    PrintWriter utfil;

    //Deklarerer alle bortsett fra de som har med fil aa gjore, disse deklareres inne i en try-catch-loop (printwriter utfil og scanner innfil).
    input=new Scanner(System.in);
    ord=new String[5000];
    antall=new int[5000];
    antUnikeOrd=0;

    try{
        innfil=new Scanner(new File(filArgs));
        //Naa skal jeg dele opp prosessen litt for aa faa inn funksjonaliteten for aa for eksempel sette alle ord til lowercase.

        while(innfil.hasNext()){
        fortelling=innfil.nextLine().toLowerCase().split(" ");
            for(int i=0; i<fortelling.length; i++){
               for(int j=0; j<5000; j++){
               if(fortelling[i].equals(ord[j])){
                   antall[j]+=1;
               }else if(!ord[j].equals(fortelling[i])){ //This is line 39
                   ord[j]=fortelling[i];
                   antall[j]+=1;
                   antUnikeOrd+=1;
               }
               System.out.print(fortelling[i]);
               System.out.print(fortelling.length);
               }
           }
           }
       }catch(Exception e){
           e.printStackTrace();
       }
    }
 }

我现在已经确认了这确实是一条有问题的线,尽管假设innfil是一个扫描器:


尝试
innfil.hasNextLine()
而不是
innfil.hasNext()

在此处创建字符串数组

ord=new String[5000];
但决不要初始化数组中的任何字符串

所以这条线

}else if(!ord[j].equals(fortelling[i])){
如果条件
fortelling[i].equals(ord[j])
失败,将抛出NPE,这将作为其请求 “
fortelling[i]
是否等于
null
”,它不能这样做,或者它是抛出NPE的罪魁祸首


您可以将您的if语句更改为此

if(fortelling[i].equals(ord[j]))
      antall[j]+=1;
else   // remove your if statement here
{
      ord[j]=fortelling[i];
      antall[j]+=1;
      antUnikeOrd+=1;
}

如果这个
fortelling[i].equals(ord[j]
是false,那么这个
if(!ord[j].equals(fortelling[i])

哪一行得到NPE?最有可能来自
ord[j].equals(fortelling[i])
你能保证
ord
中的值不为空吗?还有
ord
初始化在哪里?或者
antall[j]
或者
antUnikeOrd
抛出的原因是什么?如果(!ord[j].equals(fortelling[i]),你可以删除
如果它不抛出NPE,那么它必须是真的。现在添加了整个代码以及堆栈跟踪。与其他if关联的if语句呢?
if(fortelling[i].equals(ord[j]){
也访问
ord
。您可以调用
String.equals(null)
,它只返回falseDamnit。我如何完成从文件中读取一个单词的任务,检查它是否存在于ord数组中,如果存在:将+1添加到与ord数组中的单词位于同一索引处的antall数组中,如果不存在:将其添加到数组的末尾?您几乎有正确的代码来执行此操作您只需使用首先比较的单词填充数组
ord
。@Makri您必须使用数组吗?这里似乎
Map
更容易。此外,如果您想从文件中读取每个单词,请使用
next
方法,而不是读取整行并拆分它。
if(fortelling[i].equals(ord[j]))
      antall[j]+=1;
else   // remove your if statement here
{
      ord[j]=fortelling[i];
      antall[j]+=1;
      antUnikeOrd+=1;
}