0

这是我完成所有工作的代码:

 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)

有人可以帮我找出问题所在吗?

4

2 回答 2

2

感谢您发布的所有代码。我运行了几次并进行了更改,并找到了空指针异常的原因。

您在您的字段中声明:

static QB[] qb;

然后尝试将东西放入这个数组中:

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());

但是,您从未真正初始化/创建此数组。你需要这样的声明:

    qb = new QB[3];

但是当然,您希望该数组中的 # 始终与您尝试放入其中的内容相同或更多,否则您将获得空指针异常。

你有最大数量的玩家可以放入一个数组吗?使用那个号码。或者限制 while 循环触发的次数,例如:

 while(infile.hasNext() && count < 20)

希望有帮助!:-)

克尔斯滕

PS您发布的代码没有立即为我编译,因为您的最终 infile.nextDouble() 正在输入一个期望 int 的构造函数。我假设这是一个疏忽,正如您所说的那样,您已经编译了,并且您可能在更改文件之间的某些内容之前已经发布了。

于 2013-11-15T02:49:44.147 回答
0

我不认为你应该这样做:

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());

尝试类似的东西

while (infile.hasnext())
    qb[i++] = (i == 9 || i = 12) ? infile.nextDouble() : infile.nextInt() ;

i在这种情况下,不要忘记声明一个计数器。

编辑:还考虑保留一个字符串数组而不是声明 20 个字符串名称。您将节省大量循环它们的时间,并且它提供了更多的灵活性。

于 2013-11-15T02:34:58.887 回答