0

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)
R. A.
  • 21
  • 3

2 Answers2

0

You have not initialize value to StringBuffer variables. Default it will have null value and in the constructor you are trying to replace null value with string "". This is causing NullPointerException.

Initialize your StringBuffer variables.

private StringBuffer lastName=new StringBuffer("lastName");
private StringBuffer firstName=new StringBuffer("firstName");
private StringBuffer fullName=new StringBuffer("fullName");
  • Thank you, that helps, although now it only outputs: "fullname" then "fullname" – R. A. Jun 17 '16 at 16:56
  • If my understanding is correct then you are trying to print Fullname as "Doe John". Why are you comparing two string if you want to assign a value. – Rasesh shah Jun 17 '16 at 17:09
  • I'm actually trying to print "Doe, John", as this seems to be how names are usually entered into a database, although I could very well have forgotten the comma and space or something. – R. A. Jun 17 '16 at 17:11
  • try below code public void setName( String lN, String fiN ) { lastName = new StringBuffer(lN); firstName = new StringBuffer(fiN); fullName = fullName.append(lastName).append(firstName); } – Rasesh shah Jun 17 '16 at 17:14
  • 1
    That helps, thank you. – R. A. Jun 17 '16 at 17:29
0

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);
}
MasterBlaster
  • 948
  • 2
  • 12
  • 26
  • Would I not initialize them outside the methods, so that they can be used in all methods? This is what I have done, although the "replace" portion now does not work. – R. A. Jun 17 '16 at 17:05
  • If you initialize them inside the constructors then you can skip the "replace portion" entirely. Look at [StringBuffer(String str)](https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html#StringBuffer(java.lang.String)). – MasterBlaster Jun 17 '16 at 17:08
  • The reason `test.getName()` isn't returning "Doe John" is because in the `setName(String lN, String fiN)` method you are using the `equals()` method which checks for equality between the two `Object`s, and doesn't change the value of either `Object`s. – MasterBlaster Jun 17 '16 at 17:12
  • Ah, ok. So would it be best to use replace()? Or some other method such as insert? – R. A. Jun 17 '16 at 17:13
  • `replace()` would probably be best because if you used `insert()` you would have to delete all the contents of the `StringBuilder` first. – MasterBlaster Jun 17 '16 at 17:14