我找不到导致异常的原因。

4

我是一个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");
        }
    }
}

我已经尝试排除所有逻辑问题并测试它是否与文件输入输出相关。读取方面都没问题,所以现在我认为是逻辑问题。我仔细检查了代码,但没有找到任何问题。我甚至让我的朋友帮我看过,他也说代码没有问题。异常是在哪里抛出的?


3
能否请您包含完整的堆栈跟踪信息?这将是一个好的起点... - mre
你能分享相关的堆栈跟踪吗? - Yogendra Singh
catch 块中添加 e.printStackTrace(); 并告诉我们它的输出内容。 - Stephen C
+1 对于新手来说是良好行为的。 - Christopher Bales
java.lang.NullPointerException 在 csv.java 的第56行,csv.main() - user1834642
“csv.java:56”是哪一行? - Stephen C
3个回答

3
您在这行代码中没有初始化您的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在按照建议进行更正后)的值。
旧条件:
             while (s != "\"")
                  c1++;
在按照建议更改后(在while循环内x仍未改变):
             while (x != '"')
                  c1++;

请修改循环逻辑。

当我修复这个问题时,第46行出现了越界异常。为什么会这样? - user1834642
@user1834642:我认为你的第46行是这样的:values[c2] = str.substring(c3, (c1 - 1)); 如果是这样的话,你的计数器即c2、c3和c1超出了字符串长度。请检查逻辑。 - Yogendra Singh
@user1834642:你在 while (s != "\"") c1++; 这一行中期望得到什么?由于你没有改变 x 的值,这将导致你的 c1++ 无限增加。 - Yogendra Singh

1

虽然与具体问题无关,但是你应该使用",".equals(s)而不是s == ",",其他字符串相等性检查也类似。


0

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

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

它有两个错误:

  • c1++ 不会改变循环条件,导致程序将一直循环。
  • 使用 == / != 来比较字符串是不正确的,应该使用 equals(...)。你似乎在其他地方也犯了这个错误。

要找出引起异常的原因,你应该做的第一件事是打印出堆栈跟踪信息。在 catch 块中添加 e.printStackTrace();

或者更好的方法是将其改为仅捕获 IOException。捕获 Exception 是一个不好的想法...除非你要记录/输出完整的堆栈跟踪信息。


如果 == 是 s.equals(),那么 != 的等价物是什么? - user1834642
@user1834642 - ! s.equals() - Stephen C

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接