Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7859777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:02:07+00:00 2026-06-02T22:02:07+00:00

I am trying to test my journey class methods, however, any method that uses

  • 0

I am trying to test my journey class methods, however, any method that uses the jArray field gives a null pointer exception as the arraylist has not actually been created. I was under the impression that my test fixture would address this yet the problem persists, any help appreciated.

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;
        }
    } 

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:02:10+00:00Added an answer on June 2, 2026 at 10:02 pm

    jArray is static field of Journey class.
    You should replace

        ArrayList<Journey> jArray = new ArrayList<Journey>();
    

    with

        buscard.Journey.jArray = new ArrayList<Journey>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to test an AccountController that uses DotNetOpenAuth but I am running into
I am trying to test that a method to load a UI matrix is
I am trying to test a class that manages data access in the database
I'm trying to test a method which calls a couple of other methods in
I'm trying to test a service method that runs asynchronously (@Async). Here is the
I'm trying to test a utility method I have that creates urlencoded query strings.
Im trying to test my extension method that converts a list of strings in
trying to test out move semantics i build a simple class that allocates some
I'm trying to test an Order entity method called AddItem and I'm trying to
I'm trying to test code around a web service that is not available yet.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.