简体   繁体   English

Java程序中的空指针异常

[英]Null pointer exception in Java Program

Question: you have to check for n test cases that multiplication of a and b is equal to 3rd input value or not.If equal then print YES otherwise NO. 问题:您必须检查n个测试用例,a和b的乘积是否等于第3个输入值;如果相等,则打印YES,否则打印NO。 Input: 2 5 6 30 4 3 20 输入:2 5 6 30 4 3 20

Output: YES NO 输出:是否

Explanation: 2 is number of test cases. 说明:2是测试用例的数量。 5 is a & 6 is b ,multiplication is 30 so YES 5是a&6是b,乘法是30所以是

Constraints : 限制条件:

0 < test cases <= 1000000000 0 < a < b <= 10000000 0 <测试用例<= 1000000000 0 <a <b <= 10000000

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Scanner;


class ques1 
{
    public static void main(String[] args) throws Exception
    {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        Scanner sc=new Scanner(System.in);
        BigInteger test=new BigInteger(sc.next());

        while(!test.equals(BigInteger.ZERO))
        {
            Scanner s=new Scanner(br.readLine());
            s.useDelimiter(" ");
            BigInteger p=new BigInteger(s.next());
            BigInteger q=new BigInteger(s.next());
            BigInteger r=new BigInteger(s.next());
            if(p.multiply(q).equals(r))
            {
                System.out.println("YES");
            }
            else
            {
                System.out.println("NO");
            }
            test=test.subtract(BigInteger.ONE);
        }
    }
}

This program is working fine in eclipse.But while running on online compilers like ideone.com,etc it is giving error message as following: 该程序在eclipse上运行良好,但是在ideone.com等在线编译器上运行时,却显示错误消息,如下所示:

Exception in thread "main" java.lang.NullPointerException
    at java.io.StringReader.<init>(StringReader.java:50)
    at java.util.Scanner.<init>(Scanner.java:702)
    at ques1.main(Main.java:18)

Constraints are so big so i am using biginteger.I had also used stringtokenizer instead of scanner of input separation,but it is also giving same error. 约束是如此之大,所以我正在使用biginteger。我也使用了stringtokenizer来代替输入分离的扫描器,但是它也给出了同样的错误。 i have to take input in a spacing format so i am using stringtokenizer or scanner. 我必须以空格格式输入,所以我正在使用stringtokenizer或扫描仪。 Is there is any other way to separate them. 是否有其他方法可以将它们分开。

br.readLine() will return null when reaching the end of the file. 到达文件末尾时br.readLine()将返回null And new Scanner(String) won't accept null as parameter. 并且new Scanner(String)将不接受null作为参数。 Apart from that you should always check for valid input / if you can read data from a resource. 除此之外,您应该始终检查输入是否有效/是否可以从资源中读取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM