Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.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 对我来说没有意义的空指针?_Java_Arrays_Nullpointerexception - Fatal编程技术网

Java 对我来说没有意义的空指针?

Java 对我来说没有意义的空指针?,java,arrays,nullpointerexception,Java,Arrays,Nullpointerexception,我目前正在处理一个程序,每当我调用产品[1]时,都没有空指针错误。然而,当我调用产品[0]或产品[2]时,我会得到一个空指针错误。但是,我仍然得到两个不同的输出,几乎就像数组中有[0]和1或1和2一样。这是我的密码 FileReader file = new FileReader(location); BufferedReader reader = new BufferedReader(file); int numberOfLines = readLines();

我目前正在处理一个程序,每当我调用产品[1]时,都没有空指针错误。然而,当我调用产品[0]或产品[2]时,我会得到一个空指针错误。但是,我仍然得到两个不同的输出,几乎就像数组中有[0]和1或1和2一样。这是我的密码

    FileReader file = new FileReader(location);
    BufferedReader reader = new BufferedReader(file);

    int numberOfLines = readLines();
    String [] data = new String[numberOfLines];
    Products = new Product[numberOfLines];
    calc = new Calculator();

    int prod_count = 0;
    for(int i = 0; i < numberOfLines; i++)
    {
        data = reader.readLine().split("(?<=\\d)\\s+|\\s+at\\s+");
        if(data[i].contains("input"))
        {
            continue;
        }
        Products[prod_count] = new Product();
        Products[prod_count].setName(data[1]);
        System.out.println(Products[prod_count].getName());
        BigDecimal price = new BigDecimal(data[2]);
        Products[prod_count].setPrice(price);


        for(String dataSt : data)
        {

            if(dataSt.toLowerCase().contains("imported"))
        {
                Products[prod_count].setImported(true);
        }
            else{
                Products[prod_count].setImported(false);
            }

        }



        calc.calculateTax(Products[prod_count]);    
        calc.calculateItemTotal(Products[prod_count]);
        prod_count++;
此打印工作
System.out.println(产品[1].getProductTotal())

这将成为空指针
System.out.println(产品[2].getProductTotal())


这也将成为一个空指针
System.out.println(产品[0].getProductTotal())

您正在跳过包含“输入”的行

也许最好将
产品
制作成一个ArrayList,只向其中添加有意义的行

产品
也应以小写字母开头,以遵循Java约定。类型以大写字母开头,参数和变量以小写字母开头。并不是所有的Java编码约定都是完美的——但是这个约定非常有用

代码在其他方面结构良好,但数组不是一种非常灵活的类型,无法从程序逻辑构建(因为长度必须预先确定,跳过需要跟踪索引,并且在构建时无法跟踪大小)

通常,您应该构建列表(ArrayList)。Map(HashMap、LinkedHashMap、TreeMap)和Set(HashSet)也很有用


第二个错误:正如波希米亚人所说:在
data[]
中,您混淆了所有行列表的概念,而
data[]
是从一行解析/拆分的标记

“数据”通常是一个无意义的术语。使用有意义的术语/名称&你的程序中不太可能有bug

您可能应该只对行标记使用
标记
,而不是在需要它之前/之外声明它,也不要尝试按行索引它——因为,非常简单,应该完全没有必要这样做

for(int i = 0; i < numberOfLines; i++) {
    // we shouldn't need data[] for all lines,  and we weren't using it as such.
    String line = reader.readLine();
    String[] tokens = line.split("(?<=\\d)\\s+|\\s+at\\s+");
    //
    if (tokens[0].equals("input")) {        // unclear which you actually mean.
    /* if (line.contains("input")) { */    
        continue;
    }
for(int i=0;iString[]tokens=line.split((?您正在跳过包含“input”的行)

也许最好将
产品
制作成一个ArrayList,只向其中添加有意义的行

产品
也应以小写字母开头,以遵循Java约定。类型以大写字母开头,参数和变量以小写字母开头。并非所有Java编码约定都是完美的,但这一种非常有用

代码在其他方面结构良好,但数组不是一种非常灵活的类型,无法从程序逻辑构建(因为长度必须预先确定,跳过需要跟踪索引,并且在构建时无法跟踪大小)

通常,您应该构建List(ArrayList)。Map(HashMap、LinkedHashMap、TreeMap)和Set(HashSet)也很有用


第二个错误:正如波希米亚人所说:在
data[]
中,您混淆了所有行列表的概念,而
data[]
是从一行解析/拆分的标记

“数据”通常是一个无意义的术语。使用有意义的术语/名称&你的程序中不太可能有bug

您可能应该只对行标记使用
标记
,而不是在需要它之前/之外声明它,也不要尝试按行索引它——因为,非常简单,应该完全没有必要这样做

for(int i = 0; i < numberOfLines; i++) {
    // we shouldn't need data[] for all lines,  and we weren't using it as such.
    String line = reader.readLine();
    String[] tokens = line.split("(?<=\\d)\\s+|\\s+at\\s+");
    //
    if (tokens[0].equals("input")) {        // unclear which you actually mean.
    /* if (line.contains("input")) { */    
        continue;
    }
for(int i=0;iString[]tokens=line.split((?错误警报:您正在覆盖
数据

String [] data = new String[numberOfLines];
然后在循环中:

data = reader.readLine().split("(?<=\\d)\\s+|\\s+at\\s+");

data=reader.readLine()

String [] data = new String[numberOfLines];
然后在循环中:

data = reader.readLine().split("(?<=\\d)\\s+|\\s+at\\s+");

data=reader.readLine().split((?您需要为行号和新产品对象使用不同的索引。如果您有20行,但其中5行是“输入”,那么您只有15个新产品对象

例如:

int prod_count = 0;

for (int i = 0; i < numberOfLines; i++)
{
        data = reader.readLine().split("(?<=\\d)\\s+|\\s+at\\s+");
        if (data[i].contains("input"))
        {
            continue;
        }
        Products[prod_count] = new Product();
        Products[prod_count].setName(data[1]);
        // etc.
        prod_count++; // last thing to do
}
int prod\u count=0;
对于(int i=0;idata=reader.readLine().split((?您需要为行号和新产品对象使用不同的索引。如果您有20行,但其中5行是“输入”,那么您只有15个新产品对象

例如:

int prod_count = 0;

for (int i = 0; i < numberOfLines; i++)
{
        data = reader.readLine().split("(?<=\\d)\\s+|\\s+at\\s+");
        if (data[i].contains("input"))
        {
            continue;
        }
        Products[prod_count] = new Product();
        Products[prod_count].setName(data[1]);
        // etc.
        prod_count++; // last thing to do
}
int prod\u count=0;
对于(int i=0;idata=reader.readLine().split((?
readLines()
返回什么值?我强烈建议您学习使用调试器。readLines()返回一个int,表示文件中有多少行,David我使用过调试器,它说我在调用[2]或[0]的任何位置上都有一个空指针在该数组中。我不明白的是,调用产品[1]时,如何从calculate Tax()和calculateItemTotal()的数组中获得两个输出.getItemTotal我只得到一个,当我尝试调用2时,得到一个空指针error@user2786754通过外部for循环进行调试。执行是否会进入带有
continue
if
语句?
readLines()
返回什么值?我强烈建议您学习使用调试器。readLines()返回一个int,表示文件中有多少行,David我使用了调试器,它说我在该数组中调用[2]或[0]的任何点上都有一个空指针。我不明白的是,在调用产品[1]时,如何从数组中获得calculate Tax()和calculateItemTotal()的两个输出.getItemTotal我只得到一个,当我尝试调用2时,得到一个空指针error@user2786754通过外部for循环进行调试。执行是否进入带有
continue
if
语句?是,支持这样做,因为输入如下:输入2:10.00时输入一盒巧克力,10.00时输入一瓶香水47.50左右