[英]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.