Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.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 将字符串指定给从文件中提取的ArrayList_Java_Arraylist_Nullpointerexception - Fatal编程技术网

Java 将字符串指定给从文件中提取的ArrayList

Java 将字符串指定给从文件中提取的ArrayList,java,arraylist,nullpointerexception,Java,Arraylist,Nullpointerexception,我从文件中导入了一个字符串列表,并将它们放入数组列表。我试图切掉这些数组的末尾,所以我将它们放入一个单独的字符串格式中 这里是我设置x的地方 x = new ArrayList<BankAccounts>(); try { Scanner reader; reader = new Scanner( new File("F:\\myCreditUnion.txt") ); while ( reader.hasNext() ) { Str

我从文件中导入了一个
字符串列表
,并将它们放入
数组列表
。我试图切掉这些数组的末尾,所以我将它们放入一个单独的
字符串
格式中

这里是我设置x的地方

x = new ArrayList<BankAccounts>();
try {
    Scanner reader;
    reader = new Scanner( new File("F:\\myCreditUnion.txt") );

    while ( reader.hasNext() )
    {
        String inputLine = reader.nextLine();
        first = inputLine.substring(0, 3);
        second = Double.parseDouble(inputLine.substring(5, inputLine.length()));
        x.add(new BankAccounts(first, second));
    }

    reader.close(); 
}
x=newarraylist();
试一试{
扫描仪阅读器;
reader=新扫描仪(新文件(“F:\\myCreditUnion.txt”);
while(reader.hasNext())
{
字符串inputLine=reader.nextLine();
第一个=inputLine.substring(0,3);
second=Double.parseDouble(inputLine.substring(5,inputLine.length());
x、 增加(新银行账户(第一、第二);
}
reader.close();
}
这就是我试图砍掉末端的地方

double howmuch;
for(int i = 0; i < x.size(); i++)
{
     list.equals(x.get(i));
     howmuch = Double.parseDouble(list.substring(5, list.length()));
}
// x is the list
加倍多少;
对于(int i=0;i
我得到一个
空点异常
。不知道如何解决这个问题,因为我对编程非常陌生


我正在导入的文档包含#和字母的组合,例如101s 583.58

如果您在上述代码中得到NullPointerException,则
list
x
为null。检查设置其值的位置,或在问题中包括该部分代码。

要将值分配给
列表
,必须使用
list=x.get(i)而不是
list.equals(x.get(i))

更新:

  • equals()
    不赋值,它只检查两个对象是否相等

  • x.get(i)返回的值将是一个银行帐户,因此您无法将其分配给列表(这是一个字符串)

  • 您必须使用toString()将BankAccount转换为字符串,或者在将其分配给list之前通过调用BankAccount方法之一从中获取字符串,其工作方式取决于BankAccount类提供的方法


关于您的代码:您可能有类似的声明:
String line=null粘贴最后一段代码之前的某个地方,在
列表中得到
NullPointerException
。等于(x.get(i))行。这是因为您的
列表
对象未初始化。然而,你不需要这样做。见下文

要执行您想要的操作,应使用以下代码:

银行账户的类定义:

class BankAccounts {
   public String account; 
   public Double value;

   public BankAccounts(String account, Double value)
   {
      this.account = account;
      this.value = value;
   }
}
然后像这样重写代码:

List<BankAccounts> x = new ArrayList<BankAccounts>();
try {
    Scanner reader;
    reader = new Scanner( new File("F:\\myCreditUnion.txt") );

    while ( reader.hasNext() )
    {
        String inputLine = reader.nextLine();
        first = inputLine.substring(0, 3);
        second = Double.parseDouble(inputLine.substring(5, inputLine.length()));
        x.add(new BankAccounts(first, second));
    }

    reader.close(); 
}

@alb-Joe包含了代码,但被SO的文本解析器截断了。当你得到一个异常时,通常你也会得到一个堆栈跟踪,它告诉你异常发生在代码的哪一行。然后看看这一行中使用了哪些对象。如果它在您发布的代码段中,则
x
list
都是
null
@Joe-处理您的代码格式。保持所有内容都井井有条且间隔适当(我自己喜欢每个代码块缩进4个空格),如果可能的话,使用Eclipse或其他一些免费编辑器来帮助您保持代码整洁。然后,选择您的代码片段,然后单击编辑区域上方的
{}
(两个大括号)图标。这将正确格式化您的代码,以便我们阅读。您是否添加银行帐户并尝试获取字符串?即使没有空指针例外,这也不起作用。要详细说明吗?虽然它们是数字,但它们被作为字符串导入,但这两个字符串都有,所以如果我这样做,我会得到一个不兼容的类型错误哇,谢谢,这非常有用。当我做S.O.P(x.get(i))时,我的答案显示了内存中的位置,而不是实际位置,这有什么原因吗value@joe这是默认toString()实现的输出,因为BankAccount没有定义自己的toString(),所以不能直接将BankAccount转换为字符串。@joe再次查看您的代码,当你创建银行账户时,你不是已经拆分了结尾吗?那么我如何打印arraylist中的内容呢?哦,我应该澄清一下,我正在导入数字和字母。。我推断的是:)“表ABC 10.324的行”。但是在第二个循环中,当从文件中读取时,您只是尝试重新分析您解析的内容。O第二个循环是不同的访问器method@joe)没关系。。您已经在x列表对象中解析了正确的数据。
// x is the list    
double howmuch = 0;
for(int i = 0; i < x.size(); i++)
{
    BankAccounts accounts = x.get(i);
    howmuch = accounts.amount; // there is no need to cast since unboxing will occur here.
    // here howmuch will contain the amount for each account
}