Search code examples
javaclassnullpointerexceptionstringbuffer

"Name" class nullpointerexception; attempting to rework code


I have looked at "What is a NullPointerException and how do I fix it?", although that question and its answers simply don't help. I understand my problem; the "NameTest" program is attempting to reference the "Name" object while it's null, but I'm not sure how to rework it to fix this, as I want the lastName, firstName, and fullName to be accessible by all "Name" methods. In the "Name" program, I'm attempting to create a "Name" class, with which a blank name object can be created, and then later assign a String/Stringbuffer value (depending which would work better) with setName() method. Here is the "Name" code:

public class Name
{
private StringBuffer lastName;
private StringBuffer firstName;
private StringBuffer fullName;

public Name()
{
    lastName.replace( 0, 0, "" );
    firstName.replace( 0, 0, "" );
    fullName.replace( 0, 0, "" );
}

public Name( String lN, String fiN )
{
     lastName.replace( 0, lN.length() - 1, lN );

     firstName.replace( 0, fiN.length() - 1, fiN );     

     fullName.replace( 0, (lastName.length() + firstName.length()) + 2, lastName + ", " + firstName);
}

public Name( String fuN )
{
    String[] fuNTemp = fuN.split( ", " );
    lastName.replace( 0, fuNTemp[0].length() - 1, fuNTemp[0] );
    firstName.replace( 0, fuNTemp[1].length() - 1, fuNTemp[1] );

    fullName.replace( 0, (lastName.length() + firstName.length()) + 2, lastName + ", " + firstName);
}

public void setName( String n )
{
    String[] nTemp = n.split( ", " );
    lastName.replace( 0, nTemp[0].length() - 1, nTemp[0] );
    firstName.replace( 0, nTemp[1].length() - 1, nTemp[1] );

    fullName.replace( 0, (lastName.length() + firstName.length()) + 2, lastName + ", " + firstName);
}

public void setName( String lN, String fiN )
{
    lastName.equals( lN );
    firstName.equals( fiN );

    fullName.equals( lastName + ", " + firstName );
} 

public StringBuffer getName()
{
    return fullName;
}
}

Here is the program "NameTest", which I created simply to help figure out "Name":

public class NameTest
{
public static void main( String [] args )
{
    Name test = new Name();

    System.out.println( test.getName() );
    System.out.println();

    String Doe = new String( "Doe" );
    String John = new String( "John" );

    test.setName( Doe, John );

    System.out.println( test.getName() );
}
}

Here is the error message from "NameTest":

Exception in thread "main" java.lang.NullPointerException
    at Name.<init>(Name.java:9)
    at NameTest.main(NameTest.java:5)

Solution

  • The NullPointerException in your Name class is happening because you are trying to access it when its value is null. The value is null because that is the default value for non-primitive variable types. N K Raju has a good tutorial on data types on his Google site.

    To solve your problem, initialize the lastName, firstName, and fullName variables before using them in your constructors.

    Example with no arguments:

    public Name() {
        lastName = new StringBuffer();
        firstName = new StringBuffer();
        fullName = new StringBuffer();
    }
    

    Example with arguments:

    public Name(String lN, String fiN) {
       lastName = new StringBuffer(lN);
       firstName = new StringBuffer(fiN);
       fullName = new StringBuffer(lN + ", " + fiN);
    }