线程“main”java.util.InputMismatchException中的异常:使用scanner

时间:2013-11-15 02:03:45

标签: java nullpointerexception

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

 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)

有人可以帮我弄清楚出了什么问题吗?

2 个答案:

答案 0 :(得分: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];

但是你当然希望这个数组的#inside总是与你试图放入它的内容相同或更多,否则你会得到一个空指针异常。

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

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

希望有所帮助! : - )

P.S。您发布的代码没有立即为我编译,因为您的最终infile.nextDouble()正在进入一个期望int的构造函数。我假设这是一个疏忽,因为你说你的编译,你可能已经发布之前你改变文件之间的东西。

答案 1 :(得分: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个字符串名称。你可以节省很多时间循环它们,它提供了更大的灵活性。