Archived

This topic is now archived and is closed to further replies.

[java] Testing if the refrence is null

This topic is 5435 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am working on a Java map editior. Currently it is written using JApplet. I have the main menu with buttons on the html page. When the user clicks the "New Map" button a custom JFrame object (called ViewPort) pops-up. The applet contains a ViewPort variable that begins NULL('points' to no object). I have much greater experience using C++ with the == operator to test for null pointers, but I can't seem to figure out how to test if an object exists without using exceptions in Java. If the user clicks the "New Map" and a ViewPort is already opened(created) then i want to use dialogs to ask about saving/inform about losing data blaa blaa blaa... basicly how can i do this in Java... C++...

ViewPort& mapView;  //all java variables are references

//in button handler

if (mapView == null)
{
mapView = new ViewPort("This is the title string");
}
else
{
//inform user that another map is currently open

...
}

I just need to test for null references without using try catch if possible. thanks [edited by - IllMind on January 27, 2003 12:04:43 AM]

Share on other sites
In my experience it works the same as it does in C++, Atleast I perform my test for a null value the same way as the code you posted above. Although almost all of my experience is with the JDK 1.1, Someone correct me if it doesn''t work like that in all of the JRE''s.

Share on other sites
That is the correct method in every version of java I have used. I don''t know why you would be getting exceptions.

"... we should have such an empire for liberty as she has never surveyed since the creation ..."
Thomas Jefferson

Share on other sites
hey thanks your right it is the same (DUH!!!). I was confused because somewhere along the line i heard you were supposed to use .equals(object) because its safer (and more accurate?). I wasn't even sure if there was a null after searching through the java.sun site. But cool it works. Now a new question. I can create an object and once its created it's viewable (custom JFrame called ViewPort). But once i close the ViewPort it remains in memory (and referenced) so I can't create a new ViewPort after I already created and closed one with my current null test. I guess I could always test if it is visible and if not then i can create a new one with the old reference. This would leave the old ViewPort unreferenced and waiting for garbage collection (i assume). However the ViewPort will eventually contain alot of data like 2d arrays of tiles and custom game objects and tileset images, so Im a bit concerned about leaving unreferenced objects in memory for garbage collection. Any tips, Im pretty sure I can't specificly delete objects like in c++ correct?

[edited by - IllMind on January 28, 2003 2:05:38 AM]

Share on other sites
I might be reading your post wrong, but from th psuedo code above it seems like there can only be one viewport open at a time? If that is the case, then maybe you could just have boolean variable to determine if the user has closed that viewport. By doing that you should be able to re-use the old viewport for a new one. Just clear any data that you can, and delete or remove the object that you can from it when the user closes it. since it is a extending JFrame you should be able to make a flush() function for it or something akin to that which will clean itself out.
Like I said I might be reading it wrong.

As for deleting objects, with an array as far as I know you can't delete an object contained therein, but you can set the object equal to null. Although if you used a Vector list you can 'delete' an object contained in it by calling the removeElementAt(int index), or removeAllElements(), or even removeElement(Object obj). Which will effectively delete the object contained.

[edited by - wrathnut on January 28, 2003 2:45:39 AM]

Share on other sites
"equals(Object)" battles "=="

just to clarify ...

The comparison operator "==" just compares the references. So if you compare two objects only aliases of one object return true if you use the "==" operator.

In the other hand the equals method can be overrided in the subclasses to produce custom results. For example you would want to compare Integer object depending on their VALUE and not just plain reference. For example two new Integer(5) objects would return false if you would compare them with "==", but will (as it is implemented this way ... see the source - Integer.java) return true if you do "new Integer(5).equals(new Integer(5)".

have fun ...

Petr Stedry

Share on other sites
quote:
Original post by IllMind
hey thanks your right it is the same (DUH!!!). I was confused because somewhere along the line i heard you were supposed to use .equals(object) because its safer (and more accurate?). I wasn''t even sure if there was a null after searching through the java.sun site. But cool it works. Now a new question. I can create an object and once its created it''s viewable (custom JFrame called ViewPort). But once i close the ViewPort it remains in memory (and referenced) so I can''t create a new ViewPort after I already created and closed one with my current null test. I guess I could always test if it is visible and if not then i can create a new one with the old reference. This would leave the old ViewPort unreferenced and waiting for garbage collection (i assume). However the ViewPort will eventually contain alot of data like 2d arrays of tiles and custom game objects and tileset images, so Im a bit concerned about leaving unreferenced objects in memory for garbage collection. Any tips, Im pretty sure I can''t specificly delete objects like in c++ correct?

[edited by - IllMind on January 28, 2003 2:05:38 AM]

Don''t worry about leaving all those references around. The GC will clean them all up. As soon as it cleans up the old ViewPort, everything in it will become unreferenced and then later collected.

You also have another option. You don''t have to create a new ViewPort each time. Just use a boolean like Wrathnut said, then you can use mapView.show() to make the ViewPort visible again.

First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

Share on other sites
== - Reference equality
.equals - Value equality

"I know very well who Satan is: He is freedom. He is the uncontrolled, the incalculable, the antithesis of order and discipline, the antithesis of the legalism of outer space.... We know where a planet will be in twelve years, four months and nine days. But we don''t know where a butterfly will have flown one minute hence. Therefore the butterfly is of Satan."
-- Jens Bjørneboe

Share on other sites
Hey, thanks for all the replies everybody. I now understand the differences between == and .equals. As far as my ''ViewPort'' goes I think i will go the route of just clearing its data when a new map needs to be opened. Seems more logical and stable doing that as opposed to creating an object over and over.

Thanks!

Share on other sites
You could use the Singleton Pattern (google if you have no idea what it is) if you want only one instance of your ViewPort to exist for the duration of your program. Something like this:

  public class ViewPort {  /** make the constructor protected or private so that    * it can only be instantiated by itself (no other class can    * call: "new ViewPort()" )   */ protected ViewPort() {}  public static ViewPort getInstance() {    if(_instance == null) {        _instance = new ViewPort();    }    return _instance; } private static ViewPort _instance;}

Then when ever you want to use your ViewPort do this:

   ViewPort.getInstance().doSomethingWithMyViewPort(); //or create a reference in a class for example: ViewPort viewPortRef = ViewPort.getInstance(); viewPortRef.doSomethingWithMyViewPort(); //or I want to use renew it''s data or something  ViewPort.getInstance().initialize(); //should now have cleared all data and reset the state of the object

Share on other sites
eh thats ok. Singletons are a bit too much for my puny little map editor. I dont think anyone will get hurt if for some unknown reason 2 instances are created but thanks anyways its always refreshing to see the good ol singleton, makes me feel smarter because i understand it! (to bad i cant use it to pick up chicks )

Share on other sites
quote:
Original post by IllMind (to bad i cant use it to pick up chicks )

"Wanna come home with me and play with my singleton?"

"I know very well who Satan is: He is freedom. He is the uncontrolled, the incalculable, the antithesis of order and discipline, the antithesis of the legalism of outer space.... We know where a planet will be in twelve years, four months and nine days. But we don''t know where a butterfly will have flown one minute hence. Therefore the butterfly is of Satan."
-- Jens Bjørneboe

• Forum Statistics

• Total Topics
628715
• Total Posts
2984360

• 24
• 11
• 10
• 13
• 14