Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    csuever is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default 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());
        
                     
                   }
                   
       
        
    }

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Post your errors?

  3. #3
    csuever is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    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)

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    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.

  5. #5
    csuever is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    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());
        
                     
                   }
                   
       
        
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    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.

  7. #7
    csuever is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Quote Originally Posted by csuever View Post
    I understand what my code needs to do
    Obviously you don't otherwise your code would be working.
    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
    As I explained above your code never does this.

  9. #9
    csuever is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    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.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Quote Originally Posted by Junky View Post
    You are creating the Bowler object with the default constructor. All the code in the other constructor is never executed.
    What part of my previous reply do you not understand?

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

    Default

    Quote Originally Posted by csuever View Post
    in my code, it is reading the integers from the file isn't it?
    where do you think it is doing this?

  12. #12
    csuever is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    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.

  13. #13
    csuever is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    where do you think it is doing this?
    Java Code:
             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();}
        
      }

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    :headdesk:

    Maybe if I shout you will understand.

    YES YOU HAVE WRITTEN THE CODE TO DO THAT BUT IT IS NEVER EXECUTED

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

    Default

    Quote Originally Posted by csuever View Post
    Java Code:
             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();}
        
      }

    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:

    Java Code:
       public static  void main(String[] args) {
         Bowler l = new Bowler();
    (as has already been explained by junky)

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Java Code:
    Bowler l = new Bowler();
    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.

  17. #17
    csuever is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    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.

  18. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

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

  19. #19
    csuever is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    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.

  20. #20
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by csuever View Post
    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.
    You should be calling the constructor that contains the code you want to execute, so yes.

Page 1 of 2 12 LastLast

Similar Threads

  1. Proplem with reading txt-file into array //null
    By zenobiten in forum New To Java
    Replies: 8
    Last Post: 02-23-2011, 12:10 PM
  2. JSP/Ajax/Post giving null values
    By Mange in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-22-2010, 10:02 PM
  3. cant print 2d array(giving null values)
    By fneeks in forum New To Java
    Replies: 1
    Last Post: 04-07-2010, 08:48 PM
  4. Replies: 0
    Last Post: 03-31-2010, 05:18 AM
  5. Program can run but output all null
    By matt_well in forum New To Java
    Replies: 15
    Last Post: 07-24-2008, 08:48 AM

Posting Permissions

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