0

This is my code that does all the work:

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

}
}

This is my QB class so I can use it for the text document:

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;
 }
}

And this is my Players text document

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#

The program compiles, but I keep getting this error when I try to run it:

Exception in thread "main" java.lang.NullPointerException
at NFLStat.<init>(NFLStat.java:35)
at NFLStat.main(NFLStat.java:76)

Can someone help me figure out what's wrong?

1
  • Please label the exact line where NFLStat.java line 35 is. Also, gotta be blunt; s, st, etc. are terrible variable names in any context. Use byeString, pattString, etc. instead. Commented Nov 15, 2013 at 2:10

2 Answers 2

2

Thanks for all of the code you posted. I ran it a few times with alterations and found the reason for the null pointer exception.

You declare in your fields:

static QB[] qb;

and then attempt to put things into this array:

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

However, you never actually initialize/create this array. You'll need a statement like:

    qb = new QB[3];

But of course you want the # inside of this array to always be the same or more than what you're attempting to put into it or you'll get a null pointer exception.

Do you have a maximum number of players that can be put into an array? Use that number. Or limit the number of times your while loop fires such as:

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

Hope that helps! :-)

Kirsten

P.S. The code you posted didn't compile for me immediately because your final infile.nextDouble() was feeding into a constructor that was expecting an int. I'm assuming that was an oversight as you said yours compiled and you might have posted before you changed something around between the files.

Sign up to request clarification or add additional context in comments.

1 Comment

I posted a functional fix but should elaborate that some of the other things that have been said are good advice. Creating a constructor with as many similar parameters as you have is usually a good indicator that there is a cleaner way to accomplish what you want and it will also be easier to debug. (An array of strings passed to a constructor instead of 5+ strings is a good example.)
0

I don't think you're supposed to do that:

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

Try something like

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

Don't forget to declare a counter, i in this case.

Edit: Also consider keeping an array of strings rather than declaring 20 string names. You'll save much time looping over them and it offers much more flexibility.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.