I've been working on integrating a third-party payment gateway into my e-commerce website using SpringBoot. So far, I've followed the steps outlined in online tutorial videos without any issues. However, I've encountered an error message that says:
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]
According to the tutorial videos, to resolve this issue, I moved the EcpayPayment.xml file from the config folder to the resources folder. I also updated the confPath in PaymentVerifiedBase.java to:
protected String confPath = "./src/main/resources/EcpayPayment.xml";
protected String confPath = "/EcpayPayment.xml";
Despite making these changes, the fileURL.toString() still produces a NullPointerException, and the error message persists. I've been stuck on this for several days and don't know how to proceed. It seems like there might be an issue with fileURL.toString() not reading my EcpayPayment.xml file, but I can't pinpoint the exact reason.
Here is my code:
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
}
}
}
I would greatly appreciate any assistance or insights into resolving this issue. Thank you!
Hey here's my settings and value of confPath String, it worked after I moved the file to src/main/resources
I use:
ide pic