[英]Java Beans to test a class
给了我一个类DrivingTestM.java来测试我编写的2个类。 这是我写的课程。 当我运行DrivingTestM.java时,出现以下错误:
System.out.println( question.getDescription() );
我不确定错误可能是什么。 任何人都可以尝试阐明此错误吗? 谢谢!
Question.java:
public class Question {
String description;
String answerA;
String answerB;
String answerC;
int correctAnswer;
int answer;
Boolean answerCorrect;
public Question(){
}
public Question(String description, String answerA, String answerB, String answerC, int correctAnswer, int answer){
this.description = description;
this.answerA = answerA;
this.answerB = answerB;
this.answerC = answerC;
this.correctAnswer = correctAnswer;
this.answer = answer;
}
public Question(String description, String answerA, String answerB, String answerC, int correctAnswer){
this.description = description;
this.answerA = answerA;
this.answerB = answerB;
this.answerC = answerC;
this.correctAnswer = correctAnswer;
}
public Boolean isAnswerCorrect() {
return answerCorrect;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAnswerA() {
return answerA;
}
public void setAnswerA(String answerA) {
this.answerA = answerA;
}
public String getAnswerB() {
return answerB;
}
public void setAnswerB(String answerB) {
this.answerB = answerB;
}
public String getAnswerC() {
return answerC;
}
public void setAnswerC(String answerC) {
this.answerC = answerC;
}
public int getCorrectAnswer() {
return correctAnswer;
}
public void setCorrectAnswer(int correctAnswer) {
this.correctAnswer = correctAnswer;
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
}
DrivingTest.java:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DrivingTest {
int currentQuestionIndex = 0;
Question currentQuestion;
Boolean lastQuestion;
int score;
List<Question> q = new ArrayList<Question>();
Question quest = new Question();
public DrivingTest() throws FileNotFoundException{
File f = new File("DrivingTest.txt");
//int n = 1;
Scanner sc = new Scanner(f);
while(sc.hasNextLine()){
String desc = sc.nextLine();
String A = sc.nextLine();
String B = sc.nextLine();
String C = sc.nextLine();
String h = sc.nextLine();
int a = Integer.parseInt(h);
String blank = sc.nextLine();
q.add(new Question(desc, A,B,C,a) );
}
sc.close();
}
public void setCurrentQuestionIndex(int currentQuestionIndex) {
this.currentQuestionIndex = currentQuestionIndex;
}
public int getCurrentQuestionIndex() {
return currentQuestionIndex;
}
public Boolean isLastQuestion() {
if(currentQuestionIndex == q.size() - 1){
return true;
}
else{
return false;
}
}
public Question getCurrentQuestion() {
return currentQuestion;
}
public void setCurrentQuestion(Question currentQuestion) {
this.currentQuestion = currentQuestion;
}
public int getScore() {
return score;
}
}
DrivingTestM.java(测试文件):
import java.io.FileNotFoundException;
public class DrivingTestMain {
public static void main( String args[] ) throws FileNotFoundException
{
DrivingTest drivingTest = new DrivingTest();
while( true )
{
// display the current question
Question question = drivingTest.getCurrentQuestion();
System.out.println( question.getDescription() );
System.out.println( "\t" + question.getAnswerA() );
System.out.println( "\t" + question.getAnswerB() );
System.out.println( "\t" + question.getAnswerC() + "\n" );
// set the answer to the current question to 1
drivingTest.getCurrentQuestion().setAnswer( 1 );
// if this is the last question, we are done.
if( drivingTest.isLastQuestion() ) break;
// it is not the last question, so increment CurrentQuestionIndex
int currentQuestionIndex = drivingTest.getCurrentQuestionIndex();
drivingTest.setCurrentQuestionIndex( currentQuestionIndex + 1 );
}
// display the test score
System.out.println( "Your test score is: " + drivingTest.getScore() );
}
}
您在尝试使用当前问题之前未在任何地方设置当前问题。
您需要类似以下方法:
public void startTest()
{
currentQuestion = q.get(0);
}
接着:
DrivingTest drivingTest = new DrivingTest();
drivingTest.startTest();
while( true )
{
//....
还要确保您有疑问要解决,否则您也会遇到其他错误。
如果您使用的是eclipse,请尝试逐步调试代码...
您可能还想寻找一种更好的方法来终止该循环,目前它会在测试结束时失败...
编辑:好的循环不会中断,但它的确很杂乱...
编辑DrivingTest构造函数:
public DrivingTest() throws FileNotFoundException{
File f = new File("DrivingTest.txt");
//int n = 1;
Scanner sc = new Scanner(f);
while(sc.hasNextLine()){
String desc = sc.nextLine();
String A = sc.nextLine();
String B = sc.nextLine();
String C = sc.nextLine();
String h = sc.nextLine();
int a = Integer.parseInt(h);
String blank = sc.nextLine();
q.add(new Question(desc, A,B,C,a) );
}
sc.close();
//ensure it's 0.
currentQuestionIndex = 0;
//sets up your first question object.
setCurrentQuestion( q.get(currentQuestionIndex) );
}
您将得到java.lang.NullPointerException,因为您将Question对象存储在Q列表中,但没有从中读取对象。 在主类中尝试类似的方法,而不是:
Question question = drivingTest.getCurrentQuestion();
采用
List<Question> qList = drivingTest.getQ();
Question question = qList.get(i);
还可以在DrivingTest类中添加Q getter。
如果您不想修改主类,而只需要在getCurrentQuestion()方法中进行更改:
int i = 0;
public Question getCurrentQuestion() {
if (i<q.size()-1)
return q.get(i++);
else return q.get(i);
}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.