1

I can see the values in sysOut but still it returns nullPointer Exception. What am I doing wrong?

    class ProjUtils {

        int noSteps;
        String  testName="";
        String DMS;
        String appL;
        String func;
        String subFunc;
        String desc=""; 
        String regPriority = "High||Medium||Low";
        String type = "Manual||Automated";
        String[] stepName ;
        String[] stepDesc ;
        String[] expRes ;
        int counter=1;
        int caseCounter = 1;

    public void preDefSteps() {


            System.out.println("Enter number of predefined steps:");

            String stName = null,stDesc = null,expResu = null;

            try {

                noSteps = Integer.valueOf(br.readLine());
                int i =0;

                while(i<noSteps && br.readLine() !=null){

                    br.readLine();

                    System.out.println("Step Name:\n");
                    stName =  br.readLine();
                    System.out.println("Step Descripiton:\n");
                    stDesc =  br.readLine();
                    System.out.println("Expected Result:\n");
                    expResu = br.readLine();
                    br.readLine();
                    System.out.println(stName + "\n" + stDesc + "\n" + expResu);
                    stepName[i] = stName;
                    stepDesc[i] = stDesc;
                    expRes[i] = expResu;

                    i++;
                }

O/P: java.lang.NullPointerException at testCaseAuto.ProjUtils.preDefSteps(ProjUtils.java:199) at testCaseAuto.ProjUtils.newConfig(ProjUtils.java:82) at testCaseAuto.Home.main(Home.java:29)

Anmol Bhardwaj
  • 636
  • 6
  • 18

1 Answers1

1

Your

String[] stepName ;
String[] stepDesc ;
String[] expRes ;

are declared, but not initialized. Actually they are null. So you can't assign values to them with

stepName[i] = stName;

and so on.

Please initialize them, e.g. String[] stepName = new String[5];

For array-initialization you need to know the size of the array. It seems that you actually don't know them before the end of the program. So maybe the class ArrayList would be more fitting for your case:

List<String> stepNameList = new ArrayList<>();
[...]
stepNameList.add(stName);

So your code should look like this (no additional optimization):

package code;

import java.util.ArrayList;
import java.util.List;

class ProjUtils
{

   int noSteps;
   String testName = "";
   String DMS;
   String appL;
   String func;
   String subFunc;
   String desc = "";
   String regPriority = "High||Medium||Low";
   String type = "Manual||Automated";
   List<String> stepNameList = new ArrayList<>();
   List<String> stepDescList = new ArrayList<>();
   List<String> expResList = new ArrayList<>();
   int counter = 1;
   int caseCounter = 1;

   public void preDefSteps()
   {


      System.out.println("Enter number of predefined steps:");

      String stName = null, stDesc = null, expResu = null;

      try
      {

         noSteps = Integer.valueOf(br.readLine());
         int i = 0;

         while (i < noSteps && br.readLine() != null)
         {

            br.readLine();

            System.out.println("Step Name:\n");
            stName = br.readLine();
            System.out.println("Step Descripiton:\n");
            stDesc = br.readLine();
            System.out.println("Expected Result:\n");
            expResu = br.readLine();
            br.readLine();
            System.out.println(stName + "\n" + stDesc + "\n" + expResu);
            stepNameList.add(stName);
            stepDescList.add(stDesc);
            expResList.add(expResu);

            i++;
       [...]
}
sarkasronie
  • 335
  • 1
  • 2
  • 15