Search code examples
javanullpointerexceptionubuntu-15.04

runtime error due to Null Pointer Exception


I am programming in my Linux machine Ubuntu 14.04. The Other technical specifications are:

Machine: Ubuntu-14.04 LTS
JDK: JDK-8 (Java EE application)
Webserver: Apache Tomcat

The Java code given below throws Null Pointer Exception. The error message looks like this:

devkrishna@krishna:~/Desktop/Java/project1$ java attempt
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
Exception in thread "main" java.lang.NullPointerException
    at attempt.main(project1.java:85)

The line 85 will be (op[i].get();)

import java.io.*;
import java.util.*;
import java.lang.NullPointerException;


class Root{
    public int no=0;
    public String bookNo;
    public String book;
    public String author;
    RandomAccessFile outs;
    public void get(){
        try{
            DataInputStream in = new DataInputStream(System.in);
            System.out.println("\n");
            System.out.print("Enter book no. : ");
            bookNo = in.readLine();
            no = Integer.parseInt(bookNo);
            System.out.print("Enter the book name: ");
            book = in.readLine();
            System.out.print("Enter author's name: ");
            author = in.readLine();

        }
        catch(IOException e){
            System.out.println("I/O EXCEPTION !!!");
            System.exit(1);
        }
    }
    public void processFile(){
        try{
            outs = new RandomAccessFile("output.dat","rw");
            outs.seek(outs.length());
            outs.writeBytes(bookNo);
            outs.writeBytes("\t \t \t");
            outs.writeBytes(book);
            outs.writeBytes("\t \t \t");
            outs.writeBytes(author);
            outs.writeBytes("\n");
            outs.close();
        }
        catch(IOException e){
            System.out.println("FILE I/O ERROR !!!");
        }
    }
    public void tell(){
        try{
            System.out.println("\n");
            System.out.println("Book no. : " + bookNo);
            System.out.println("Book name: " + book);
            System.out.println("Author: " + author);
            System.out.println("\n");
        }
        catch(Exception e){
            System.out.println("EXCEPTION !!!");
        }
    }
}

class attempt{
    public static void main(String[] args){
        Root op[] = new Root[5];
        for(int i=0;i < 5; i++){
            op[i].get();
            op[i].processFile();
            op[i].tell();
        }
    }
}

Implementing this inside main method of class Attempt.

for(int i=0;i < 5; i++){
    op[i] = new Root(); // here
    op[i].get();
    op[i].processFile();
    op[i].tell();
}

throws the compilation errors:

devkrishna@krishna:~/Desktop/Java/project1$ javac project1.java
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
project1.java:85: error: ']' expected
                Root op[i]=new Root();
                    ^
project1.java:85: error: illegal start of expression
                Root op[i]=new Root();
                     ^
2 errors

Solution

  • You're initializing your Root array without initializing its elements.

    op[i].get will be null for each i, and invoking any method on it will throw a NullPointerException.

    A simple idiom would be:

    for(int i=0;i < 5; i++){
        op[i] = new Root(); // here
        op[i].get();
        op[i].processFile();
        op[i].tell();
    }
    

    An ugly inline alternative would be:

    Root op[] = new Root[]{new Root(),new Root(),new Root(),new Root(),new Root()};
    

    Another ugly inline alternative:

    Root[] op = {new Root(),new Root(),new Root(),new Root(),new Root()};
    

    Finally, the java.util.Arrays class has a prettier fill method:

    Root op[] = new Root[5];
    java.util.Arrays.fill(op, new Root());
    

    However, that will fill your array with the same instance instead of one instance per element, which might not be your desired result.