Results 1 to 2 of 2
  1. #1
    corong1997 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Accessor returns string as null

    So I can't figure out why the accessor (I'm guessing it's because of the accessor) keeps returning the String name as null when I run the program. I am trying to use a scanner in the tester program to store a string for the name, but whenever I try to print it out, the virtual machine prints "null" instead of the name.

    Tester-
    Java Code:
    import java.util.Scanner;
    
    public class StudentTester
    {
        
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            
            System.out.println("What is student 1's name?");
            String name1 = scan.nextLine();
            
            System.out.println("What is student 2's name?");
            String name2 = scan.nextLine();
            
            Student s1 = new Student();
            Student s2 = new Student(8,89,712,name2);
            System.out.println(s1.getName() + "'s current Average is: " + s1.getAverage()
                                + " Total is: " + s1.getTotal() + " Number of Quizzes is: "
                                + s1.getQuizNumber());
            
            System.out.println(s2.getName() + "'s current Average is: " + s2.getAverage()
                                + " Total is: " + s2.getTotal() + " Number of Quizzes is: "
                                + s2.getQuizNumber());
                                
            System.out.println("==========After new quiz is entered ============");        
            s1.addQuiz(20);
            s2.addQuiz(50);
            
            System.out.println(s1);
            System.out.println(s2);
            
        }
        
    }

    Class-
    Java Code:
    public class Student
    {
    
    //Instance Variables
    
    private int totalScore;
    private int averageScore;
    private int quizNumber;
    private String name;
    //Construcctors
    public Student()
    {
        totalScore = 0;
        quizNumber = 0;
        averageScore = 0;
        name = null;
    }
    public Student(int number, int average, int total, String name)
    {
        quizNumber = number;
        totalScore = total;
        averageScore = average;
        name = name;
    }
    
    //Accessors
    public double getAverage()
    {
        return averageScore; 
    }
    public int getTotal()
    {
        return totalScore;
    }
    public int getQuizNumber()
    {
        return quizNumber;
    }
    
    public String getName()
    {
        return name;
    }
    //Mutators
    public void addQuiz(int quiz)
    {
        totalScore = totalScore + quiz;
        quizNumber = quizNumber + 1;
        averageScore = totalScore / quizNumber;
    }
    
    
    //toString
    public String toString()
    {
        String str = "Student's average score is " + averageScore + " in " + quizNumber + " tests";
        return str;
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Accessor returns string as null

    Because name defaults to nulll. When you do name = name that doesn't change the instance field. You need to do this.name = name to distinguish between the instance field and the local argument.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. ArrayList returns null
    By aborgeld in forum New To Java
    Replies: 6
    Last Post: 04-01-2011, 02:32 PM
  2. XML and SAX - class returns 'null'
    By Laffel in forum Advanced Java
    Replies: 2
    Last Post: 03-07-2011, 10:14 PM
  3. Splashscreen returns null
    By Charlie161 in forum AWT / Swing
    Replies: 2
    Last Post: 03-04-2011, 02:25 PM
  4. Replies: 1
    Last Post: 02-07-2011, 11:44 AM
  5. getImplementationVersion() returns null
    By newbiejava in forum New To Java
    Replies: 22
    Last Post: 09-12-2010, 10:31 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
  •