How do I print the array of another class?

I have created an array with flight destinations in the public class flights and now I want to print out the array using a method in the public class customers. But for some reason the array always prints out as null and I sandly can't my mistake.

main class:

public class Main {
    public static void main(String[] args) {
        flight flight = new flight();
        customer customer = new customer();
        flight.createExampleData();
        customer.output();
    }
}

public class flights:

public class flight{
    public String[] destination = new String[2000];
    public void createExampleData(){
        this.destination[1] = "Paris";
        this.destination[2] = "Geneve";
        this.destination[3] = "Florida";
    }
}

public class customers:

public class customer{
    flight flight = new flight();
    public int i;
    public void output() {
        this.i=1;
        while (i<4){
            System.out.println("Flightnumber: " + this.i);
            System.out.println("Destination: " + flight.destination[this.i]);
            System.out.println("");
            this.i++;
        }
    }
}

(I can't put the print in the flights class for reasons not visible in this simplyfied version of the program)

Outcome from the method outpout after the method createExampleData:


Flightnumber: 1

Destination: null

Flightnumber: 2

Destination: null

Flightnumber: 3

Destination: null


Thanks for the help

You are using two different flight objects, one created in main and one created in your customer class and then you call flight.createExampleData on the instance created in main but the output method uses the one in the customer object so the array in that one was never given any values, hence null in the output.

My suggestion for now is to make the flight variable in customer public

public class customer{
    public flight flight = new flight();
    ...
}

and then change main to

public class Main {
    public static void main(String[] args) {
        customer customer = new customer();
        customer.flight.createExampleData();
        customer.output();
    }
}

A better solution could be to add a getFlight() method to customer instead and keep the variable private.

How to access the array of another class in Java, how to print out that charHpArray for two days and everything i found on google has been useless because they dont contain another array . Re: Calling a print method from another class (printing array) search the number array to find which one has the highest 'int2'. If you have an array of objects and the objects contain more than one value. Add a method to the object to return the value that you want to compare against.

Maybe you haven't executed the function createExampleData() in your customers class?

Java: How do I print an array from another class? My array contains , In Chapter 13.1, we define a Deck class that encapsulates an array of cards, and we write for comparing cards, so we can tell if one is higher or lower than another. it's helpful to add the following print statement at the beginning of the loop:� Write a Java Program to Print Array Elements. Alternatively, write a Java program to Print Elements in an Array using For Loop, While Loop, and Functions with n example of each. Java Program to Print Array Elements using For Loop This program in Java allows the user to enter the Size and elements of an Array.

    flight.createExampleData();

When you call this, createExampleData method in Flight class is executed.

    customer.output();

When you call this output method in Customer class is executed.

There is no relationship between Flight and Customer class in your code. So, customer object's output method wouldn't know what happened in the createExampleData of Flight class.

You can rather do this

  String [] flightDestinations = flight.createExampleData();
  customer.output(flightDestinations);

You will have to change your output method in customer class to use this string array and print its details. Also, the return type for createExampleData should be String [] for this to work.

Arrays of Objects | Think Java, Get code examples like "how to access contents of an array from another class in java" instantly right from your google search results with the� In the above program, since each element in array contains another array, just using Arrays.toString () prints the address of the elements (nested array). To get the numbers from the inner array, we just another function Arrays.deepToString (). This gets us the numbers 1, 2 and so on, we are looking for.

how to access contents of an array from another class in java Code , theNumber; } //annother non static method, that prints out the instances value Exactly which array in exactly which class is being "called" from exactly where? In this article, we will show you a few ways to print a Java Array. 1. Arrays.toString. This is the simplest way to print an Array – Arrays.toString (since JDK 1.5)

java, //method to print an array, taking array as an argument So, the main method calls the method in another class by passing the array to this� By Creating an Object of other class and use that object to invoke the array from your class. You have two classes A and B. Now create another class B and create the object of class A in that class -. In class B, I am creating a integer and storing the first value of array to it. So it will print 1 in this case.

How To Pass / Return an Array In Java, Please Help - Using an Array from another class Hello, I'm new to JAVA and I'm trying to pass an array from one class to another, but I can't get it right. if you want to print the volume array .use the for loop to print . Hello, I'm new to JAVA and I'm trying to pass an array from one class to another, but I can't get it right. I've been able to get individual values on at a time. If any one can help me out and give me a quick sample of how to do it, I've done this below.

Comments
  • You have AnkuftOrt and destination, which one is correct ? Note that array indexes start at 0.
  • And to answer your question, you get null since you never call createExampleData() in output().
  • Voting to close as a typo
  • 1. Be sure to call flight.createExampleData(). 2. Use a for loop instead of a while loop here.
  • @JoakimDanielson the typo is only in the this post and not my code that doesn't work since I changed the names of the variables to english for the post. I call for the fuction in my main class which executes the function createExampleData() and then output(). but it still outputs null
  • Thanks a lot it works now. But could you briefly explain to me why I had to do it like that please?
  • @reichenwald Each instance of flight will have its own array destination and since your output() method belongs to the customer object it uses the flight object that also belongs (is a member variable) to customer. So therefore you need to call createExampleData() on the right instance of flight so that the right destination array gets updated.
  • I have in my main class which executes the function createExampleData() and then output()
  • @reichenwald, i think you should check the answer by Joakim Danielson, i agree with him. Also i hesitate about the line flight flight = flight flight(); in customers class, maybe it's better to use flight flight = new flight();. Finally, names of the classes traditionally start with upper-case letter. (they become more recognizable). For example, Customer or Flight.