Results 1 to 6 of 6
Thread: trying to create an object
- 12-06-2012, 09:40 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
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
Then this is my main methodJava 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; } }
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()); } } }
-
Re: trying to create an object
Which line throws the NPE?
- 12-06-2012, 11:50 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: trying to create an object
Exception in thread "main" java.lang.NullPointerException
at mattPlayer.main(mattPlayer.java:73)
-
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.
- 12-07-2012, 02:27 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: trying to create an object
It's System.out.println(jDoe[j].getAtBats());
- 12-07-2012, 03:02 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
What is best object to create?
By lam5442 in forum New To JavaReplies: 1Last Post: 02-23-2011, 09:44 PM -
Create new Calendar object???
By aliencc in forum New To JavaReplies: 4Last Post: 01-17-2011, 01:19 PM -
Can we create object without new operator ?
By rohitjava in forum New To JavaReplies: 11Last Post: 09-08-2010, 05:26 AM -
Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 04:29 AM -
create object
By paul21 in forum New To JavaReplies: 4Last Post: 03-07-2010, 07:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks