NullPointerException

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




}
}
 
A

Andrew Thompson

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

// 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>

--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
 
D

Daniel Pitts

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




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

Lew

Andrew said:
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).
 
L

Lew

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.*;

public class Baseball {

private int number, hits, walks, outs;

public Baseball() {
number = 0;

int instance variables are already initialized to zero without having to say so.
....

Daniel said:
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();
 
P

proudbug

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

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:");
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]);

}



}- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top