Results 1 to 2 of 2
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default String passed as argument to a constructor returns null value

    I made a program to practice inheritance which uses a GUI.
    It consists of three classes for the moment:

    School_School.java
    Java Code:
    abstract public class School_School 
    {
    	public abstract void getName();
    	public abstract void printName();
    }
    School_Teachers.java
    Java Code:
    import javax.swing.JOptionPane;
    
    public class School_Teachers extends School_School{
    	private String firstName;
    	private String lastName;
    	private String name = firstName + " " + lastName;
    	public School_Teachers(String firstName,String lastName)
    	{
    		this.firstName=firstName;
    		this.lastName=lastName;
    	}
    	public School_Teachers()
    	{
    		getName();
    	}
    	public void getName()
    	{
    		this.firstName = JOptionPane.showInputDialog(null, "Enter First Name: ", "Teacher | Registration", JOptionPane.OK_OPTION);
    		this.lastName = JOptionPane.showInputDialog(null, "Enter Last Name: ", "Teacher | Registration", JOptionPane.OK_OPTION);
    	}
    	public void printName()
    	{
    		System.out.println("Name Entered: "+this.name);
    	}
    }
    School_MainClass.java
    Java Code:
    public class School_MainClass {
    	public static void main(String[] args) {
    		School_School teacher = new School_Teachers("Severus","Snape");
    		teacher.printName();
    
    	}
    
    }
    The problem is that the output returns
    Name entered: null null
    and i dont understand why that is? Could someone please explain and tell me where i've gone wrong - thanks!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eLancaster View Post
    Java Code:
    import javax.swing.JOptionPane;
    
    public class School_Teachers extends School_School{
    	private String firstName;
    	private String lastName;
    	private String name = firstName + " " + lastName;
    	public School_Teachers(String firstName,String lastName)
    	{
    		this.firstName=firstName;
    		this.lastName=lastName;
    	}
    You don't initialize firstName and lastName (you can't because those names are only known when the constructor is called) so they equal null. You try to initialize the name member with those other two null values. Set the value of your name value in your constructor (after firstName and lastName have received they value) and you'r fine.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. What is the Use of No argument Constructor in JAVA Beans
    By JyotirmoyDeb in forum New To Java
    Replies: 2
    Last Post: 10-16-2010, 01:44 AM
  2. getImplementationVersion() returns null
    By newbiejava in forum New To Java
    Replies: 22
    Last Post: 09-12-2010, 09:31 AM
  3. Null array when passed to MouseListener
    By stevemcc in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 10:42 PM
  4. How to resolve Constructor argument in Spring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-29-2008, 12:43 PM
  5. How to resolve Constructor argument in Spring
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 08:34 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
  •