【问题标题】:I cant find what causes the Exception我找不到导致异常的原因
【发布时间】:2012-11-06 23:32:15
【问题描述】:

我是一个 java 菜鸟,也是这个网站的新手,所以请耐心等待。如果我在发布此内容时做错了什么,请告诉我,对于我所做的任何事情或任何错误的语法,我提前道歉。

我需要在 java 中编写一个自定义 CSV 解析器,它不会在引号内分隔逗号。我不能使用任何与 CSV 类相关的东西。 1,2,3,4 -> 1 2 3 4 a,"b,c",d -> a b,c d

无论我尝试什么,我总是会遇到异常

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

public class csv
{
    public static void main(String[] args)
    {
        File file = new File("csv.test");
        BufferedReader buf = null;

        try
        {
            buf = new BufferedReader(new FileReader(file));

            String str;

            while ((str = buf.readLine()) != null)
            {
                String[] values = null; // array for saving each split substr
                int c1 = 0; // counter for current position in string
                int c2 = 0; // counter for next space in array to save substr
                int c3 = 0; // location of last comma or quotation for substring creation
                int i = 0; // counter used later for printing

                while (c1 <= str.length())
                {
                    char x = str.charAt(c1);
                    String s = Character.toString(x);

                    if (c1 == str.length())
                    {
                        values[c2] = str.substring(c3, (c1 - 1));
                    }
                    else if (s == ",")
                    {
                        values[c2] = str.substring(c3, (c1 - 1));
                        c1++;
                        c2++;
                        c3++;
                    }
                    else if (s == "\"")
                    {
                        c1++;
                        c3 = c1;
                        while (s != "\"")
                            c1++;
                        values[c2] = str.substring(c3, (c1 - 1));
                        c1 = c1 + 2;
                        c2++;
                        c3 = c1;
                    }
                    else
                    {
                        c1++;
                    }
                    while (i < values.length)
                        System.out.println("\"" + values[i] + "\"");
                }
            }
        }
        catch (Exception e)
        {
            System.out.println("Error locating test file");
        }
    }
}

我已尝试删除所有逻辑并测试它是否与文件 io 相关。读起来很好,所以我归结为它与逻辑有关。我看了一遍,找不到任何问题。我什至让一个朋友看过它,说它看起来不错。哪里抛出了异常?

【问题讨论】:

  • 能否请您包含完整的堆栈跟踪信息?这将是一个好的开始......
  • 可以分享一下相关的stacktrace吗?
  • e.printStackTrace(); 添加到catch 块并告诉我们它的含义...
  • +1 表现良好的菜鸟
  • java.lang.NullPointerException at csv.main(csv.java:56)

标签: java file csv


【解决方案1】:

您没有在String[] values = null; 这一行中初始化您的values,因此当您使用它时它会失败,即在列表values[c2] = str.substring(c3, (c1 - 1));

要解决此问题,请使用适当的长度初始化 values 数组,例如String[] values = new String[SIZE];。可能您需要使用str.length() 作为数组的SIZE

在您的比较else if (s == ",") 中,您正在使用== 比较字符串,这是不正确的。相反,您可以将x 本身用作else if (x == ',')

编辑:您的以下条件将使c1 无限增加,因为您不会在任何地方更改str(x after correction as advised) 的值。

旧状态:

             while (s != "\"")
                  c1++;

按照建议更改后(仍然错误,因为 x 在 while 循环内没有更改):

             while (x != '"')
                  c1++;

请更正循环逻辑。

【讨论】:

  • 当我修复这个问题时,我在第 46 行得到了 OutOfBounds 异常。这是为什么呢?
  • @user1834642:我相信你的第 46 行是这样的:values[c2] = str.substring(c3, (c1 - 1)); 如果是这样,你的计数器,即c2, c3 &amp; c1 超出了字符串长度。请检查逻辑。
  • @user1834642:您对while (s != "\"") c1++; 有什么期待。这将导致您的 c1++ 无限增加,因为您没有更改 x 的值。
【解决方案2】:

与具体问题无关,但不是s == ",",您应该执行",".equals(s),与其他字符串相等性检查类似。

【讨论】:

    【解决方案3】:

    这不是你的异常的原因,但它显然是不正确的:

        while (s != "\"")
            c1++;
    

    不正确有两个原因:

    • 由于c1++ 不会改变循环条件,这将永远循环。
    • 当您应该使用equals(...) 时,您正在使用== / != 来比较字符串。您似乎在其他地方也犯了这个错误。

    要找出导致异常的原因,您应该做的第一件事是打印出堆栈跟踪。将 e.printStackTrace(); 添加到 catch 块。

    或者更好的是,将其更改为仅捕获 IOException。捕获Exception 是个坏主意……除非您要记录/输出完整的堆栈跟踪。

    【讨论】:

    • 如果 == 是 s.equals(),那么 != 的等价物是什么。
    • @user1834642 - ! s.equals()
    猜你喜欢
    • 1970-01-01
    • 2018-08-04
    • 2017-07-27
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多