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

changing the values of array elements  RSS feed

 
Masha Stekker
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am struggling with a problem in which I'm attempting to change the values of an array.

I'm unsure if the problem is because I'm interpreting the rules of Java incorrectly, or whether the mistake is something specific in my code somewhere. Maybe someone could read my understanding of what is supposed to happen and see if I've got it horribly wrong...

These are my assumptions:

I have three classes: in class Two, I instantiate an array of type class One. Lets say this array of objects is called array1.

Would it be correct to say that in doing so I'm instantiating a number of objects of class One?

I set the values the elements of this array using the public methods of class One, which changes the values of the private variables of class One. Lets say this array of objects is called array1.

Class Three is a subclass of class Two - it inherits class Two. In class Three, I attempt to change the values of the variables of the elements in array1 in the following way.

I use the public methods in class Two (which are inherited by class three) which in turn call the public methods of class One, which then change the values of that class's private variables.

This seems the correct way to me, but it's not working. I get a "null pointer exception" at the point when Class Two attempts to call the methods of Class One.

I hope this is not too vague for someone to get an idea of what I'm attempting: are there any obvious flaws in my assumptions?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Masha Stekker:

Would it be correct to say that in doing so I'm instantiating a number of objects of class One?


Nope. A new array is a set of null pointers of the given type. There are no objects except for the array object itself. I suspect this is your whole problem. When you create an array

One array1 = new One[10];

You have to actually then create the 10 One objects:


[ May 15, 2004: Message edited by: Ernest Friedman-Hill ]
 
Masha Stekker
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, that makes sense - I have done that - I mean, I have said not just

One array1 = new One[10];

But created a loop with array1 = new One(); and then set each element to be a specific value.

So I assume I now have the 10 One objects, as you say.

My problem is this: I want to to change the values of the variables in these objects (if that is the correct way of putting it). It is important that it is the values of these specific objects that must change. I try to call a method of the class One that will change the values of class One's private variables.

The method call is in class Three, which is a subclass of class Two. The method that class Three calls is setAlive(); which is in method in class Two,

public void setAlive(int x)
{
array1[x].setAlive();

}

and the method that is called in this method (also called setAlive()) is a method in class One that changes the values of class One's private variables.

But I get a null point exception unless I create a new set of objects of the array in the class Two method in this way:

public void setAlive(int x)
{
array1[x] = new One();
array1[x].setAlive();

}

But this does not work. I dont get a null point exception any more, but it seems as though there is now a new array1, and changing the values of that does not affedt the objects in the original array. Can this be?

I've also tried creating a sort of "interim" array to hold the new value and then set the value of the original array elements equal to the interim like this
{
array2[x] = new One();
array2[x].setAlive();
array1[x] = array2[x];

}

but this seems to have exactly the same effect as the version above - ie it does not seem that the correct object gets changed.


Thanks for your patience
[ May 16, 2004: Message edited by: Masha Stekker ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But created a loop with array1 = new One(); and then set each element to be a specific value.

Let's see what that code looks like.

it seems as though there is now a new array1

It should certainly seem as though there is a new One object in the array, and so then you wouldn't see any changes in a One object that got displaced by that method call.

If you were to post more of your code, it might be easier to point you in the right direction.

On a slightly different note...

The basic Java programming concepts that you're working to understand here are definitely things that you'll want to master. It appears to me that the concepts that you're struggling with include working with arrays, and references to objects.

For better learning arrays, I recommend the following lessons.
  • Sun's Java Tutorial on Arrays
  • Chapters 46-49 of Bradley Kjell's Introduction to Computer Science using Java
  • Chapter 8 of David J. Eck's Introduction to Programming Using Java
  • Dick Baldwin's Java Programming Tutorials (search for the word "array" on the page)


  • For better understanding references, I recommend reading some of the JavaRanch Campfire Stories, including "Cup Size" and "Pass-by-Value Please".
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!