0

I am currently writing a RESTFUL web service program. I am expecting to receive input for either one or two input (depending on my returnValue result). I have a difficulty to assign the value to 2 dimension array.

I have tried the above code an I encountered : java.lang.NullPointerException

Any idea on how to assign the MethodName and MethodStatus into 2 dimension array?

Expected output forMethodSet should be ([1][1]),([2][1]).

The AdaptiveAuthService.adaptiveAuth should be called each time new 2 dimension array are presented.

WebService input:

{"methodSet": [{"num":1,"methodName": 1, "methodStatus": "1"}, 
{"num":2,"methodName": 2, "methodStatus": "1"}]}

AdaptiveRESTFUL:

@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
            }
        }
    }   

AdaptiveAuthService code:

public static int adaptiveAuth(MethodClass[][] methodSet)throws SQLException {

    int aAC = methodSet[0].length;
    int authMethod = methodSet[0][aAC - 1].getmethodName(); //returning nullpointer exception

If you have any idea or way to solve the problem, please suggest and let me know.

Gayan Mettananda
  • 1,498
  • 14
  • 21

1 Answers1

1

From your code

for (int i = 1; i < methodSet.length; i++){
    for (int j = 1; j < methodSet[i].length; j++) {
        methodSet[i][j] = new MethodClass();

You initialiaze i and j from 1 which mean methodSet[0] will not be initialize (null), and you try to getmethodName() from a null value then you will get NullPointerException

Andreas
  • 2,455
  • 10
  • 21
  • 24
Dang Nguyen
  • 1,209
  • 1
  • 17
  • 29