Making an object public

Hi. I create an object of a type in my main method as follow:

Ciudades ciudades =new Ciudades();/

then, I tried to use this object in another class and I create a method that receives this object

publicvoid setCiudades(Ciudades ciudades){

lasCiudades = ciudades

this.getCiudades(lasCiudades);

}

I create a method with a return to make the object available to all the methods in the class

public Ciudades getCiudades(Ciudades lasCiudades){

return lasCiudades;

}

finally, when I want to use this object in a method in this class, I got a null pointer exception...

Somebody knows what is the reason of this?

regards...

[975 byte] By [karma1234a] at [2007-11-27 4:09:32]
# 1

Change this

public Ciudades getCiudades(Ciudades lasCiudades){

return lasCiudades;

}

to this

public Ciudades getCiudades(Ciudades lasCiudades){

return this.lasCiudades;

}

and ideally this

public Ciudades getCiudades(){

return this.lasCiudades;

}

cotton.ma at 2007-7-12 9:14:59 > top of Java-index,Java Essentials,New To Java...
# 2
You might want to consider reviewing some tutorials as well. http://java.sun.com/docs/books/tutorial/java/javaOO/index.html
cotton.ma at 2007-7-12 9:14:59 > top of Java-index,Java Essentials,New To Java...
# 3

Thanks for your answer, but It dont works. I磎 going to explain my problem in other way:

I create some objects in my main method. I have a master class that controls all the functions of the program, is like a controller. In the main method I create an object controller and within its parameters I send the other objects that I created. For example:

Genetista genetista = new Genetista();//AUXILIAR OBJECTS

Cartografo cartografo = new Cartografo();//AUXILIAR OBJECTS

Dibujante dibujante = new Dibujante();//AUXILIAR OBJECTS

then I create the controlador object

Controlador controlador = new Controlador(genetista,cartografo,dibujante);

I the controlador class, I have some atributes that have the Genetista, Cartografo, Dibujante types, for example:

private Genetista elGenetista;

private Cartografo elCartografo;

private Dibujante elDibujante;

Then in the constructor of the controlador class, I initialize this objects:

public Controlador(Genetista genetista, Cartografo cartografo, Dibujante dibujante){

elGenetista = genetista;

elCartografo = cartografo;

elDibujante = dibujante;

}

Then, when I try to use this objects they have null value, so I can not do nothing. For example I use:

elGenetista.getValues()

And I got a null pointer exception, because the elGenetista object does not exist, but I have created it in the constructor.

Any idea?

Kind regards.

karma1234a at 2007-7-12 9:14:59 > top of Java-index,Java Essentials,New To Java...
# 4
I can find nothing wrong based on what you just stated. Could you show us more of your code? Perhaps a small example that will compile and run on its own?
petes1234a at 2007-7-12 9:14:59 > top of Java-index,Java Essentials,New To Java...
# 5

> Thanks for your answer, but It dont works. I磎 going

> to explain my problem in other way:

>

Well I'll be honest with you. There's really no point.

The code you posted originally is very much rubbish. My edits will help you fix a problem but I am not surprised that there are more.

You should go and read the tutorials. Failing that you should be posting your relevent code. Posting more explanations is a just a waste of everyone's time, including yours.

cotton.ma at 2007-7-12 9:14:59 > top of Java-index,Java Essentials,New To Java...
# 6
I suggest maybe posting the block of code that creates an instance of that Controlador class.What are you passing in to that constructor?Where are you getting that?What is the actual stack trace?
xiarcela at 2007-7-12 9:14:59 > top of Java-index,Java Essentials,New To Java...
# 7

Thanks, I solved the problem.

The problem is that I was using two different objects, in my main method I create something like Controlador controlador = new Controlador(x,y), I was supposed to use this object, but I was using another one that was Controlador controlador = new Controlador(). They are different objects, so they do not have the same information, that was the reason of my problem.

Thanks everybody and regards.

karma1234a at 2007-7-12 9:14:59 > top of Java-index,Java Essentials,New To Java...