由于空指针异常导致的运行时错误
runtime error due to Null Pointer Exception
我正在我的 Linux 机器上编程 Ubuntu 14.04。其他技术规格为:
Machine: Ubuntu-14.04 LTS
JDK: JDK-8 (Java EE application)
Webserver: Apache Tomcat
下面给出的Java代码抛出空指针异常。错误消息如下所示:
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)
第 85 行将是 (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();
}
}
}
在 class Attempt
.
的主要方法中实现这个
for(int i=0;i < 5; i++){
op[i] = new Root(); // here
op[i].get();
op[i].processFile();
op[i].tell();
}
抛出编译错误:
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
您正在初始化 Root
数组,但未初始化其元素。
对于每个 i
,op[i].get
将是 null
,调用任何方法都会抛出 NullPointerException
。
一个简单的成语是:
for(int i=0;i < 5; i++){
op[i] = new Root(); // here
op[i].get();
op[i].processFile();
op[i].tell();
}
一个丑陋的内联替代方案是:
Root op[] = new Root[]{new Root(),new Root(),new Root(),new Root(),new Root()};
另一个丑陋的内联替代方案:
Root[] op = {new Root(),new Root(),new Root(),new Root(),new Root()};
最后,java.util.Arrays
class 有一个更漂亮的 fill
方法:
Root op[] = new Root[5];
java.util.Arrays.fill(op, new Root());
但是,这将用相同的实例而不是每个元素一个实例填充您的数组,这可能不是您想要的结果。
我正在我的 Linux 机器上编程 Ubuntu 14.04。其他技术规格为:
Machine: Ubuntu-14.04 LTS
JDK: JDK-8 (Java EE application)
Webserver: Apache Tomcat
下面给出的Java代码抛出空指针异常。错误消息如下所示:
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)
第 85 行将是 (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();
}
}
}
在 class Attempt
.
for(int i=0;i < 5; i++){
op[i] = new Root(); // here
op[i].get();
op[i].processFile();
op[i].tell();
}
抛出编译错误:
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
您正在初始化 Root
数组,但未初始化其元素。
i
,op[i].get
将是 null
,调用任何方法都会抛出 NullPointerException
。
一个简单的成语是:
for(int i=0;i < 5; i++){
op[i] = new Root(); // here
op[i].get();
op[i].processFile();
op[i].tell();
}
一个丑陋的内联替代方案是:
Root op[] = new Root[]{new Root(),new Root(),new Root(),new Root(),new Root()};
另一个丑陋的内联替代方案:
Root[] op = {new Root(),new Root(),new Root(),new Root(),new Root()};
最后,java.util.Arrays
class 有一个更漂亮的 fill
方法:
Root op[] = new Root[5];
java.util.Arrays.fill(op, new Root());
但是,这将用相同的实例而不是每个元素一个实例填充您的数组,这可能不是您想要的结果。