public class Yearly {
public void getYearlyData() throws FileNotFoundException {
String[] state = StatesList.getValues();
int numberOfStates = state.length;
我有一个数组:String[] state
,其中包含用户指定的状态的名称。
它可能看起来像这样:String[] state = {"Illinois", "Kansas", "Wyoming"};
下面是二维的锯齿状数组。每个元素的第一个子元素是州名,然后是该州的报纸名称。
String statePapersInitial[][] = {
{ "Alabama", "BGNB"},
{ "Alaska", "ADNB", "ALKP"},
{ "Arizona", "ADSB" },
..................//Total = 51.................
{ "Wyoming","WTEB", "WYOM", "WMPP"};
目标是创建一个新的锯齿状数组,该数组将仅statePapersInitial
包含数组中包含用户在state
数组中选择的状态名称的那些元素。
我调用了新数组statePapers
我尝试了以下方法:
String[][] statePapers = null;
int x = 0;
for (x = 0; x < state.length; x++)
{
int i = 0;
for (i = 0; i < statePapersInitial.length; i++)
{
if (state[x].equals(statePapersInitial[i][0]))
{
int j = 1;
for (j = 1; j < statePapersInitial[i].length; j++)
statePapers[x][j-1] = statePapersInitial[i][j];
System.out.println(statePapers[x][j-1]);
}
else;
}
}
但是,它不起作用,它在线程“main”中引发异常
java.lang.NullPointerException
at Yearly.getYearlyData(Yearly.java:95)
at Main.main(Main.java:9)