错误:Exception in thread "main" java.lang.NullPointerException
at test.main(test.java:23)
代码:
import java.io.*;
import java.util.*;
import java.lang.*;
public class test{
public static void main(String args[]) {
File file = new File(args[0]);
BufferedReader reader = null;
try {
System.out.println(" ");
reader = new BufferedReader(new FileReader(args[0]));
String tempString = null;
//String[][] question = new String[20][1000];
//String[][] choice = new String[20][1000];
//String[] answer = new String[100];
question[] que =new question[5];
int i=0;
int j=0;
int x=0;
int y=0;
int q=0;
while ((tempString = reader.readLine()) != null) {
que[i].q[j]=tempString;
j++;
while (!(tempString = reader.readLine()).equals("<choice>"))
{
que[i].q[j]=tempString;
//System.out.println(tempString);
j++;
}
i++;
while (!(tempString = reader.readLine()).equals("<answer>"))
{
//choice[x][y]=tempString;
//System.out.println(tempString);
y++;
}
x++;
while (!(tempString = reader.readLine()).equals("<question>"))
{
//answer[q]=tempString;
//System.out.println(tempString);
}
q++;
}reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
for (int ii=0;ii<20;ii++)
for (int jj=0;jj<10;jj++)
System.out.println("question[ii][jj]");
}
}
class question
{
public String[] q = new String[10];
}
附件
<question>
Analyze the following code:
public class Test {
private int t;
public static void main(String[] args) {
int x;
System.out.println(t);
}
}
<choice>
The variable t is not initialized and therefore causes errors
The variable t is private and therefore cannot be accessed in the main method
t is non-static and it cannot be referenced in a static context in the main method.
The variable x is not initialized and therefore causes errors.
The program compiles and runs fine.
<answer>
3
<question>
Consider the following program: import myLibrary.*;
import myLibrary.*;
public class ShowSomeClass {
// code for the class...
}
What is the name of the java file containing this program?
<choice>
myLibrary.java
ShowSomeClass.java
ShowSomeClass
ShowSomeClass.class
<answer>
2
<question>
Which of the following is TRUE?
<choice>
In java, an instance field declared public generates a compilation error.
int is the name of a class available in the package java.lang
Instance variable names may only contain letters and digits.
A class has always a constructor (possibly automatically supplied by the java compiler).
<answer>
4
<question>
Consider the following code snippet
String river = new String(“Columbia”);
System.out.println(river.length());
What is printed?
<choice>
6
7
8
Columbia
<answer>
3
<question>
Which of the following may be part of a class definition?
<choice>
instance variables
instance methods
constructors
all of the above
<answer>
4
at test.main(test.java:23)
代码:
import java.io.*;
import java.util.*;
import java.lang.*;
public class test{
public static void main(String args[]) {
File file = new File(args[0]);
BufferedReader reader = null;
try {
System.out.println(" ");
reader = new BufferedReader(new FileReader(args[0]));
String tempString = null;
//String[][] question = new String[20][1000];
//String[][] choice = new String[20][1000];
//String[] answer = new String[100];
question[] que =new question[5];
int i=0;
int j=0;
int x=0;
int y=0;
int q=0;
while ((tempString = reader.readLine()) != null) {
que[i].q[j]=tempString;
j++;
while (!(tempString = reader.readLine()).equals("<choice>"))
{
que[i].q[j]=tempString;
//System.out.println(tempString);
j++;
}
i++;
while (!(tempString = reader.readLine()).equals("<answer>"))
{
//choice[x][y]=tempString;
//System.out.println(tempString);
y++;
}
x++;
while (!(tempString = reader.readLine()).equals("<question>"))
{
//answer[q]=tempString;
//System.out.println(tempString);
}
q++;
}reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
for (int ii=0;ii<20;ii++)
for (int jj=0;jj<10;jj++)
System.out.println("question[ii][jj]");
}
}
class question
{
public String[] q = new String[10];
}
附件
<question>
Analyze the following code:
public class Test {
private int t;
public static void main(String[] args) {
int x;
System.out.println(t);
}
}
<choice>
The variable t is not initialized and therefore causes errors
The variable t is private and therefore cannot be accessed in the main method
t is non-static and it cannot be referenced in a static context in the main method.
The variable x is not initialized and therefore causes errors.
The program compiles and runs fine.
<answer>
3
<question>
Consider the following program: import myLibrary.*;
import myLibrary.*;
public class ShowSomeClass {
// code for the class...
}
What is the name of the java file containing this program?
<choice>
myLibrary.java
ShowSomeClass.java
ShowSomeClass
ShowSomeClass.class
<answer>
2
<question>
Which of the following is TRUE?
<choice>
In java, an instance field declared public generates a compilation error.
int is the name of a class available in the package java.lang
Instance variable names may only contain letters and digits.
A class has always a constructor (possibly automatically supplied by the java compiler).
<answer>
4
<question>
Consider the following code snippet
String river = new String(“Columbia”);
System.out.println(river.length());
What is printed?
<choice>
6
7
8
Columbia
<answer>
3
<question>
Which of the following may be part of a class definition?
<choice>
instance variables
instance methods
constructors
all of the above
<answer>
4
at test.main(test.java:23为什么为null?
http://topic.csdn.net/u/20110901/21/a79d61de-fcda-453b-a4d1-2638b2a47c88.html?399121import java.io.*;
import java.util.*;
import java.util.List;class QuestionLibrary {
private static int counter = 1;
public final int id = counter++;// 给每一道question一个唯一的id private String question;
private List<String> choices;// 每一个问题里都对应有若干个choice,那么把这些choice也存起来吧;
private String answer; QuestionLibrary(String question, List<String> choices, String answer) {
super();
this.question = question;
this.choices = choices;
this.answer = answer;
} public String getQuestion() {// 你是用private来修饰question的,那么别的类就只能通过此方法来获取question了
return question;
} public void setQuestion(String question) {
this.question = question;
} public List<String> getChoices() {
return choices;
} public void setChoices(List<String> choices) {
this.choices = choices;
} public String getAnswer() {
return answer;
} public void setAnswer(String answer) {
this.answer = answer;
}
}public class ReadTxtToClass {
List<QuestionLibrary> list = new ArrayList<QuestionLibrary>();// 保存所有question
BufferedReader br = null;
String line = null;
StringBuffer question = null;
List<String> choice = null;
StringBuffer answer = null;
public void fileToLibrary() throws Exception {
list = new ArrayList<QuestionLibrary>();
br = new BufferedReader(new FileReader("test.txt"));
question = new StringBuffer();
choice = new ArrayList<String>();
answer = new StringBuffer(); while ((line = br.readLine()) != null) {//消除文件开头的空行,直到<question>
while(line.trim().equals("<question>")) {//将文件指针移到题目开头,//从<question>下一行开始处理,在下一个<question>或者文件尾结束处理 while (!(line = br.readLine().trim()).equals("<choice>"))
//读取<choice>前的信息
question.append(line + "\n");
while (!(line = br.readLine().trim()).equals("<answer>"))
//读取<answer>前的信息
choice.add(new String(line));
//每一个替有多个选项,每个question的每一个选项之间必须有明显的界限,但文件中没有给出(某些选项是以“.”号结束,但有些没有)
//这里我假定每一行就是一个选项
while ((line = br.readLine()) != null) {
if (!(line = line.trim()).equals("<question>"))// 如果还未到文件尾,读取下一个<question>前的信息
answer.append(line + "\n");
else break;
} QuestionLibrary q = new QuestionLibrary(new String(question),
new ArrayList(choice), new String(answer));
list.add(q);
question.delete(0, question.length());//把里面的内容清空,以便存放下一道题
choice.removeAll(choice);
answer.delete(0, answer.length());
if(line == null) break;//如果已经到了文件尾
}
if(line == null) break;//如果已经到了文件尾
}
} public static void main(String args[]) throws Exception {
ReadTxtToClass rttc = new ReadTxtToClass();
rttc.fileToLibrary();//将文件信息转换为试题库
List list = rttc.list;//准备查看表中的信息
for(int i=0; i< list.size(); i++) {
System.out.println("---------问题" + (i+1)+"\n" +((QuestionLibrary)list.get(i)).getQuestion());
System.out.println("---------问题" + (i+1)+"的选项");
for(String temp : ((QuestionLibrary)list.get(i)).getChoices())
System.out.println(temp);
System.out.println("---------问题" + (i+1)+"的答案\n" +((QuestionLibrary)list.get(i)).getAnswer());
}
}
}
/*output:
---------问题1
Analyze the following code:
public class Test {
private int t;
public static void main(String[] args) {
int x;
System.out.println(t);
}
}---------问题1的选项
The variable t is not initialized and therefore causes errors
The variable t is private and therefore cannot be accessed in the main method
t is non-static and it cannot be referenced in a static context in the main method.
The variable x is not initialized and therefore causes errors.
The program compiles and runs fine.
---------问题1的答案
3---------问题2
Consider the following program: import myLibrary.*;
import myLibrary.*;
public class ShowSomeClass {
// code for the class...
}
What is the name of the java file containing this program?---------问题2的选项
myLibrary.java
ShowSomeClass.java
ShowSomeClass
ShowSomeClass.class
---------问题2的答案
2---------问题3
Which of the following is TRUE?---------问题3的选项
In java, an instance field declared public generates a compilation error.
int is the name of a class available in the package java.lang
Instance variable names may only contain letters and digits.
A class has always a constructor (possibly automatically supplied by the java compiler).
---------问题3的答案
4---------问题4
Consider the following code snippet
String river = new String(“Columbia”);
System.out.println(river.length());
What is printed?---------问题4的选项
6
7
8
Columbia
---------问题4的答案
3---------问题5
Which of the following may be part of a class definition?---------问题5的选项
instance variables
instance methods
constructors
all of the above
---------问题5的答案
4
*/
at test.main(test.java:23)
说明23行,抛异常,是个null,lz自己在debug下