二维数组-java.lang.NullPointerException
我目前正在编写一个RESTfulWeb服务程序。 我希望收到一个或两个输入的输入(取决于我的二维数组-java.lang.NullPointerException,java,multidimensional-array,Java,Multidimensional Array,我目前正在编写一个RESTfulWeb服务程序。 我希望收到一个或两个输入的输入(取决于我的returnValueresult)。 我很难给二维数组赋值 我尝试了上面的代码,但遇到了:java.lang.NullPointerException 您知道如何将MethodName和MethodStatus分配到二维数组中吗 MethodSet的预期输出应该是([1][1]),([2][1]) 每次出现新的二维数组时,都应调用AdaptiveAuthService.adaptiveAuth Web服
returnValue
result)。
我很难给二维数组赋值
我尝试了上面的代码,但遇到了:java.lang.NullPointerException
您知道如何将MethodName
和MethodStatus
分配到二维数组中吗
MethodSet
的预期输出应该是([1][1]),([2][1])
每次出现新的二维数组时,都应调用AdaptiveAuthService.adaptiveAuth
Web服务输入:
{"methodSet": [{"num":1,"methodName": 1, "methodStatus": "1"},
{"num":2,"methodName": 2, "methodStatus": "1"}]}
适应性:
@POST
@Path("/post")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public int adaptiveAuth(String objDataLog) throws SQLException{
JSONObject obj = new JSONObject(objDataLog);
JSONArray arr=(JSONArray)obj.get("methodSet");
//to get how many method been send to TE
if (arr != null) {
JSONObject objMethod;
String Num;
for (Object o : arr) {
objMethod = (JSONObject) o;
Num = String.valueOf(objMethod.get("num"));
resultNum = Integer.parseInt(Num);
logWriter("resultNum: "+resultNum);
}
}
//declaration of 2 dimension array
MethodClass[][] methodSet = new MethodClass[resultNum][resultNum];
//to get value for MethodName, MethodStatus
if (arr != null) {
JSONObject objMethod1;
String MethodName, MethodStatus;
for (Object o1 : arr) {
objMethod1 = (JSONObject) o1;
MethodName = String.valueOf(objMethod1.get("methodName"));
MethodStatus = String.valueOf(objMethod1.get("methodStatus"));
int resultMethodName = Integer.parseInt(MethodName);
int resultMethodStatus = Integer.parseInt(MethodStatus);
logWriter("MethodName: "+resultMethodName);
logWriter("MethodStatus: "+resultMethodStatus);
for (int i = 1; i < methodSet.length; i++){
for (int j = 1; j < methodSet[i].length; j++) {
methodSet[i][j] = new MethodClass();
methodSet[i][j].setmethodName(resultMethodName);
methodSet[i][j].setmethodStatus(resultMethodStatus);
}
//calling function to do calculation
returnValue = AdaptiveAuthService.adaptiveAuth(methodSet);//returning null pointer exception
}
}
}
如果你有任何想法或方法来解决这个问题,请建议并让我知道 来自您的代码
for (int i = 1; i < methodSet.length; i++){
for (int j = 1; j < methodSet[i].length; j++) {
methodSet[i][j] = new MethodClass();
for(int i=1;i
从1初始化i
和j
,这意味着methodSet[0]
将不会被初始化(null
),然后尝试从null
值中获取getmethodName()
for (int i = 1; i < methodSet.length; i++){
for (int j = 1; j < methodSet[i].length; j++) {
methodSet[i][j] = new MethodClass();