Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

NullPointerException

Thread view: 
ayusuf@gmail.com - 10 Dec 2007 01:56 GMT
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 -


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.