Results 1 to 6 of 6
  1. #1
    mbw290 is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default trying to create an object

    I wish to store x years of atBats and hits for an object I created named player. In other words this object will store one name then an array element which will be atBats and an array element which is hits. I'm getting a null pointer exception...

    This is my object
    Java Code:
    import java.util.*;
    public class Player {
    
    	private String name;
    	private int atBats;
    	private int hits;
    	private int year;
    
    	public Player (String pName, int pAtBats, int pHits)
    	{
    		name =pName;
    		atBats=pAtBats;
    		hits=pHits;
    		
    	}
    	public int getHits()
    	{
    		return hits;
    	}
    	public int getAtBats()
    	{
    		return atBats;
    	}
    	public float getAverage(){
    		
    		return (float)hits/(float)atBats;
    	}
    	public float getYear(){
    		return year;
    	}
    
    		
    
    	public String reverseName ()
    	{
    		String first= " ";
    		String last = " ";
    		StringTokenizer st;
    		st=new StringTokenizer (name, " ");
    		if (st.countTokens() !=2){
    			System.exit(1);
    		}
    		while (st.hasMoreElements()){
    			first=st.nextToken();
    			last=st.nextToken();
    		}
    	return last + first;
    	}
    	}
    Then this is my main method

    Java Code:
    import java.io.*;
    public class mattPlayer {
    
    
    	 public static void main(String[] args)throws IOException
    	{
    		 int numHit[];
    		 int numAB[];		 
    		int j=0;
    		 String years;
    		 int numYears=0;
    		 int i=0;
    		 String hits;
    		 String atBats;
    		 Player jDoe [];
    		 jDoe=new Player [numYears];
    		 int nYear=0;
    		 
    		 //DecimalFormat formatter=new DecimalFormat (".000");
    		 System.out.print("Enter player name: ");
    		 BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
    			String name;
    		name=br.readLine();
    			
    			System.out.print("How many years do you have? : ");
    			years=br.readLine();
    			
    			try {
    				numYears=Integer.parseInt(years);
    				for (i=0;i<numYears;i++){
    					numHit=new int [numYears];
    					numAB=new int [numYears];
    					jDoe=new Player [numYears];
    					 System.out.print("Enter player hits: ");
    					 hits=br.readLine();
    							 try {
    								 numHit[i]=Integer.parseInt(hits);
    								 }
    							 catch (NumberFormatException e){
    								 System.exit(1);
    							 }
    							 
    							 System.out.print("Enter player at bats: ");
    							 atBats=br.readLine();
    									 try {
    										 numAB[i]=Integer.parseInt(atBats);
    										 }
    									 catch (NumberFormatException e){
    										 System.exit(1);
    									 }
    							  jDoe[i]=new Player(name,numAB[i],numHit[i]);
    							  
    				}
    			}
    							
    				
    				
    				catch (NumberFormatException e){
    					System.exit(1);
    				}
    				//System.out.print("enter year to retrieve (remember 0 is the first year you entered): ");
    				//String year=br.readLine();
    			//try {
    				//nYear=Integer.parseInt(year);
    			
    		//		System.out.println(jDoe[nYear].getAtBats());
    				
    			//}
    			//catch (NumberFormatException e){
    		
    			//}
    				for (j=0;j<numYears;j++){
    					System.out.println(jDoe[j].getAtBats());
    				}
    			
    
    			
    		
    				
    				
    			
    			
    				
    				
    				
    			
    			
    			
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    mbw290 is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: trying to create an object

    Exception in thread "main" java.lang.NullPointerException
    at mattPlayer.main(mattPlayer.java:73)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: trying to create an object

    You need to tell us or show us which line that is. You have a null variable on that line and without knowing which line it is you and us can't solve this.

    Hint: also make sure that you've constructed all arrays before using them.

  5. #5
    mbw290 is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: trying to create an object

    It's System.out.println(jDoe[j].getAtBats());

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: trying to create an object

    jDoe[j] is null.
    Now you need ot put some System.out.println("<some usueful text>") calls scattered around in there so you can see what the code is doing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. What is best object to create?
    By lam5442 in forum New To Java
    Replies: 1
    Last Post: 02-23-2011, 09:44 PM
  2. Create new Calendar object???
    By aliencc in forum New To Java
    Replies: 4
    Last Post: 01-17-2011, 01:19 PM
  3. Can we create object without new operator ?
    By rohitjava in forum New To Java
    Replies: 11
    Last Post: 09-08-2010, 05:26 AM
  4. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  5. create object
    By paul21 in forum New To Java
    Replies: 4
    Last Post: 03-07-2010, 07:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •