2

I am using JDK 1.7, I am checking the code for all conditions of input. if user does not enter any value in the String then it throws a NullPointerException. Is there a way to prevent causing NullPointerException even if the user does not enter any value?

i tried try catch block to catch exception

import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
   public static void main(String args[]) throws Exception {

    Scanner s = new Scanner(System.in);    
    int i=s.nextInt();
    System.out.println(i);

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str = br.readLine();
    try{
        int length=str.length();  //NullPointerException here

        if(length>=1 && length<=15)
        {
            System.out.println(str);
        }
    }
    catch(NullPointerException e)
    {
        System.out.println("Must enter a string");
    }
   }
}

sample input- 5 null

Expected Output- 5 (Empty string value-> "" but no exception thrown message)

4
  • NPE is an unchecked exception, so it is bad practice to catch it. Instead just do a simple null check Commented Jan 19, 2019 at 19:47
  • 2
    Also why have both a Scanner and a BufferedReader to read from System.in? Why not remove the BufferedReader and just have String str = s.nextLine()? (And in reference to your likely future question read this) Commented Jan 19, 2019 at 19:48
  • 1
    I think this is an environment issue. I do not get a NullPointerException when I compile and run it in JDK 1.7. Please ensure you are running the right class. Commented Jan 19, 2019 at 20:00
  • if str is null, calling any method such as .length() will cause a NullPointerException Commented Jan 19, 2019 at 20:34

4 Answers 4

3

Java 8

int length = Optional.ofNullable(str).orElse("").length();

Java 7

int length = str == null ? 0 : str.length();

Java 7 + Apache Commons

int length = StringUtils.length(str);

Use Scanner

Use Scanner instead of BufferedReader; scane.nextLine() returns not null string.


public static void main(String... args) {
    try (Scanner s = new Scanner(System.in)) {
        System.out.println(s.nextInt());

        s.nextLine();

        String str = s.nextLine();

        if (str.length() >= 1 && str.length() <= 15)
            System.out.println(str);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of try/catch you could check if str is not null before you call str.length()

Scanner s = new Scanner(System.in);
String str = s.nextLine();
if (str != null && str.length() <= 15) {
   System.out.println(str);
} else {
   System.out.println("Must enter a string");
}

Comments

0
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
   public static void main(String args[]) throws Exception {

    Scanner s = new Scanner(System.in);    
    int i=s.nextInt();
    System.out.println(i);

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str = br.readLine();
        if(str!=null && str.length() >=1 && str.length()<=15)
        {
            System.out.println(str);
        }
    }
   }
}

Comments

0

1) Read the documentation - note that BufferedReader.readline can legitimately return null under well-defined circumstances.

2) Write code that can handle the possible null return.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.