You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中调用remove()方法触发NullPointerException求调试帮助

求助:Java代码调用remove()方法触发NullPointerException

问题说明

我是Java新手,调用remove()方法时会出现NullPointerException错误。测试发现setString()和getString()方法运行正常,但在remove()方法中使用getString()的返回值时就会触发该异常。

现有代码

CustomString类代码

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...
            }
        }
        // 原代码此处应有返回语句
        return removedChars;
    }
}

硬编码可正常运行的版本

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...
        }
    }
    return removedChars;
}

JUnit测试用例

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...

问题原因与修复方案

原因

NullPointerException的触发点是inputString.length()——当myString为null时,getString()返回null,调用null对象的length()方法必然抛出空指针异常。从测试用例可以看到,testRemove()里存在两种触发场景:

  • 初始化后未调用setString(),直接调用remove("")
  • 调用setString(null)后调用remove("")

修复

在remove()方法开头增加null判断,若inputString为null,直接返回空字符串,符合测试用例的预期:

public String remove(String arg) {
    String removedChars = "";
    String inputString = getString();
    
    // 处理inputString为null的情况
    if (inputString == null) {
        return removedChars;
    }

    for (int i = 0; i < inputString.length(); i++) {
        if (Character.isAlphabetic(inputString.charAt(i))) {
            // 后续逻辑继续实现
        }
    }
    return removedChars;
}

内容的提问来源于stack exchange,提问作者PvtPropane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 06:57:37