这个Java NullPointerException的原因是什么? [重复]

时间:2022-12-15 17:04:32

This question already has an answer here:

这个问题在这里已有答案:

I seem to get a NullPointerException in my code at line 15, but I just cannot understand why. I have been stuck on this for some hour now, and I don't know how to fix it. I have read up on what NullPointerException is, and I think I have a clear grasp of what it is, but I thought my if-statement would fix it, but apparently not.

我似乎在第15行的代码中得到了NullPointerException,但我无法理解为什么。我现在已经坚持了几个小时,我不知道如何解决它。我已经读过NullPointerException是什么,我想我已经清楚地知道它是什么,但我认为我的if语句会解决它,但显然不是。

Here is the code:

这是代码:

package learning;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Index{
    public static void main(String args[]){
        Index indexObject = new Index();
        String filePath = "C:\\Users\\Edvin\\Desktop\\inputPrices.txt";
        Scanner inputScanner = new Scanner(System.in);
        int average = 0;
        if(indexObject != null){
            average = indexObject.findAverage(getFileInfo(filePath).split(","));
        }
        else{
            System.out.println("Object instance is null. Terminating program.");
        }

        int currentItemInput = inputScanner.nextInt();
        if(currentItemInput<average && currentItemInput != 0){

        }
    }

    private static String getFileInfo(String x){
        File listOfPrices = new File(x);
        String error = "An error occurred";
        try{
            BufferedReader getInfo = new BufferedReader(new FileReader(listOfPrices));

            String prices = getInfo.readLine();

            while(prices != null){
                prices = getInfo.readLine();
            }
            return prices;
        }
        catch(FileNotFoundException e){
            System.out.println("Couldn't find File");
            System.exit(0);
            return error;
        }
        catch(IOException e){
            System.out.println("An I/O error occurred");
            System.exit(0);
            return error;
        }
    }

    public int findAverage(String[] tempIndivPrices){
        int x = 0;
        int y = 0;
        int total;
        int [] indivIntPrices = null;
        for(String i : tempIndivPrices){
            indivIntPrices[x] = Integer.parseInt(tempIndivPrices[x]);
            x++;
        }
        for (int element : indivIntPrices){
            total =+ indivIntPrices[element];
            if(element==indivIntPrices.length-1){
                int result = total / indivIntPrices.length;
                System.out.println(result);
                return result;
            }
        }
        return 0;
    }
}

3 个解决方案

#1


2  

You are declaring an array but you initialize it to null in this line: int [] indivIntPrices = null;

您正在声明一个数组,但在此行中将其初始化为null:int [] indivIntPrices = null;

indivIntPrices is now a null pointer. You are then trying to access this array two lines later indivIntPrices[x] = ... which is not going to work because the variable is not pointing to an array.

indivIntPrices现在是一个空指针。然后你试图在两行之后访问这个数组indivIntPrices [x] = ...这不会起作用,因为变量没有指向数组。

To solve this, allocate a new array of integers with the same size as the input array:

要解决此问题,请分配一个与输入数组大小相同的新整数数组:

int [] indivIntPrices = new int[tempIndivPrices.length];

#2


1  

Try to check this part of the code: getFileInfo(filePath)

尝试检查这部分代码:getFileInfo(filePath)

Are you getting the properly result of it? It's the only part that you can get this error.

你得到了正确的结果吗?这是唯一可以获得此错误的部分。

#3


0  

Remove this loop:

删除此循环:

while(prices != null){
    prices = getInfo.readLine();
}

Then, it works.

然后,它的工作原理。

#1


2  

You are declaring an array but you initialize it to null in this line: int [] indivIntPrices = null;

您正在声明一个数组,但在此行中将其初始化为null:int [] indivIntPrices = null;

indivIntPrices is now a null pointer. You are then trying to access this array two lines later indivIntPrices[x] = ... which is not going to work because the variable is not pointing to an array.

indivIntPrices现在是一个空指针。然后你试图在两行之后访问这个数组indivIntPrices [x] = ...这不会起作用,因为变量没有指向数组。

To solve this, allocate a new array of integers with the same size as the input array:

要解决此问题,请分配一个与输入数组大小相同的新整数数组:

int [] indivIntPrices = new int[tempIndivPrices.length];

#2


1  

Try to check this part of the code: getFileInfo(filePath)

尝试检查这部分代码:getFileInfo(filePath)

Are you getting the properly result of it? It's the only part that you can get this error.

你得到了正确的结果吗?这是唯一可以获得此错误的部分。

#3


0  

Remove this loop:

删除此循环:

while(prices != null){
    prices = getInfo.readLine();
}

Then, it works.

然后,它的工作原理。