indexOf(), arrayList 问题 java
indexOf(), arrayList issue java
大家好,
我目前正在开发一个 Java 程序,该程序下载文件、读取文件并将其数据复制到不同的 arrayList 中。然后我只想得到一些数据的索引,用"indexOf()"。
我的 main 没有问题:
import java.util.GregorianCalendar;
import yhhFin.StockDownloader;
import java.util.GregorianCalendar;
import java.util.ArrayList;
import java.util.Calendar;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.io.*;
public class YhhFin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //declaration du Scanner
String track = "AAPL";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
int moisAJD = localDate.getMonthValue();
int anneeAJD = localDate.getYear();
int jourAJD =localDate.getDayOfMonth();
GregorianCalendar end = new GregorianCalendar ( anneeAJD, moisAJD, jourAJD);
GregorianCalendar start = new GregorianCalendar(2011, 11, 11);
StockDownloader test = new StockDownloader(track, start, end);
System.out.println("Quel prix?");
double prix = sc.nextDouble();
StockDownloader tro = new StockDownloader(prix) ;
}
}
然后我的另一个 class 包含方法 StockDownloader(prix) => 问题
package yhhFin;
public class StockDownloader
{
public static final int DATE = 0;
public static final int OPEN = 1;
public static final int HIGH = 2;
public static final int LOW = 3;
public static final int CLOSE = 4;
public static final int VOLUME = 5;
public static final int ADJCLOSE = 6;
private ArrayList<GregorianCalendar> dates;
private ArrayList<Double> open;
private ArrayList<Double> high;
private ArrayList<Double> low;
private ArrayList<Double> close;
private ArrayList<Integer> volume;
private ArrayList<Double> adjclose;
public StockDownloader (String symbol, GregorianCalendar start, GregorianCalendar end)
{
dates= new ArrayList<GregorianCalendar>();
open= new ArrayList<Double>();
high= new ArrayList<Double>();
low= new ArrayList<Double>();
close= new ArrayList<Double>();
volume= new ArrayList<Integer>();
adjclose= new ArrayList<Double>();
// http://chart.finance.yahoo.com/table.csv?s=IBM&a=0&b=2&c=1962&d=3&e=12&f=2017&g=d&ignore=.csv
String url = "http://chart.finance.yahoo.com/table.csv?s="+symbol+"&a="+start.get(Calendar.MONTH)+
"&b="+ start.get(Calendar.DAY_OF_MONTH)+
"&c="+start.get(Calendar.YEAR)+
"&d="+end.get(Calendar.MONTH)+
"&e="+end.get(Calendar.DAY_OF_MONTH)+
"&f="+end.get(Calendar.YEAR)+
"&g=d&ignore=.csv";
try
{
URL yhoofin = new URL (url);
URLConnection data = yhoofin.openConnection();
Scanner input = new Scanner(data.getInputStream());
if(input.hasNext()) //skip header
input.nextLine();
while(input.hasNextLine())
{
String line = input.nextLine();
String st[]=line.split(",");
for(int i = 0; i < st.length; i++)
{
DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
Date date = df.parse(st[0]);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
dates.add((GregorianCalendar) cal);
double valOpen = Double.parseDouble(st[1]);
open.add(valOpen);
//System.out.println(open.get(i));
double valHigh = Double.parseDouble(st[2]);
high.add(valHigh);
double valLow = Double.parseDouble(st[3]);
low.add(valLow);
double valClose = Double.parseDouble(st[4]);
close.add(valClose);
int valvolume = Integer.parseInt(st[5]);
volume.add(valvolume);
double adjClose = Double.parseDouble(st[6]);
adjclose.add(adjClose);
}
}
}
catch (Exception e)
{
System.err.println(e);
}
}
//the important method
public StockDownloader (double price) {
int index;
index= open.indexOf(price);//the code crash when reaching this line
System.out.println("INDEX : " +index);
}
}
我尽量做到明确,这不是范围问题,因为数组是在 class 中声明的,所以我可以访问它们。
我在这个问题上苦苦挣扎了几天,能否请您赐教。
非常感谢。
Vik55
PS: 我做了一些研究,我看到很多 post 与此类似但无法解决我的问题,显然我可能有声明问题,我不太明白.
你有两个构造函数。一个初始化数组列表,另一个不初始化。当然,你的所有数组列表在构造函数中都是 NULL,它不会初始化它们,所以当它试图访问其中一个时,它会因空指针异常而失败。
这是一个非常基本的错误,所有体面的编译器都会对此发出警告。所以:
了解如何在您的开发环境中启用警告。
启用所有警告。
关注他们
index= open.indexOf(price);//the code crash when reaching this line
它崩溃是因为 open 指向 null,也就是它没有初始化。
你应该添加
open= new ArrayList<Double>();
就像你对另一个构造函数所做的那样。
大家好,
我目前正在开发一个 Java 程序,该程序下载文件、读取文件并将其数据复制到不同的 arrayList 中。然后我只想得到一些数据的索引,用"indexOf()"。
我的 main 没有问题:
import java.util.GregorianCalendar;
import yhhFin.StockDownloader;
import java.util.GregorianCalendar;
import java.util.ArrayList;
import java.util.Calendar;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.io.*;
public class YhhFin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //declaration du Scanner
String track = "AAPL";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
int moisAJD = localDate.getMonthValue();
int anneeAJD = localDate.getYear();
int jourAJD =localDate.getDayOfMonth();
GregorianCalendar end = new GregorianCalendar ( anneeAJD, moisAJD, jourAJD);
GregorianCalendar start = new GregorianCalendar(2011, 11, 11);
StockDownloader test = new StockDownloader(track, start, end);
System.out.println("Quel prix?");
double prix = sc.nextDouble();
StockDownloader tro = new StockDownloader(prix) ;
}
}
然后我的另一个 class 包含方法 StockDownloader(prix) => 问题
package yhhFin;
public class StockDownloader
{
public static final int DATE = 0;
public static final int OPEN = 1;
public static final int HIGH = 2;
public static final int LOW = 3;
public static final int CLOSE = 4;
public static final int VOLUME = 5;
public static final int ADJCLOSE = 6;
private ArrayList<GregorianCalendar> dates;
private ArrayList<Double> open;
private ArrayList<Double> high;
private ArrayList<Double> low;
private ArrayList<Double> close;
private ArrayList<Integer> volume;
private ArrayList<Double> adjclose;
public StockDownloader (String symbol, GregorianCalendar start, GregorianCalendar end)
{
dates= new ArrayList<GregorianCalendar>();
open= new ArrayList<Double>();
high= new ArrayList<Double>();
low= new ArrayList<Double>();
close= new ArrayList<Double>();
volume= new ArrayList<Integer>();
adjclose= new ArrayList<Double>();
// http://chart.finance.yahoo.com/table.csv?s=IBM&a=0&b=2&c=1962&d=3&e=12&f=2017&g=d&ignore=.csv
String url = "http://chart.finance.yahoo.com/table.csv?s="+symbol+"&a="+start.get(Calendar.MONTH)+
"&b="+ start.get(Calendar.DAY_OF_MONTH)+
"&c="+start.get(Calendar.YEAR)+
"&d="+end.get(Calendar.MONTH)+
"&e="+end.get(Calendar.DAY_OF_MONTH)+
"&f="+end.get(Calendar.YEAR)+
"&g=d&ignore=.csv";
try
{
URL yhoofin = new URL (url);
URLConnection data = yhoofin.openConnection();
Scanner input = new Scanner(data.getInputStream());
if(input.hasNext()) //skip header
input.nextLine();
while(input.hasNextLine())
{
String line = input.nextLine();
String st[]=line.split(",");
for(int i = 0; i < st.length; i++)
{
DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
Date date = df.parse(st[0]);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
dates.add((GregorianCalendar) cal);
double valOpen = Double.parseDouble(st[1]);
open.add(valOpen);
//System.out.println(open.get(i));
double valHigh = Double.parseDouble(st[2]);
high.add(valHigh);
double valLow = Double.parseDouble(st[3]);
low.add(valLow);
double valClose = Double.parseDouble(st[4]);
close.add(valClose);
int valvolume = Integer.parseInt(st[5]);
volume.add(valvolume);
double adjClose = Double.parseDouble(st[6]);
adjclose.add(adjClose);
}
}
}
catch (Exception e)
{
System.err.println(e);
}
}
//the important method
public StockDownloader (double price) {
int index;
index= open.indexOf(price);//the code crash when reaching this line
System.out.println("INDEX : " +index);
}
}
我尽量做到明确,这不是范围问题,因为数组是在 class 中声明的,所以我可以访问它们。
我在这个问题上苦苦挣扎了几天,能否请您赐教。
非常感谢。
Vik55
PS: 我做了一些研究,我看到很多 post 与此类似但无法解决我的问题,显然我可能有声明问题,我不太明白.
你有两个构造函数。一个初始化数组列表,另一个不初始化。当然,你的所有数组列表在构造函数中都是 NULL,它不会初始化它们,所以当它试图访问其中一个时,它会因空指针异常而失败。
这是一个非常基本的错误,所有体面的编译器都会对此发出警告。所以:
了解如何在您的开发环境中启用警告。
启用所有警告。
关注他们
index= open.indexOf(price);//the code crash when reaching this line
它崩溃是因为 open 指向 null,也就是它没有初始化。 你应该添加
open= new ArrayList<Double>();
就像你对另一个构造函数所做的那样。