将字符串分配给stringArray时返回NullPointerException?

时间:2018-03-15 07:29:23

标签: java nullpointerexception java.util.scanner

我可以看到sysOut中的值,但仍然会返回nullPointer Exception。我做错了什么?

    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 at   testCaseAuto.ProjUtils.newConfig(ProjUtils.java:82)at   testCaseAuto.Home.main(Home.java:29)

1 个答案:

答案 0 :(得分:1)

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

已声明,但未初始化。实际上他们是null。因此,您无法使用

为其指定值
stepName[i] = stName;

等等。

请初始化它们,例如String[] stepName = new String[5];

对于数组初始化,您需要知道数组的大小。在程序结束之前,你似乎真的不知道它们。所以类ArrayList可能更适合你的情况:

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

所以你的代码应该是这样的(没有额外的优化):

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++;
       [...]
}