皇族年会:请问下面这段代码中,哪里进行了空操作?

来源:百度文库 编辑:高考问答 时间:2024/05/01 00:59:35
import java.util.*;
import java.io.*;

public class CardList
{
private int count_Cards;
private final int MAX = 10;
private String s;
public Card cards[];
public CardList()
{
count_Cards = 0;
cards = new Card[MAX];
s="";
}
public int getCountCards()
{
return count_Cards;
}

public void readInput()
{
FileReader txtFile = null;
BufferedReader in = null;

try
{
txtFile = new FileReader("cards.txt");
in = new BufferedReader(txtFile);
while ((s = in.readLine()) !=null)
{
StringTokenizer t = new StringTokenizer(s," \t\n\r\f");
cards[count_Cards].setCode(Integer.parseInt(t.nextToken()));
cards[count_Cards].setCardName(t.nextToken());
cards[count_Cards].setPrice(Double.parseDouble(t.nextToken()));
count_Cards++;
}

}
catch(FileNotFoundException fnf)
{
System.out.println(fnf);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
in.close();
}
catch(IOException ioe){}
}

public static void main(String args[])
{
CardList asd = new CardList();
asd.readInput();
System.out.println(asd.cards[0].getCode());
}

}
是会提示有NullPointerException
并且已经有cards.txt
Card类是public的,已经进行了定义,同CardList类处于同一个根目录下

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

public class CardList
{
private int count_Cards;
private final int MAX = 10;
private String s;
public Card cards[];
public CardList()
{
cards = new Card[MAX];
count_Cards = 0;
for(int i=0;i<MAX;i++)
cards[i] = new Card();
}
public int getCountCards()
{
return count_Cards;
}

public void readInput()
{
FileReader txtFile = null;
BufferedReader in = null;

try
{
txtFile = new FileReader("cards.txt");
in = new BufferedReader(txtFile);
StringTokenizer t;
while ((s = in.readLine())!=null)
{
t = new StringTokenizer(s);
while(t.hasMoreTokens())
{
cards[count_Cards].setCode(Integer.parseInt(t.nextToken()));
cards[count_Cards].setCardName(t.nextToken());
cards[count_Cards].setPrice(Double.parseDouble(t.nextToken()));
}
System.out.println(cards[count_Cards].getCode());
count_Cards++;
}

}
catch(FileNotFoundException fnf)
{
System.out.println(fnf);
}
catch(IOException e)
{
System.out.println(e);
}
}

public static void main(String args[])
{
CardList asd = new CardList();
asd.readInput();
System.out.println(asd.cards[0].getCode());
}
/* public updateCardList(Sale sale)*/
}

请问你
public Card cards[];
是什么?
你自己定义的类吗?
为什么没有import进来?

空操作
you mean space?

maks sure you got file cards.txt
txtFile = new FileReader("cards.txt");