我正在尝试测试我的旅程类方法,但是,任何使用jArray字段的方法都会产生空指针异常,因为实际上并没有创建arraylist。我的印象是我的测试夹具会解决这个问题,但问题仍然存在,任何帮助都会受到赞赏。
private buscard.Journey tj1;
private buscard.Journey tj2;
/**
* Default constructor for test class JourneyTest
*/
public JourneyTest()
{
tj1 = new buscard.Journey(20120101, 120000, "74", 1);
tj2 = new buscard.Journey(20120102, 120000, "74", 1);
ArrayList<Journey> jArray = new ArrayList<Journey>();
jArray.add(tj1);
}
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
protected void setUp()
{
tj1 = new buscard.Journey(20120101, 120000, "74", 1);
tj2 = new buscard.Journey(20120102, 120000, "74", 1);
ArrayList<Journey> jArray = new ArrayList<Journey>();
jArray.add(tj1);
}
public void testGetDate()
{
Journey testJ1 = new Journey(20120101, 12.00, "74", 1);
assertEquals(20120101, testJ1.getDate());
}
public void testGetTime(){
Journey testJ1 = new Journey(20120101, 12.00, "74", 1);
assertEquals(12.0, testJ1.getTime());
}
public void testGetBusNumber(){
Journey testJ1 = new Journey(20120101, 12.00, "74", 1);
assertEquals("74", testJ1.getBusNumber());
}
public void testConstructor(){
Journey testJ1 = new Journey(20120101, 12.00, "74", 1);
assertEquals("74", testJ1.getBusNumber());
assertEquals(12.0, testJ1.getTime());
assertEquals(20120101, testJ1.getDate());
}
}
package buscard;
import java.util.ArrayList;
/**
* Write a description of class journey here.
*
* @author (your name)
* @version (a version number or a date)
*/
class Journey
{
public int date;
public double time;
public String busNumber;
public int journeyType;
public static double dayCharge = 0;
public static final double maxDayCharge = 3.50;
public static double weekCharge = 0;
public static final double maxWeekCharge = 15;
public static double monthCharge = 0;
public static final double maxMonthCharge = 48;
private int journeyNumber;
private static int numberOfJourneys = 0;
public double costOfJourney;
public static ArrayList<Journey> jArray;
public Journey(int date, double time, String busNumber, int journeyType)
{
this.date = date;
this.time = time;
this.busNumber = busNumber;
this.journeyType = journeyType;
journeyNumber = ++numberOfJourneys;
}
static
{
ArrayList<Journey> jArray = new ArrayList<Journey>();
}
public int getDate(){
return date;
}
public double getTime(){
return time;
}
public String getBusNumber(){
return busNumber;
}
public double getCostOfJourney(){
return costOfJourney;
}
public int getJourneyType(){
return journeyType;
}
public boolean isInSequence(int date, double time){
Journey prevJourney = jArray.get(jArray.size()-1);
return((prevJourney.getDate() < date)||(prevJourney.getDate() == date && prevJourney.time < time));
}
public static double returnLeast(){
double d = maxDayCharge - dayCharge;
double m = maxMonthCharge - monthCharge;
double w = maxWeekCharge - weekCharge;
double least = 0;
if (d <= w && d <= m)
{
least = d;
}
else if(w <= d && w <= m)
{
least = w;
}
else if(m <= d && m <= w)
{
least = m;
}
return least;
}
public double journeyCost(Journey reqJourney){
if (journeyType == 1){
if (dayCharge <= 2.50 && weekCharge <= 14 && monthCharge <= 47)
{
costOfJourney = 1;
}
else
{
costOfJourney = returnLeast();
}
}
else if (journeyType == 2)
{
if (dayCharge <= 1.80 && weekCharge <= 13.30 && monthCharge <= 46.30)
{
costOfJourney = 1.70;
}
else
{
costOfJourney = returnLeast();
}
}
else if (journeyType == 3)
{
if (dayCharge <= 1.60 && weekCharge <= 13.10 && monthCharge <= 46.10)
{
costOfJourney = 1.90;
}
else
{
costOfJourney = returnLeast();
}
}
return costOfJourney;
}
public static void updateCurrentCharges(int date){
int newDayOfYear = DateFunctions.getYearDay(date);
int newWeekOfYear = DateFunctions.getYearWeek(date);
int newMonthOfYear = DateFunctions.getYearMonth(date);
if (newDayOfYear > WmBusPass.dayOfYear)
{
WmBusPass.dayOfYear = newDayOfYear;
dayCharge = 0;
}
if (newWeekOfYear > WmBusPass.weekOfYear)
{
WmBusPass.weekOfYear = newWeekOfYear;
weekCharge = 0;
}
if (newMonthOfYear > WmBusPass.monthOfYear)
{
WmBusPass.monthOfYear = newMonthOfYear;
monthCharge = 0;
}
}
}
答案 0 :(得分:3)
jArray
是Journey
类的静态字段。
你应该替换
ArrayList<Journey> jArray = new ArrayList<Journey>();
与
buscard.Journey.jArray = new ArrayList<Journey>();