这是我完成所有工作的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.lang.*;
public class NFLStat extends JPanel
{
static int count = 0;
static QB[] qb;
static JButton button;
static String s, st, str, stri, strin, string, strings, stringst, stringstr, stringstri, stringstrin, stringstring, string1;
public NFLStat()
{
button = new JButton("QB");
button.addActionListener(new Listener());
add(button);
Scanner infile = null;
try
{
infile = new Scanner(new File("Players")).useDelimiter("#");
}
catch(FileNotFoundException e)
{
JOptionPane.showMessageDialog(null, "Error: File not found.");
System.exit(0);
}
while(infile.hasNext())
{
qb[count] = new QB(infile.next(), infile.next(), infile.next(), infile.nextInt(), infile.nextInt(), infile.nextInt(), infile.nextInt(), infile.nextInt(), infile.nextInt(), infile.nextDouble(), infile.nextInt(), infile.nextInt(), infile.nextDouble(), infile.nextInt(), infile.nextInt(), infile.nextInt());
count++;
}
}
public static class Listener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame = new JFrame("NFL Stats");
JPanel panel = new JPanel();
frame.add(panel);
frame.setSize(1000,400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
for(int x = 0; x < count; x++)
{
s = Integer.toString(qb[x].getBye());
st = Integer.toString(qb[x].getPAtt());
str = Integer.toString(qb[x].getCmp());
stri = Integer.toString(qb[x].getPyd());
strin = Integer.toString(qb[x].getPtd());
string = Integer.toString(qb[x].getInter());
strings = Double.toString(qb[x].getRate());
stringst = Integer.toString(qb[x].getRAtt());
stringstr = Integer.toString(qb[x].getRyd());
stringstri = Double.toString(qb[x].getAvg());
stringstrin = Integer.toString(qb[x].getRtd());
stringstring = Integer.toString(qb[x].getFL());
string1 = Integer.toString(qb[x].getProj());
JLabel label = new JLabel(qb[x].getName() + "|" + qb[x].getTeam() + "|" + qb[x].getUpcoming() + "|" + s +"|" + st + "|" + str + "|" + stri +"|"+ strin + "|" + string +"|" + strings + "|" + stringst + "|" + stringstr + "|" + stringstri + "|" + stringstrin + "|" + stringstring + "|" + string1);
panel.add(label);
}
}
}
public static void main(String[] args) throws Exception
{
JFrame frame = new JFrame("NFL Stats");
frame.setSize(400,400);
frame.setLocation(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new NFLStat());
frame.setVisible(true);
}
}
这是我的 QB 类,所以我可以将它用于文本文档:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class QB
{
private String name;
private String team;
private String upcoming;
private int bye;
private int Patt;
private int cmp;
private int Pyd;
private int Ptd;
private int inter;
private double rate;
private int Ratt;
private int Ryd;
private int avg;
private int Rtd;
private int fl;
private int proj;
public QB()
{
name = "";
team = "";
upcoming = "";
bye = 0;
Patt = 0;
cmp = 0;
Pyd = 0;
Ptd = 0;
inter = 0;
rate = 0.0;
Ratt = 0;
Ryd = 0;
avg = 0;
Rtd = 0;
fl = 0;
proj = 0;
}
public QB(String n, String t, String u, int b, int p, int c, int y, int td, int i, double rat, int r, int yd, int a, int tdr, int f, int pro)
{
name =n;
team = t;
upcoming = u;
bye = b;
Patt = p;
cmp = c;
Pyd = y;
Ptd = td;
inter = i;
rate = rat;
Ratt = r;
Ryd = yd;
avg = a;
Rtd = tdr;
fl = f;
proj = pro;
}
//Get methods
public String getName()
{
return name;
}
public String getTeam()
{
return team;
}
public String getUpcoming()
{
return upcoming;
}
public int getBye()
{
return bye;
}
public int getPAtt()
{
return Patt;
}
public int getCmp()
{
return cmp;
}
public int getPyd()
{
return Pyd;
}
public int getPtd()
{
return Ptd;
}
public int getInter()
{
return inter;
}
public double getRate()
{
return rate;
}
public int getRAtt()
{
return Ratt;
}
public int getRyd()
{
return Ryd;
}
public int getAvg()
{
return avg;
}
public int getRtd()
{
return Rtd;
}
public int getFL()
{
return fl;
}
public int getProj()
{
return proj;
}
}
这是我的球员文本文件
Smith, Alex#KC#@Den#10#315#188#1919#9#4#81.4#53#265#5.0#1#0#145#Tannehill, Ryan#Mia#SD#6#331#202#2206#13#10#81.2#23#85#3.7#1#5#142#Bradford, Sam#STL#BYE#11#262#159#1687#14#4#90.9#15#31#2.1#0#1#142#
该程序可以编译,但是当我尝试运行它时,我不断收到此错误:
Exception in thread "main" java.lang.NullPointerException
at NFLStat.<init>(NFLStat.java:35)
at NFLStat.main(NFLStat.java:76)
有人可以帮我找出问题所在吗?