Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using an object in a different class' method  RSS feed

 
Goksenin Seyhan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.
I have Cells class which extends JFrame, which has a Neighbourcell Vector<Cells>.In my main frame i am using creating objects from Cells class using multidimensional array (cell[][] = new Cells[i][j]). My mainframe has a rule(GameRule), which is created using Rules class. In my Rules class there is a method which is taking a Cells object as parameter. And i am trying to get to the methods/fields of this object in that method.
here is a part of the code--------

public void lifestatus (Cells a){
a=new Cells();


for (int i=0; i<a.neighbourcell.size();i++)
{

if(a.neighbourcell.elementAt(i).getState()==1)
{
a.setNeighbourStateSum(a.getNeighbourStateSum()+1);
-------
in my mainframe i try to run this method to the cells i created within this line =

Gamerule.lifestatus(cell[i][j]);
------
cell[i][j] has neighbourcell vector at this point of code. But when i try to call this method, it initializes a new cell as you can see, and that cell doesnt have any object in the neighbourcell vector. I want to get to the vector of cell[i][j], but i could not achieve that. Any help will be most appreciated. Sorry for the long post, dunno how to make it shorter. Thanks!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Goksenin,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a potential problem...

You're passing an instance of Cells to the method, where it's referenced by the local variable 'a'. But then you immediately re-assign 'a' to point to a new instance of Cells, so the reference value you've passed is lost.
 
Goksenin Seyhan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry all, i didnt know of naming policy(thanks for warning)
changing it now(although goksenin is my real name, no matter how weird it seems
still helps appreciated(couldnt understand previous reply) in fact if i dont reassing a as a new instance, i get a null pointer exception. im lost guys )
[ March 04, 2006: Message edited by: Goksenin ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where exactly are you getting null pointer exception? The compiler should show you the line that's causing this. Please post that, and we'll track it down.

Below is an annotated version of what I was trying to say above. When you call the lifestatus method, you're passing a particular instance of Cells as a parameter. The method is presumably supposed to do something using that instance of Cells. However, in the next line, you create a new instance of Cells and assign that to the variable that was referencing the one passed to the method. So now anything you do in that method with "a" will be on the new instance of Cells instead of the one you passed. And depending on how a Cells object is created with the no-args constructor, this might be giving you a null pointer exception.

(Thanks for taking care of your display name. I wasn't questioning "Goksenin." It's just that we need a first and a last name. )
[ March 04, 2006: Message edited by: marc weber ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc,

I think Goksenin was trying to say that if he removes the "a = new Cells();" then he gets a null pointer exception.

Goksenin,

marc's point is correct. You shouldn't createa new Cells object like you are doing. So where does the NullPointerException (NPE) occur? If you can provide the stack trace (i.e. the error message), it will help us help you with this problem.

Layne
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:
marc,

I think Goksenin was trying to say that if he removes the "a = new Cells();" then he gets a null pointer exception...

Right, I didn't catch that at first. But the creation of a new Cells object is definitely not a solution, so I was trying to explain why we need to remove that line. Then we can troubleshoot.
 
Goksenin Seyhan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc, layne, thank you both.
layne you are right if i remove a = new Cells() i get a null pointer exception on this line =

for (int i = 0; i < a.neighbourcell.size(); i++)

it is the first line i get the null pointer exception. then i get the stack trace again and again where it refers to this point(ie, the line that calls this method, then the initialization line of the frame that has the line that calls this method etc etc). if i make a new instance, then all the vector is initialized with 10 nulls, if i dont make a new instance i get null pointer exception. so what to do?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Goksenin Seyhan:
marc, layne, thank you both.
layne you are right if i remove a = new Cells() i get a null pointer exception on this line =

for (int i = 0; i < a.neighbourcell.size(); i++)

it is the first line i get the null pointer exception. then i get the stack trace again and again where it refers to this point(ie, the line that calls this method, then the initialization line of the frame that has the line that calls this method etc etc). if i make a new instance, then all the vector is initialized with 10 nulls, if i dont make a new instance i get null pointer exception. so what to do?

This sounds like the problem is NOT in this particular method. Someone else calls this method with a null reference. So where do you call this method? Can you track down why the parameter that is passed to it is null when it shouldn't be? If you need help, please post the code where this method is called and we can help you from there.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!