(SpringBoot)无法读取资源 - 已更改文件路径 - java.lang.NullPointerException

问题描述 投票:0回答:1

我一直致力于使用 SpringBoot 将第三方支付网关集成到我的电子商务网站中。到目前为止,我已按照在线教程视频中概述的步骤进行操作,没有出现任何问题。但是,我遇到了一条错误消息:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because "fileURL" is null]

根据教程视频,为了解决此问题,我将 EcpayPayment.xml 文件从 config 文件夹移动到 resources 文件夹。我还将 PaymentVerifiedBase.java 中的 confPath 更新为:

protected String confPath = "./src/main/resources/EcpayPayment.xml";
protected String confPath = "/EcpayPayment.xml";

尽管进行了这些更改,fileURL.toString() 仍然会产生 NullPointerException,并且错误消息仍然存在。我已经被这个问题困扰好几天了,不知道如何继续。似乎 fileURL.toString() 没有读取我的 EcpayPayment.xml 文件可能存在问题,但我无法查明确切原因。

这是我的代码:

package ecpay.payment.integration.ecpayOperator;

import ecpay.payment.integration.errorMsg.ErrorMessage;
import ecpay.payment.integration.exception.EcpayException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PaymentVerifyBase{
    protected String confPath = "./src/main/resources/EcpayPayment.xml"; //the route I changed
    protected Document doc;
    public PaymentVerifyBase(){
        URL fileURL = this.getClass().getResource(confPath);
        doc = EcpayFunction.xmlParser(fileURL.toString());//toString is NullPointer
        doc.getDocumentElement().normalize();
    }
    
    protected void requireCheck(String FieldName, String objValue, String require){
        if(require.equals("1") && objValue.isEmpty())
            throw new EcpayException(FieldName+"為必填");
    }
    
    protected void valueCheck(String type, String objValue, Element ele){
        if(objValue.isEmpty()){
            return;
        }
        if(type.equals("String")){
            if(ele.getElementsByTagName("pattern") != null){
                Pattern r = Pattern.compile(ele.getElementsByTagName("pattern").item(0).getTextContent().toString());
                Matcher m = r.matcher(objValue);
                if(!m.find())
                    throw new EcpayException(ele.getAttribute("name")+ErrorMessage.COLUMN_RULE_ERROR);
            }
        } else if(type.equals("Opt")){
            List<String> opt = new ArrayList<String>();
            NodeList n = ele.getElementsByTagName("option");
            for(int i=0; i < n.getLength(); i++){
                opt.add(n.item(i).getTextContent().toString());
            }
            if(!opt.contains(objValue))
                throw new EcpayException(ele.getAttribute("name")+ErrorMessage.COLUMN_RULE_ERROR);
        } else if(type.equals("Int")){
            String mode = ele.getElementsByTagName("mode").item(0).getTextContent();
            String minimum = ele.getElementsByTagName("minimal").item(0).getTextContent();
            String maximum = ele.getElementsByTagName("maximum").item(0).getTextContent();
            if(objValue.isEmpty())
                throw new EcpayException(ele.getAttribute("name")+ErrorMessage.CANNOT_BE_EMPTY);
            int value = Integer.valueOf(objValue);
            if(mode.equals("GE") && value < Integer.valueOf(minimum))
                throw new EcpayException(ele.getAttribute("name")+"不能小於"+minimum);
            else if(mode.equals("LE") && value > Integer.valueOf(maximum))
                throw new EcpayException(ele.getAttribute("name")+"不能大於"+maximum);
            else if(mode.equals("BETWEEN") && value < Integer.valueOf(minimum) && value > Integer.valueOf(maximum))
                throw new EcpayException(ele.getAttribute("name")+"必須介於"+minimum+"和"+maximum+"之間");
            else if(mode.equals("EXCLUDE") && value >= Integer.valueOf(minimum) && value <= Integer.valueOf(maximum))
                throw new EcpayException(ele.getAttribute("name")+"必須小於"+minimum+"或大於"+maximum);
        } else if(type.equals("DepOpt")){
            // TODO
        } 
    }
}

我将非常感谢您为解决此问题提供的任何帮助或见解。谢谢!

spring-boot nullpointerexception tostring
1个回答
0
投票

嘿,这是我的设置和 confPath String 的值,在我将文件移动到 src/main/resources 后它就起作用了

© www.soinside.com 2019 - 2024. All rights reserved.