你好我在这个程序中得到 NullPointerException 你能帮我吗?Author 类具有私有属性和 getter setter 方法。Fileoperations 类读取文件并使用 stringtokenizer 逐行分割。我在“ athr[0].setId(tempId);”行收到错误。当我从文件中读取 tempId 等于 100 时。我认为问题出在 Autor 类,但我无法解决。
作者
public class Author {
private int id;
private String name;
private String univ;
private String depart;
private String email;
private String article[] = new String[5];
private int artCtr=0;
public int getArtCtr() {
return artCtr;
}
public void setArtCtr(int artCtr) {
this.artCtr = artCtr;
}
FileOperations flp =new FileOperations();
public String getArticle(int nmb) {
return article[nmb];
}
public void setArticle(String article,int nmb) {
this.article[nmb] = article;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUniv() {
return univ;
}
public void setUniv(String univ) {
this.univ = univ;
}
public String getDepart() {
return depart;
}
public void setDepart(String depart) {
this.depart = depart;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
文件操作
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class FileOperations {
Author athr[] = new Author[500];
int athCtr = 0;
int flag = 0;
String temp;
int tempId;
public void strAthr(String path) throws IOException {
String metin = "Suheyb";
System.out.println(path);
FileReader fl = new FileReader(path);
try (BufferedReader read = new BufferedReader(fl)) {
while (metin != null) {
metin = read.readLine();
if (metin != null) {
StringTokenizer str = new StringTokenizer(metin, "/t ");
if (str.hasMoreTokens()) {
temp = str.nextToken();
tempId = Integer.parseInt(str.nextToken());
System.out.println(tempId + " " + athCtr);
if (athCtr != 0) {
for (int i = 0; i < athCtr; i++) {
if (athr[i].getId() == tempId) {
flag = 1;
if (str.hasMoreTokens()) {
athr[i].setName(str.nextToken());
if (str.hasMoreTokens()) {
athr[i].setDepart(str.nextToken());
if (str.hasMoreTokens()) {
athr[i].setEmail(str
.nextToken());
if (str.hasMoreTokens()) {
athr[i].setArticle(
str.nextToken(), 0);
athr[i].setArtCtr(1);
if (str.hasMoreTokens()) {
athr[i].setArticle(
str.nextToken(),
1);
athr[i].setArtCtr(2);
if (str.hasMoreTokens()) {
athr[i].setArticle(
str.nextToken(),
2);
athr[i].setArtCtr(3);
if (str.hasMoreTokens()) {
athr[i].setArticle(
str.nextToken(),
3);
athr[i].setArtCtr(4);
if (str.hasMoreTokens()) {
athr[i].setArticle(
str.nextToken(),
4);
athr[i].setArtCtr(5);
}
}
}
}
}
}
}
}
}
}
if (flag != 0) {
flag = 0;
athr[athCtr].setId(tempId);
if (str.hasMoreTokens()) {
athr[athCtr].setName(str.nextToken());
if (str.hasMoreTokens()) {
athr[athCtr].setDepart(str.nextToken());
if (str.hasMoreTokens()) {
athr[athCtr].setEmail(str
.nextToken());
if (str.hasMoreTokens()) {
athr[athCtr].setArticle(
str.nextToken(), 0);
athr[athCtr].setArtCtr(1);
if (str.hasMoreTokens()) {
athr[athCtr].setArticle(
str.nextToken(), 1);
athr[athCtr].setArtCtr(2);
if (str.hasMoreTokens()) {
athr[athCtr]
.setArticle(
str.nextToken(),
2);
athr[athCtr]
.setArtCtr(3);
if (str.hasMoreTokens()) {
athr[athCtr]
.setArticle(
str.nextToken(),
3);
athr[athCtr]
.setArtCtr(4);
if (str.hasMoreTokens()) {
athr[athCtr]
.setArticle(
str.nextToken(),
4);
athr[athCtr]
.setArtCtr(5);
}
}
}
}
}
}
}
}
athCtr++;
}
}
else {
System.out.println(tempId);
athr[0].setId(tempId);
System.out.println(athr[0].getId());
if (str.hasMoreTokens()) {
athr[athCtr].setName(str.nextToken());
if (str.hasMoreTokens()) {
athr[athCtr].setDepart(str.nextToken());
if (str.hasMoreTokens()) {
athr[athCtr].setEmail(str.nextToken());
if (str.hasMoreTokens()) {
athr[athCtr].setArticle(
str.nextToken(), 0);
athr[athCtr].setArtCtr(1);
if (str.hasMoreTokens()) {
athr[athCtr].setArticle(
str.nextToken(), 1);
athr[athCtr].setArtCtr(2);
if (str.hasMoreTokens()) {
athr[athCtr].setArticle(
str.nextToken(), 2);
athr[athCtr].setArtCtr(3);
if (str.hasMoreTokens()) {
athr[athCtr]
.setArticle(
str.nextToken(),
3);
athr[athCtr]
.setArtCtr(4);
if (str.hasMoreTokens()) {
athr[athCtr]
.setArticle(
str.nextToken(),
4);
athr[athCtr]
.setArtCtr(5);
}
}
}
}
enter code here
}
}
}
}
athCtr++;
}
}
}
}
}
}
}