NullPointerException

Discussion in 'Java' started by ayusuf@gmail.com, Dec 10, 2007.

  1. Guest

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




    }
    }
    , Dec 10, 2007
    #1
    1. Advertising

  2. wrote:
    >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
    Andrew Thompson, Dec 10, 2007
    #2
    1. Advertising

  3. Daniel Pitts Guest

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


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Dec 10, 2007
    #3
  4. Lew Guest

    Andrew Thompson wrote:
    > 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).

    --
    Lew
    Lew, Dec 10, 2007
    #4
  5. Lew Guest

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

    >> hits = 0;
    >> walks = 0;
    >> outs = 0;
    >> }

    ....

    Daniel Pitts wrote:
    > 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();


    --
    Lew
    Lew, Dec 10, 2007
    #5
  6. Roedy Green Guest

    Roedy Green, Dec 10, 2007
    #6
  7. proudbug Guest

    On Dec 9, 8:56 pm, wrote:
    > 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 -
    proudbug, Dec 10, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon A. Cruz

    Re: NullPointerException Error??

    Jon A. Cruz, Jul 6, 2003, in forum: Java
    Replies:
    0
    Views:
    509
    Jon A. Cruz
    Jul 6, 2003
  2. Tohru Kao
    Replies:
    3
    Views:
    420
    Neil Masson
    Jul 14, 2003
  3. Tohru Kao
    Replies:
    1
    Views:
    380
    Chris
    Jul 8, 2003
  4. Dhek Bhun Kho
    Replies:
    0
    Views:
    2,226
    Dhek Bhun Kho
    Jul 9, 2003
  5. Tom
    Replies:
    12
    Views:
    8,027
    Chris Smith
    Aug 5, 2003
Loading...

Share This Page