user1914367
user1914367

Reputation: 171

InputStreamReader NullPointerException

What can it be the reason to get NullPointerException sometimes with below code piece.

BufferedReader reader = new BufferedReader(new InputStreamReader
                    (getClass().getClassLoader().getResourceAsStream(this.getClientHost()+".txt")));

The strange part that it sometimes works without exception but sometimes give this exception. My code i trying to read that file many times. Generally it reads in first time successful and in the second attempt it gives this exception.

Here is all code:

public class DictionaryImp extends RemoteServer implements Dictionary {
static FileOutputStream log;
    public DictionaryImp(){
        super();        
    }
public String statictics() {
        // TODO Auto-generated method stub
        try {
            setLog("Statistic", this.getClientHost());
        } catch (ServerNotActiveException e1) {
            // TODO Auto-generated catch block
            System.err.println("server is not active!!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       //some statistic    

        return msg;
    }

public String log() throws RemoteException {

        try {
            setLog("Log", this.getClientHost());
        } catch (ServerNotActiveException e1) {
            // TODO Auto-generated catch block
            System.err.println("Client is not active!!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String msg = "";
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader
                    (getClass().getClassLoader().getResourceAsStream(this.getClientHost()+".txt")));
            String line = null;

            while((line = reader.readLine()) != null){
                msg += line + "\n";
            }
            reader.close();
        } catch (ServerNotActiveException | IOException e) {
            // TODO Auto-generated catch block
            System.err.println("Client is not active");
        }

        return msg;
    }

private static void  createLogFile(String logFileName){
        File logFile = null;
        logFile = new File(logFileName);

        if(!logFile.exists())
            try {
                logFile.createNewFile();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                System.err.println("Log file couldn't created!!");
            }
        try {
            log = new FileOutputStream(logFile, true);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            System.err.println("Log file not found!!");
        }       
    }

    private static void setLog(String event, String ip) throws IOException{
        createLogFile("Resources/"+ip+".txt");
        String time = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(Calendar.getInstance().getTime());
        String msg = ip + "\t " +event + "\t " + time + "\n";
        try {
            log.write(msg.getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.err.println("Log file not found!!");
        }
        log.close();
    }
}

Exception in thread "main" java.lang.NullPointerException
    at java.io.Reader.<init>(Unknown Source)
    at java.io.InputStreamReader.<init>(Unknown Source)
    at DictionaryImp.search(DictionaryImp.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
    at sun.rmi.transport.Transport$1.run(Unknown Source)
    at sun.rmi.transport.Transport$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
    at com.sun.proxy.$Proxy0.search(Unknown Source)
    at ClientRMI.calcThroughput(ClientRMI.java:200)
    at ClientRMI.main(ClientRMI.java:65)

Upvotes: 0

Views: 15383

Answers (2)

oschlueter
oschlueter

Reputation: 2698

You're accessing a Resource that couldn't be found, try to illustrate it with this code:

    InputStream is = getClass().getClassLoader().getResourceAsStream("dictionary.txt");

    System.out.println(is == null);

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240870

because resource you are attempting to read (dictionary.txt )is not in classpath at the root and being resolved null

Upvotes: 1

Related Questions