在 Java 中删除 NullPointerException


 public class leftrec {
 static int isleft(String[] left,String[] right)
    {
       int f=0;
       for(int i=0;i<left.length;i++)
       {
           for(int j=0;j<right.length;j++)
           {
               if(left[i].charAt(0)==right[j].charAt(0))
               {
                   System.out.println("Grammar is left recursive");
                   f=1;
               }
           }
       }
          return f;   
    }
    public static void main(String[] args) {
        // TODO code application logic here
        String[] left=new String[10];
        String[] right=new String[10];
        Scanner sc=new Scanner(System.in);
        System.out.println("enter no of prod");
        int n=sc.nextInt();
        for(int i=0;i<n;i++)
        {
            System.out.println("enter left prod");
            left[i]=sc.next();
            System.out.println("enter right prod");
            right[i]=sc.next();
        }
        System.out.println("the productions are");
        for(int i=0;i<n;i++)
        {
            System.out.println(left[i]+"->"+right[i]);
        }
        int flag=0;
       flag=isleft(left,right);
           if(flag==1)
           {
               System.out.println("Removing left recursion");
           }
           else
           {
               System.out.println("No left recursion");
           }       
    }
}

我编写了这段代码来找出给定的语法是否是递归的。当我编译程序时,它给了我行NullPointerException

if(left[i].charAt(0)==right[j].charAt(0))

isleft(left,right);

如何删除异常?

我想你的输入有问题,你只是把字符串数组的长度定为 10。

String[] left=new String[10];
String[] right=new String[10];

不要对字符串数组长度进行硬编码

int n=sc.nextInt();
String[] left=new String[n];
String[] right=new String[n];
for(int i=0;i<n;i++){
    System.out.println("enter left prod");
    left[i]=sc.next();
    System.out.println("enter right prod");
    right[i]=sc.next();
}

可能,这将是问题所在

您需要按如下方式更改代码:

package com.cgi.ie2.common;
import java.util.Scanner;
public class LeftRecursive {
static int isleft(String[] left, String[] right)
{
    int f = 0;
    for (int i = 0; i < left.length; i++) {
        for (int j = 0; j < right.length; j++)
        {
            if (left[i].charAt(0) == right[j].charAt(0)) {
                System.out.println("Grammar is left recursive");
                f = 1;
            }
        }
    }
    return f;
}
public static void main(String[] args) {
    // TODO code application logic here
    Scanner sc = new Scanner(System.in);
    System.out.println("enter no of prod");
    int n = sc.nextInt();
    //Changes done here::::
    String[] left = new String[n];
    String[] right = new String[n];
    for (int i = 0; i < n; i++) {
        System.out.println("enter left prod");
        left[i] = sc.next();
        System.out.println("enter right prod");
        right[i] = sc.next();
    }
    System.out.println("the productions are");
    for (int i = 0; i < n; i++) {
        System.out.println(left[i] + "->" + right[i]);
    }
    int flag = 0;
    flag = isleft(left, right);
    if (flag == 1) {
        System.out.println("Removing left recursion");
    } else {
        System.out.println("No left recursion");
    }
}
  }

此代码将消除 NullpointerExceptions

如果您从控制台获得 prod 的编号,则需要相应地设置字符串数组,因为我所做的更改是:

    System.out.println("enter no of prod");
    int n = sc.nextInt();
    //Changes done here::::
    String[] left = new String[n];
    String[] right = new String[n];

对于更好的代码,我可以建议你遵循基本的编码约定,这使你的代码可读,代码只有在它直接运行时才不完美,如果遵循编码约定,代码是完美的,所以请通过以下链接来获取编码约定的基本概念:

http://www.javacodegeeks.com/2012/10/java-coding-conventions-considered-harmful.htmlhttp://java.about.com/od/javasyntax/a/nameconventions.htm

你不能初始化没有大小的数组。您已经将数组大小指定为 10,如果您输入大于 10 或小于 10 的产品,您将收到错误。如果你想使用动态大小,你应该使用java集合。最好的方法是数组列表

static int isLeft(ArrayList left, ArrayList right)

    {
        int f = 0;
        for (int i = 0; i < left.size(); i++) {
            for (int j = 0; j < right.size(); j++)
            {
                if (left.get(i).charAt(0) == right.get(j).charAt(0)) {
                    System.out.println("Grammar is left recursive");
                    f = 1;
                }
            }
        }
        return f;
    }
    public static void main(String[] args) {
        // TODO code application logic here
        ArrayList<String> left = new ArrayList<String>();
        ArrayList<String> right = new ArrayList<String>();

        Scanner sc = new Scanner(System.in);
        System.out.println("enter no of prod");
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            System.out.println("enter left prod");
            String leftText = sc.next();
            left.add(leftText);
            System.out.println("enter right prod");
            String rightText = sc.next();
            right.add(rightText);
        }
        System.out.println("the productions are");
        for (int i = 0; i < n; i++) {
            System.out.println(left.get(i) + "->" + right.get(i));
        }
        int flag;
        flag = isLeft(left, right);
        if (flag == 1) {
            System.out.println("Removing left recursion");
        } else {
            System.out.println("No left recursion");
        }
    }

相关内容

  • 没有找到相关文章

最新更新