0

I'm very new to Java and I am making this program which randomly puts robots in a predefined arena.

I was testing my addRobot method in the main when this happened enter image description here

Here's the code of my 2 classes:

import java.util.Scanner;
import java.util.Random;



public class RobotArena {

    private int xmax,ymax;
    Robot ro;
    RobotArena roaa;
    public static Robot rrr[];
    RobotArena (int xs, int ys, Random r){
        xmax = xs;
        ymax = ys;
        ro = new Robot(r.nextInt(xmax), r.nextInt(ymax), this); 
    }

    public void addRobot () {

    Scanner add = new Scanner(System.in); 
    System.out.print("How many robots would you like to add?");


    int numberofRobots = add.nextInt();
    rrr = new Robot[numberofRobots];
    add.close();

    for(int c=0 ;c < numberofRobots ; c++)
    {
        Random addro2 = new Random();
        int Xcoordinate = addro2.nextInt(xmax);
        int Ycoordinate = addro2.nextInt(ymax);
        rrr[c]= new Robot(Xcoordinate,Ycoordinate, null);
        System.out.println( rrr[c]);
    }

    }

import java.util.Random;

public class Robot {

    private int x, y, dx, dy, ID;
    private static int RobotID;
    private  RobotArena roA;


    Robot(){
        this(0,0, null);

    }

    Robot(int sx, int sy, RobotArena ra){
        x = sx;
        y = sy;
        dx = 1;
        dy = 1;
        roA = ra;
        ID = RobotID;
        RobotID++;


    }

    public String toString() {
        return "Robot ID = " + ID +", robot is at " + x + ", " + y;
    }

    public boolean isHere(int ix, int iy) {
            if (ix == x && iy == y) {
                return true;
            }
            else 
            {
                return false;
            }
    }


    public static void main(String[] args) {

        RobotArena rrr1;
        Random addro = new Random();
        rrr1 = new RobotArena(20, 5, addro);
        rrr1.addRobot();
        }

}

Even though I never call the toString function, which generates a string for every robot's ID and position in the Arena,

shmosel
  • 49,289
  • 6
  • 73
  • 138
Yiannis
  • 155
  • 1
  • 13
  • If an user answered your question please also accept his answer ([Accepting Answers: How does it work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)). If not then please specify what remains unanswered, this is a crucial part of StackOverflow, thank you very much. – Zabuzard Nov 14 '17 at 10:33

3 Answers3

2

It is called indirectly. You call System.out.println(rrr[c]); and the method is implemented such that it prints argument.toString() of the given argument.

Let's take a look at a recent implemention (8u40-b25). System.out points to an object of type PrintStream, this is how its println method is implemented:

 public void println(Object x) {
     String s = String.valueOf(x);
     synchronized (this) {
         print(s);
         newLine();
     }
 }

It calls String.valueOf(x) (implementation):

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

Which calls obj.toString() on the argument.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • Oh, makes sense. I tried making a toString function for the RobotArena class. I removed the line `System.out.println( rrr[c]); ` and tried to make the add method return an integer, the one I get from scanning to ask the user how many robots to add. However, when making it a variable in my toString method by `int numberofRobots2 = roaa.addRobot();` I get a nullpointerexception error. Can you help me with that – Yiannis Oct 22 '17 at 19:19
  • Yeah it means that `roaa` is `null`. Also, inside your `Robot` class you don't know `roaa`, you only have `roa`. Note that you always set `roa` to `null` since you only construct robots by using `new Robot(Xcoordinate,Ycoordinate, null);`. So you always only assign `null` to `roa`. See also: [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zabuzard Oct 22 '17 at 19:25
  • OpenJDK 6-b14 is not quite the "current implementation" as the official Oracle Java 9 was released earlier this year. – Thorbjørn Ravn Andersen Oct 22 '17 at 19:31
  • @ThorbjørnRavnAndersen Yes. I changed the links to `8u40-b25`. Unfortunately **grep code** doesn't provide more recent source codes. – Zabuzard Oct 22 '17 at 19:33
  • @ThorbjørnRavnAndersen Yeah, already did that. Thanks for the comment. – Zabuzard Oct 22 '17 at 19:36
-1

this is because in java - if you print an object - the toString method will be called implicitly

inxoy
  • 3,484
  • 2
  • 13
  • 21
-1

if you do :

System.out.println( rrr[c]);

then the println method is calling the toString for you internally... that is the real explanation of that beh.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97