java - 线程异常...: null - trying to convert String from file to Integer

标签 java exception

就像标题中一样,我被这个错误困扰了一段时间。我正常地从文件中获取值,但是当我尝试转换它时,错误就消失了。我阅读了很多主题,但找不到任何与我的案例类似的案例(带文件)或任何好的提示。我尝试添加断言,但没有帮助。错误的完整描述是:

Exception in thread "main" java.lang.NumberFormatException: null
at java.base/java.lang.Integer.parseInt(Integer.java:620)
at java.base/java.lang.Integer.parseInt(Integer.java:776)
at EnergyMeasure_needs_to_be_completed.main(EnergyMeasure_needs_to_be_completed.java:85)

我也是初学者(但我想你已经知道了,呵呵;))

import java.util.Scanner;
import java.io.*;

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

        //int work_of_energy;
        Scanner input = new Scanner(System.in);

        System.out.println("\t\t\t\t Hi , this program will count how many kWh you're using");

        //asks about number of devices
        System.out.println("First of all, how many the same devices do you have in your house ?");
        int devices = input.nextInt();

        boolean bool = false;

        do {
            if (devices < 0) {
                System.out.println("You can't have less than 0 devices in your home!\nMake input once again :");
                devices = input.nextInt();
            } else {
                System.out.println("Okay, so you've got " + devices + " same devices.");
                bool = true;
                break;
            }
        }while(bool = true);

        //asks about time of use
        System.out.println("\nHow many hours you use them per day?");
        int time_use = input.nextInt();

        do {
            if (time_use > 24 || time_use < 0) {
                System.out.println("Wrong!\nMake input once again :");
                time_use = input.nextInt();
            }
            else{
                System.out.println("You use your devices for " + time_use + "h");
                bool = true;
                break;
            }
        }while(bool = true);
        /*else if(!input.hasNextInt()){
            System.out.println("Invalid input! \nEnter an integer : ");
            time_use = input.nextInt();
        } */

        //downloads value of power from file
        String power_dev; //path to the file

        power_dev = null; //reference to one line at a time

        try {
            FileReader fileReader = //reads text files in the default encoding
                    new FileReader("power_monitors");

            BufferedReader bufferedReader = //deal with a line at a time
                    new BufferedReader(fileReader);

            while((power_dev = bufferedReader.readLine()) != null) { 
                System.out.println("\nThe power of your devices is " + power_dev + "W");
            }

            bufferedReader.close(); //close file

        }
        catch (FileNotFoundException e) { //if file doesn't exist catch the except
            System.out.println("Unable to open file");
        }

        //assert power_dev != null; 

        int power_dec = Integer.parseInt(power_dev); //change the String ,to Integer
        int power_of_devices = power_dec * devices; //summary devices power
        //count the cost of work (W = P * t) [kWh]
        int work_of_energy = (power_of_devices / 1000) * time_use;

        System.out.println("The work of energy equals : " + work_of_energy);
    }
}

最佳答案

如果打印 power_dev,你会得到什么?它是什么格式?因为 readLine() 返回一个文本行,所以根据您正在读取的源,您可能会得到不仅仅是一个 int。

为什么不使用read()方法呢?它返回一个 int,因此您不必解析 power_dev。

同样,在没有看到文件或没有可重现代码的情况下很难回答您的问题,但我最好的猜测是 power_dev 返回 null 或 Integer.parseInt() 方法无法解析的内容。

关于java - 线程异常...: null - trying to convert String from file to Integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59796209/

相关文章:

java - 为 TeamCity 配置 Java 版本

java - 找不到文件java

python - 当异常发生时我应该提出还是冒泡?

java - 我不明白为什么会出现此错误 : ConcurrentModificationException

Java [unchecked] 未检查大小写警告

java - 如何将图像从java小程序发送到javascript?

java - java中的映射键值对未正确获取

java - 发送网格: "The provided authorization grant is invalid, expired, or revoked"

java - 为什么以相反的执行顺序处理 try-with-resource 的抑制异常?

python - 为什么我不能将 char 附加到 Python 中的空列表?