I am trying to create an applet for people to enter information, and the program should return a certain stat.
However, after the user inputs all of the necessary information, no data is returned but this error message is:
Exception in thread "main" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:991)
at java.lang.Double.parseDouble(Double.java:510)
at Player.toDouble(Player.java:110)
at Player.numSingles(Player.java:115)
at Player.clutchFactor(Player.java:122)
at Test.main(Test.java:19)
The program successfully returns a double when I input data, but it won't return when the user takes the data.
import java.io.*;
public class Test {
public static void main (String[] args) throws IOException
{
System.out.println("Go to sports.yahoo.com/mlb/ and look for situational stats");
Player a= new Player ("2800", "33", "2", "23", "2", "51", "1", "3", "1", "345");
Player bigGuy = new Player ();
System.out.println(a.clutchFactor());
System.out.println(bigGuy.clutchFactor());
}
}
import java.io.*;
import java.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Player {
public String AB;
public String HR;
public String K;
public String BB;
public String hits;
public String dub;
public String trip;
public String SB;
public String CS;
public String OBP;
public Player(String a, String h, String k, String b, String hi, String d, String t, String sb, String cs, String obp)
{
this.AB = a;
this.HR = h;
this.K = k;
this.BB = b;
this.hits = hi;
this.dub = d;
this.trip = t;
this.SB = sb;
this.CS = cs;
this.OBP = obp;
}
public Player() throws IOException {
//the data that will be entered by the user
String AB;
String hits;
String dub;
String trip;
String HR;
String BB;
String K;
String SB;
String CS;
String OBP;
//what the program will ask to be entered
StringReader sreader = new StringReader("check");
BufferedReader reader = new BufferedReader(sreader);
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Number of At Bats ");
AB = reader.readLine();
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Number of hits ");
hits = reader.readLine();
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Number of doubles ");
dub = reader.readLine();
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Number of triples ");
trip = reader.readLine();
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Number of home runs ");
HR = reader.readLine();
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Number of walks ");
BB = reader.readLine();
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Number of strikeouts ");
K = reader.readLine();
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Number of Steals ");
SB = reader.readLine();
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Number of times caught stolen ");
CS = reader.readLine();
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("On base percentage ");
OBP = reader.readLine();
}
//methods
public static double toDouble(String a)
{
return Double.parseDouble(a);
}
public Double numSingles()
{
return toDouble(this.hits)-(toDouble(this.trip)+toDouble(this.dub)+toDouble(this.HR));
}
public String clutchFactor()
{
if (1>0) //modify this to be late game and true)
{
double cfNum = (numSingles()+2*toDouble(this.dub)+3*toDouble(this.trip)+4*toDouble(this.HR)+(toDouble(this.BB)/2));
double cfDenom = ((toDouble(this.OBP) + 1)/1000) *(2*toDouble(this.K)+1);
double almost = 1000*(cfNum/cfDenom)*((toDouble(this.SB)+1)/(toDouble(this.SB)+toDouble(this.CS)+1));
return ("" + almost/toDouble(this.AB));
}
else
{
return "Stats must be entered from splits in clutch situations, ie 7th inning and on w/ the score w/in 3";
}
}
}