views:

56

answers:

6
import java.io.*;
public class Mainclassexec
{

      public static void main(String[] args) 
      {
         String input = null;
         try 
         {
           String capitalized = capitalize(input);
           System.out.println(capitalized);
         } catch (NullPointerException e) 
           {
              System.out.println(e.toString());
           }
      }

      public static String capitalize(String s) throws NullPointerException
      {
            System.out.println("Enter a string");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            s=br.readLine(); //--->error here IOException must be caught
            // or declared to be thrown
            if (s == null)
            {
               throw new NullPointerException("You have passed a null argument");
            }
            Character firstChar = s.charAt(0);
            String theRest = s.substring(1);
            return firstChar.toString().toUpperCase() + theRest;
        }
}

How should i clear this error? Also please suggest me some site to learn exception handling I am very confuse with this topic.

A: 

Here you go,

public class Mainclassexec {

    public static void main(String[] args) {
        String input = null;
        try {
            String capitalized = capitalize(input);
            System.out.println(capitalized);
        } catch (IOException e) {
            System.out.println(e.toString());
        } 
    }

    public static String capitalize(String s) throws IOException {
        System.out.println("Enter a string");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        s = br.readLine(); 
        Character firstChar = s.charAt(0);
        String theRest = s.substring(1);
        return firstChar.toString().toUpperCase() + theRest;
    }
}

One advice, NullPointerException is a RuntimeException. You don't have to throw it explicitly. The best practice is to handle Nullpointers wherever possible instead of throwing it. It makes the code nasty and it has no added value during compile time.

You can refer this link for a detailed tutorial on Exception handling in Java.

Bragboy
A: 

Add "IOException" to the list of exceptions that the capitalize method throws

i.e.

public static String capitalize(String s) throws NullPointerException, IOException
...

This way it tells any piece of code calling the capiatlize method that it must be able to handle both types of exception.

srkiNZ84
A: 

BufferedReader.readLine() is a method that throws IOException, meaning your program should handle that error itself. It is written that way so that you can do any measure that you need (e.g. to make it clear to the user that the string entered is null, which is the best practice on java and not try to catch the value using s==null).

lock
A: 

Add IOException to the throws clause. You can also use just Exception. You can also use try-catch to handle the IOException differently - consume it (not recommended) or throw some other Exception. The NullPointerException is non checked, so you don't need to add to the throws clause.

fastcodejava
A: 

@Bragboy's answer is enough to fix the compilation error and get the program working. The problem that this fixes is that the BufferedReader.readLine() method can throw IOException, and that is a checked exception. Java insists that when a checked exception is thrown within a method (or some other method that the method calls), it must EITHER be caught in the method using a try / catch OR declared as thrown by the method. @Bragboy's answer does the latter in the capitalize, and then catches the IOException in the main method.

However, there are other important issue too.

Currently capitalize does not do what the method name and signature clearly implies. The signature implies that the method capitalizes its argument. In fact, it totally ignores its argument and reads (and capitalizes) a String from stand input instead.

A better design would be to read the input String into input in the main method, and pass it as the s argument. Then change capitalize to just capitalize the argument String.

Two other points of style:

  • The class name should be MainClassExec ... not Mainclassexec; refer to the Java Style Guide for an explanation of the Java naming conventions.

  • The way that your application is dealing with missing input is ugly. Assuming that you've fixed capitalize as I suggested, then the main method should test that the input variable is not null before calling capitalize.

Finally, in my opinion, there is rarely any point doing something like this:

if (x == null) {
    throw new NullPointerException(...);
}
x.something();

Instead you should simply do this:

x.something();

This will automatically throw an NullPointerException if x is null. Besides, it is a bad idea to use the message of NullPointerException to contain a user error message. Most NPEs happen as a result of programming errors somewhere. If you start using NPEs to report errors resulting from (for example) bad input from the user, you'll end up with a mess.

Stephen C