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]);
}
}
Andrew Thompson - 10 Dec 2007 02:36 GMT
>Everytime i compile i get no errors but when i run it and hit enter I
>get this.
[quoted text clipped - 3 lines]
>
>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];
// we have created a 'space' for the Baseball's
// buit they do not exist yet!
for (int ii=0; ii<20; ii++) {
player[ii] = new Baseball();
}
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();
}
}
}
Please note that newsreaders typically wrap text at
around 72 chars. I did not rewrap this text - to show
you what effect that has.
Please keep code lines short. Here is a tool that helps chack
line width <http://www.physci.org/twc.jnlp>

Signature
Andrew Thompson
http://www.physci.org/
Lew - 10 Dec 2007 03:22 GMT
> Please note that newsreaders typically wrap text at
> around 72 chars. I did not rewrap this text - to show
> you what effect that has.
>
> Please keep code lines short. Here is a tool that helps chack
> line width <http://www.physci.org/twc.jnlp>
To the OP: this would be a LOT easier if you didn't embed TABs in Usenet
listings. Use spaces to indent, and keep it short (2, 3, or 4 spaces).

Signature
Lew
Daniel Pitts - 10 Dec 2007 02:36 GMT
> Everytime i compile i get no errors but when i run it and hit enter I
> get this.
[quoted text clipped - 57 lines]
> }
> }
Baseball[] player =new Baseball[20]; creates 20 variables that can hold
a Baseball object. It doesn't create 20 Baseball objects.
in your while loop, you need to add
player[gameCounter] = new Baseball();

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Lew - 10 Dec 2007 03:24 GMT
ayusuf@gmail.com wrote:
>> Everytime i [sic] compile i [sic] get no errors but when i [sic] run it and hit enter I
>> get this.
>>
>> Exception in thread "main" java.lang.NullPointerException
>> at Baseball.main(Baseball.java:37)
>> import java.util.*;
>>
[quoted text clipped - 4 lines]
>> public Baseball() {
>> number = 0;
int instance variables are already initialized to zero without having to say so.
>> hits = 0;
>> walks = 0;
>> outs = 0;
>> }
...
> Baseball[] player =new Baseball[20]; creates 20 variables that can hold
> a Baseball object. It doesn't create 20 Baseball objects.
> in your while loop, you need to add
>
> player[gameCounter] = new Baseball();

Signature
Lew
Roedy Green - 10 Dec 2007 11:18 GMT
>NullPointerException
see
http://mindprod.com/jgloss/runerrormessages.html#NULLPOINTEREXCEPTION

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
proudbug - 10 Dec 2007 17:18 GMT
On Dec 9, 8:56 pm, ayu...@gmail.com wrote:
> Everytime i compile i get no errors but when i run it and hit enter I
> get this.
[quoted text clipped - 39 lines]
>
> while(gameCounter <= 20){
gameCounter should go from 0 to 19 (inclusive) for an array of 20
Baseball elements. Yours is from 1 to 20. Will get
ArrayOutOfBoundException at runtime even after you fix the null
pointer bug.
> System.out.print("For game " + gameCounter + ", enter the player
> number:");
[quoted text clipped - 17 lines]
>
> - Show quoted text -