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 8731375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:13:29+00:00 2026-06-13T09:13:29+00:00

I’m building a railway simulator, with the classes simulator, station, train, route and passenger.

  • 0

I’m building a railway simulator, with the classes simulator, station, train, route and passenger. I’m having a problem with passing a route object as a parameter to a train object – it keeps coming up as null. I’ve included my code as the best way to explain my problem.

import java.util.ArrayList;

public class Simulator {
int numStations;    
Route route1;
ArrayList<Station> stations;
ArrayList<Train> trains;
Route[] routes;

public static void main(String[] args) {

    //set up network
    Simulator sim = new Simulator();                
    ArrayList<Station> stations = new ArrayList<Station>();
    stations = sim.generateStations();
    Route[] routes;
    routes = sim.generateRoutes(stations);
    System.out.println("Route array " + routes);
    ArrayList<Train> trains = new ArrayList<Train>();
    trains = sim.generateTrains(routes);

    //start simulator
    sim.generatePassengers(stations);
    Train currentTrain = trains.get(0);
    System.out.println("current train " + currentTrain);
    //route null here
    System.out.println("Route of current train " + currentTrain.route);


}


public Route[] generateRoutes(ArrayList<Station> stations){
    //initialise routes between stations

    Station[] stationList1 = {stations.get(0), stations.get(1), stations.get(2), stations.get(3), stations.get(4), stations.get(5)};
    int[] stationDist1 = {200,100,200,300,200,300};
    Route route1 = new Route(true, stationList1, stationDist1);
    System.out.println("route1 " + route1);

    //make list of all routes
    Route[] routeList;  
    routeList = new Route[1];
    routeList[0] = route1;

    return routeList;
}

public ArrayList<Train> generateTrains(Route[] routes){
    //initialise trains
    ArrayList<Train> trainList = new ArrayList<Train>();
    trainList.add(new Train(routes[0], 100, 0, true, 5));
    trainList.add(new Train(routes[0], 100, 4, false, 10));

    System.out.println("first train in list " + trainList.get(0));
    System.out.println("first route in array " + routes[0]);
    //route is null here
    System.out.println("route of first train " + (trainList.get(0)).route);

    return trainList;
}
}

Here is my output:

route1 Route@addbf1

Route array [LRoute;@42e816

Route1 in array Route@addbf1

first train in list Train@190d11

first route in array Route@addbf1

route of first train null

current train Train@190d11

Route of current train null

Can anyone explain where I’m going wrong?

Edit:
Train class declaration:

public class Train {
Route route;
int capacity;
int recentLoc;
boolean forwards;
boolean atStation;
int speed;
Train currentTrain;
int timeAtStation;

//train constructor
public Train(Route r, int c, int i, boolean f, int s){
    r = route;
    c = capacity;
    i = recentLoc;
    f = forwards;
    s = speed;
    atStation = true;
    timeAtStation = 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-13T09:13:30+00:00Added an answer on June 13, 2026 at 9:13 am

    You’re not assigning values to your instance variables. Default value for object is null. Change “r = route” into “route = r” inside your Train constructor (the same for all the other fields).

    public class Train {
    Route route;
    int capacity;
    int recentLoc;
    boolean forwards;
    boolean atStation;
    int speed;
    Train currentTrain;
    int timeAtStation;
    
    //train constructor
    public Train(Route r, int c, int i, boolean f, int s){
        route = r;
        capacity = c;
        recentLoc = i;
        forwards = f;
        speed = s;
        atStation = true;
        timeAtStation = 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
i got an object with contents of html markup in it, for example: string
I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.