NullPointerException whenever I try to use output from one method in another method

Viewed 31

Can I get a bit of help debugging the Java code below? I'm new to Java and I'm currently getting a NullPointerException error for the remove() method.

From the tests I've run, it looks like the setString() and getString() methods work fine, but when I try to use the output from getString() in the remove() method, I get the NullPointerExceptionError.

import java.util.*;

public class CustomString {
    
    //instance variables

    String myString;
    boolean isSet;

    //constructor

    //initializes myString to null
    //initializes isSet to false
    public CustomString() {

    }


    // methods 
    
    public void setString(String string) {
        this.myString = string;
        this.isSet = true;
    }


    public String getString() {
        return this.myString;
    }

    /*
    Returns a new string version of the current string where the alphabetical
    characters specified in the given arg, are removed 
    */
    public String remove(String arg) {
        String removedChars = ""; //declare output string
        String inputString  = getString(); //get input string to be altered

        for (int i = 0; i < inputString.length(); i++) { //iterate over myString
            if (Character.isAlphabetic(inputString.charAt(i))) {
                //...code continues...

I've also noticed that it seems to run fine if I explicitly hardcode the desired string like so:

public String remove(String arg) {
    String removedChars = "";
    String inputString  = "DESIRED INPUT STRING";

    for (int i = 0; i < inputString.length(); i++) {
        if (Character.isAlphabetic(inputString.charAt(i))) {
            //...code continues...

Any help would be greatly appreciated! Sorry in advance for any bad forum etiquette, I'm pretty new to stack overflow. I've been lurking, but so far I haven't been able to find a fix that works for my particular case.

EDIT: Sorry, I forgot to mention that the code is part of an assignment where we learn about unit testing. The setString function is fed arguments, then the code we write has to pass the assertEquals tests below:

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class CustomStringTest {

    //declare custom string for testing
    CustomString myCustomString;

    @BeforeEach
    public void setUp() throws Exception {
        //initialize custom string for testing
        this.myCustomString = new CustomString();
    }

    @Test
    void testGetString() {
        
        //string should be null to start, before setting it
        assertNull(this.myCustomString.getString());
        
        this.myCustomString.setString("hello");
        assertEquals("hello", this.myCustomString.getString());
        
        this.myCustomString.setString("Ar10CCfg");
        assertEquals("Ar10CCfg", this.myCustomString.getString());

        this.myCustomString.setString("k()soP OQ*(34");
        assertEquals("k()soP OQ*(34", this.myCustomString.getString());
    }
    
    @Test
    void testSetString() {
        
        //string should be null to start, before setting it
        assertNull(this.myCustomString.getString());

        this.myCustomString.setString("Good-bye!");
        assertEquals("Good-bye!", this.myCustomString.getString());
        
        this.myCustomString.setString("Ar10CCfg");
        assertEquals("Ar10CCfg", this.myCustomString.getString());

        this.myCustomString.setString("k()soP OQ*(34");
        assertEquals("k()soP OQ*(34", this.myCustomString.getString());
    }
    
    @Test
    void testRemove() {
        assertEquals("", this.myCustomString.remove(""));
        
        this.myCustomString.setString(null);
        assertEquals("", this.myCustomString.remove(""));
        
        this.myCustomString.setString("my lucky numbers are 6, 8, and 19.");
        assertEquals("my lucky numbes e 6, 8, nd 19.", this.myCustomString.remove("ra6"));

        this.myCustomString.setString("Ar10CCfg");
        assertEquals("A10g", this.myCustomString.remove("crvf10"));

        this.myCustomString.setString("k()soP OQ*(34");
        assertEquals("()oP O*(34", this.myCustomString.remove("()k 3QS4"));
        
    }
///...tests continue...
1 Answers

Before each Test, the Object is being reset

@BeforeEach
public void setUp() throws Exception {
    //initialize custom string for testing
    this.myCustomString = new CustomString();
}

, so in this code

assertEquals("", this.myCustomString.remove(""));

the String is null.

As it is null, then inputString.length() will cause a NPE

Related