1 Reply Latest reply: Jan 3, 2014 7:08 PM by user11436745 RSS

    java array throws exception for multiple values

    user11436745
      int nRows = 0;
              for (EITest testCurr : testList) {
                  testCaseListArray[nRows] = new Object[3];
                  String testName = testCurr.getTestName();
                  LOG.info("testName=" + testName);
                  testCaseListArray[nRows][0] = testName;
                  String workerRetrieveURL = testCurr.getExpectedDataAPI();
                  testCaseListArray[nRows][1] = workerRetrieveURL;
                  LOG.debug("workerRetrieveURL=" + workerRetrieveURL);
                  List<ActualData> actualDataList = testCurr.getActualData();
                  for (ActualData actualData : actualDataList) {
                      LOG.debug("actualData=" + actualData.getActualDataJSONFile());
                      if (actualData.getActualDataJSONFile() != null ) {
                          testCaseListArray[nRows][2] = actualData.getActualDataJSONFile();
                          nRows++;
                      }
                  }
              }
      

      This throws null pointer exception because of array initialization error. How to fix this?

      I tried this:

       

      int nRows=0;
              for(EITest testCurr: testList){
                  
                  testCaseListArray[nRows] = new Object[3];
                  String testName = testCurr.getTestName();
                  LOG.info("testName=" + testName);
                  testCaseListArray[nRows][0] = testName;
                  String workerRetrieveURL = testCurr.getExpectedDataAPI();
                  testCaseListArray[nRows][1] = workerRetrieveURL;
                  LOG.debug("workerRetrieveURL=" + workerRetrieveURL);
                  String actualData = testCurr.getActualData().get(0).getActualDataJSONFile();
                  LOG.debug("actualData=" + actualData);
                  testCaseListArray[nRows][2] = actualData;
                  nRows++;
               }
      

      But this is not complete solution.

       

      2) I want to make this as a collection object such as array list instead of Array declaration.

        • 1. Re: java array throws exception for multiple values
          rp0428

          Post complete code so that people can try to reproduce what you are reporting.

          This throws null pointer exception because of array initialization error.

          You need to SHOW us what you are doing; not just tell us.

           

          Post the actual command you are executing and the actual results you are getting. The exception should provide the line number in the code so SHOW us what line that is.

          int nRows = 0;  

                  for (EITest testCurr : testList) {  

                      testCaseListArray[nRows] = new Object[3];  

          . . .

                      for (ActualData actualData : actualDataList) {  

                          LOG.debug("actualData=" + actualData.getActualDataJSONFile());  

                          if (actualData.getActualDataJSONFile() != null ) {  

                              testCaseListArray[nRows][2] = actualData.getActualDataJSONFile();  

                              nRows++;  

                          }  

                      }  

                  }

            Why do you have 'nRows++' INSIDE the FOR loop and IF statement where it may never get executed? That means you might just be allocating a new sub-array and wiping out the previous one you just used.