Results 1 to 20 of 23
- 03-31-2011, 11:38 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
output giving null, methods not reading properly
Whenever I try to run the program, its giving me null for my scoresheet and 0 for my score. And my toString() is returing null but my System.out.println(l.getName()); and System.out.println(l.getGame()); are returning the proper strings. But i need to have my score and scoresheet to read properly. Thanks for the help.
PHP Code:import java.lang.*; import java.util.*; import java.io.*; import java.io.File; import java.io.FileReader; import java.util.Scanner; public class Bowler{ private int [] pins; private String name, game, scoresheet; private int score; private Bowler(){ pins = new int[16]; } public Bowler(String name, String game, String f){ this.name = name; this.game = game; String filename = f; try{ Scanner s = new Scanner(new FileReader("bowl.txt")); int i = 0; while(s.hasNextLine()){ pins[i] = s.nextInt(); i++; System.out.println(pins[i]); } s.close(); }catch(Exception e){e.printStackTrace();} } private void computeScoresheet(){ int i = 0; for(int frame = 1; frame < 10; frame++) // frames 1-9 { if(pins[i] == 10) { scoresheet += "X"; i++; // observed one element } else if(pins[i] + pins[i+1] == 10) { scoresheet += pins[i] + "/"; i+= 2; //observed two elements } else if(pins[i] != 0) { scoresheet += pins[i]; i++; } else{ scoresheet += "-"; } } } private void computeScore(){ score = 0; int i, j; int lastFrameIndex = 10; for(i = 0 ; i < lastFrameIndex; i++){ if(scoresheet.charAt(i) < 'X'){ score += pins[i]; for(j = 1; j < 3; j++) if((i+j)<scoresheet.length()){ score += pins[i+j]; } else if(scoresheet.charAt(i) < 10){ score += pins[i]; if(i+1 < scoresheet.length()) score += pins[i+1]; }} for(i = lastFrameIndex; i < scoresheet.length(); i++) score += pins[i]; } } public String getName(){ try{ Scanner s = new Scanner(new FileReader("bowl.txt")); name = s.nextLine(); s.close(); }catch(Exception e){e.printStackTrace();} return name ; } public String getGame(){ try{ Scanner s = new Scanner(new FileReader("bowl.txt")); s.nextLine(); game = s.nextLine(); s.close(); }catch(Exception e){e.printStackTrace();} return game ; } public String getScoresheet(){ this.computeScoresheet(); return this.scoresheet; } public int getScore(){ this.computeScore(); return this.score; } public String toString(){ return name+" "+ game; } public boolean equals(Object o){ return true; } public static void main(String[] args) { Bowler l = new Bowler(); System.out.println(l.toString()); System.out.println(l.getName()); System.out.println(l.getGame()); } }
- 03-31-2011, 11:42 PM #2
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Post your errors?
- 03-31-2011, 11:50 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
well i fixed what you had put, it was supposed to be a == not a < but, then when i try in my main method use System.out.println(l.toString()); System.out.println(l.getScore()); its giving me the following error.
PHP Code:null java.lang.NullPointerException at Bowler.computeScore(Bowler.java:74) at Bowler.getScore(Bowler.java:116) at Bowler.main(Bowler.java:134) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
- 04-01-2011, 12:20 AM #4
The code you posted never calls the getScore or computeScore methods so it does not match the error you are getting. If you want help then you need to provide relevant information. We don't read minds.
Having said that I will have to gaze into my crystal ball and guess. You have 2 constructors: the default no argument constructor (why is it private?) and the constructor that takes 3 String arguments. Your pins array is only initialised in the default constructor and not in the other constructor. So if you create a Bowler object with the 3 String constructor then your pins array will still be null.
- 04-01-2011, 12:36 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Sorry about the other thread and not giving relevant info, first time on here. Here's the code that got the error that i already posted even when I initialize under the calss. The default constructor is private because thats the way that i was told to create it. What would be the easiest way to be able to have my pins array be called in the other methods I have and to get an answer.
Java Code:import java.lang.*; import java.util.*; import java.io.*; import java.io.File; import java.io.FileReader; import java.util.Scanner; public class Bowler{ private int [] pins= new int[16]; private String name, game, scoresheet; private int score; private Bowler(){ } public Bowler(String name, String game, String f){ this.name = name; this.game = game; String filename = f; try{ Scanner s = new Scanner(new FileReader("bowl.txt")); int i = 0; while(s.hasNextLine()){ pins[i] = s.nextInt(); i++; System.out.println(pins[i]); } s.close(); }catch(Exception e){e.printStackTrace();} } private void computeScoresheet(){ int i = 0; for(int frame = 1; frame < 10; frame++) // frames 1-9 { if(pins[i] == 10) { scoresheet += "X"; i++; // observed one element } else if(pins[i] + pins[i+1] == 10) { scoresheet += pins[i] + "/"; i+= 2; //observed two elements } else if(pins[i] != 0) { scoresheet += pins[i]; i++; } else{ scoresheet += "-"; } } } private void computeScore(){ score = 0; int i, j; int lastFrameIndex = 10; for(i = 0 ; i < lastFrameIndex; i++){ if(scoresheet.charAt(i) == 'X'){ score += pins[i]; for(j = 1; j < 3; j++) if((i+j)<scoresheet.length()){ score += pins[i+j]; } else if(scoresheet.charAt(i) < 10){ score += pins[i]; if(i+1 < scoresheet.length()) score += pins[i+1]; }} for(i = lastFrameIndex; i < scoresheet.length(); i++) score += pins[i]; } } public String getName(){ try{ Scanner s = new Scanner(new FileReader("bowl.txt")); name = s.nextLine(); s.close(); }catch(Exception e){e.printStackTrace();} return name ; } public String getGame(){ try{ Scanner s = new Scanner(new FileReader("bowl.txt")); s.nextLine(); game = s.nextLine(); s.close(); }catch(Exception e){e.printStackTrace();} return game ; } public String getScoresheet(){ this.computeScoresheet(); return this.scoresheet; } public int getScore(){ this.computeScore(); return this.score; } public String toString(){ return name; } public boolean equals(Object o){ return true; } public static void main(String[] args) { Bowler l = new Bowler(); System.out.println(l.toString()); System.out.println(l.getName()); System.out.println(l.getGame()); System.out.println(l.getScore()); } }
- 04-01-2011, 12:43 AM #6
You are creating the Bowler object with the default constructor. All the code in the other constructor is never executed. You need to take a step back and think about what your code needs to do.
- 04-01-2011, 12:50 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
I understand what my code needs to do which is the following:
Operation Notes
+ Bowler( name : String,
game : String, filename :
String )
· set instance data accordingly
· read the integers from the file denoted by filename and store
into the pins array
· set the scoresheet and score by calling the helper methods
computeScoresheet and computeScore, respectively
- computeScoresheet( ) : void
· create the bowling scoresheet that represents the pins array
(see discussion below) ; set this result to the instance variable
scoresheet
- computeScore( ) : void
· find the score for the particular bowling game (see discussion
below and Algorithm 1) ; set this result to the instance
variable score
+ getName( ) : String
+ getGame( ) : String
+ getScoresheet( ) : String
+ getScore( ) : int
· return the appropriate instance data
+ toString( ) : String · return the String representation similar to Figure 5
+ equals( o : Object ) :
boolean
· return true if each element of the pins array in “o”
corresponds to the same element in the pins array of “this”
· return false otherwise
+ main( args : String[ ] ) :
static void
· instantiate and utilize a Bowler object to sufficiently test the
functionality
In saying what I know what to do, its the execution of each part that is messing me up. I have all the parts that I need its the fact that they arent running properly hence the nullpointerexception that I'm getting is the issue. Im supposed to initialize the array where I have it, i just am not understanding the fact why im getting that error.
- 04-01-2011, 12:57 AM #8
Obviously you don't otherwise your code would be working.
As I explained above your code never does this.Operation Notes
+ Bowler( name : String,
game : String, filename :
String )
· set instance data accordingly
· read the integers from the file denoted by filename and store
into the pins array
- 04-01-2011, 01:14 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
in my code, it is reading the integers from the file isn't it? I know i dont have it from the filename, but I'm not that worried about that part.
- 04-01-2011, 01:17 AM #10
-
- 04-01-2011, 01:25 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Pretty much all of it to be honest, I am confused by how my other constructors are not being exectued. If you could clarify that for me it would be greatly appreciated.
- 04-01-2011, 01:26 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
- 04-01-2011, 01:29 AM #14
:headdesk:
Maybe if I shout you will understand.
YES YOU HAVE WRITTEN THE CODE TO DO THAT BUT IT IS NEVER EXECUTED
-
That code is in the constructor that you never call. You call the default constructor in the main method, and the default constructor (the one without any parameters), has none of this code above:
(as has already been explained by junky)Java Code:public static void main(String[] args) { Bowler l = new Bowler();
- 04-01-2011, 01:33 AM #16
This creates a new Bowler object by calling the default constructor. Only one constructor will ever be called by the new operator. All your code in the other constructor is not executed because it is never called.Java Code:Bowler l = new Bowler();
- 04-01-2011, 01:33 AM #17
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
I understand that they're not being executed. I just dont understand how they arent being executed. Like what would I need to do for them to be executed, such as where would they have to be called in order it to work. Sorry for the trouble. I don't know what else to do. Pretty much all I need to know is how they can be executed properly.
- 04-01-2011, 01:35 AM #18
Why don't you create the Bowler object with the other constructor instead. Is it really that hard to understand?
Java Code:Bowler l = new Bowler("Fred", "Blah", "bowl.txt");
- 04-01-2011, 01:35 AM #19
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Now i think i understand, Im calling the wrong constructor when I need to be calling the other Bowler constructor? Sorry for the confusion i just wasnt catching on to what you were saying.
- 04-01-2011, 01:38 AM #20
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Similar Threads
-
Proplem with reading txt-file into array //null
By zenobiten in forum New To JavaReplies: 8Last Post: 02-23-2011, 12:10 PM -
JSP/Ajax/Post giving null values
By Mange in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-22-2010, 10:02 PM -
cant print 2d array(giving null values)
By fneeks in forum New To JavaReplies: 1Last Post: 04-07-2010, 08:48 PM -
OOP/RTII: How can I properly override AND overload methods for use with collections?
By CyJackX in forum New To JavaReplies: 0Last Post: 03-31-2010, 05:18 AM -
Program can run but output all null
By matt_well in forum New To JavaReplies: 15Last Post: 07-24-2008, 08:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks