A
ayusuf
Everytime i compile i get no errors but when i run it and hit enter I
get this.
Exception in thread "main" java.lang.NullPointerException
at Baseball.main(Baseball.java:37)
How would i fix this?
import java.util.*;
/**
* @(#)Baseball.java
*
*
* @author
* @version 1.00 2007/12/9
*/
public class Baseball {
private int number, hits, walks, outs;
public Baseball() {
number = 0;
hits = 0;
walks = 0;
outs = 0;
}
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Baseball[] player = new Baseball[20];
int gameCounter = 1;
System.out.println("This program asks the user for a baseball
player's number\n"
+ "and their number of hits, walks, and outs for multiple games.
\n" +
"Only nine players bat each game.\n\n");
while(gameCounter <= 20){
System.out.print("For game " + gameCounter + ", enter the player
number:");
player[gameCounter].number = console.nextInt();
System.out.print("Enter the hits for game " + gameCounter + " for
player " + gameCounter);
player[gameCounter].hits = console.nextInt();
System.out.print("Enter the walks for game " + gameCounter + "
for player " + gameCounter);
player[gameCounter].walks = console.nextInt();
System.out.print("Enter the outs for game " + gameCounter + " for
player " + gameCounter);
player[gameCounter].outs = console.nextInt();
}
//System.out.println(player[1]);
}
}
get this.
Exception in thread "main" java.lang.NullPointerException
at Baseball.main(Baseball.java:37)
How would i fix this?
import java.util.*;
/**
* @(#)Baseball.java
*
*
* @author
* @version 1.00 2007/12/9
*/
public class Baseball {
private int number, hits, walks, outs;
public Baseball() {
number = 0;
hits = 0;
walks = 0;
outs = 0;
}
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Baseball[] player = new Baseball[20];
int gameCounter = 1;
System.out.println("This program asks the user for a baseball
player's number\n"
+ "and their number of hits, walks, and outs for multiple games.
\n" +
"Only nine players bat each game.\n\n");
while(gameCounter <= 20){
System.out.print("For game " + gameCounter + ", enter the player
number:");
player[gameCounter].number = console.nextInt();
System.out.print("Enter the hits for game " + gameCounter + " for
player " + gameCounter);
player[gameCounter].hits = console.nextInt();
System.out.print("Enter the walks for game " + gameCounter + "
for player " + gameCounter);
player[gameCounter].walks = console.nextInt();
System.out.print("Enter the outs for game " + gameCounter + " for
player " + gameCounter);
player[gameCounter].outs = console.nextInt();
}
//System.out.println(player[1]);
}
}