Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

Trouble using access functions in an array of objects

Thread view: 
Aki Laukkanen - 31 May 2004 14:14 GMT
Okay, here's what i've got stuck with this time:
I have an array of objects, and I want to set properties of each object
in the array using the objects' access function.
While doing this, i get a NullPointerException.
What am I doing wrong?

Example of the code:

class Member {
    private long aValue;
    public void SetAValue(long aValue){
        this.aValue = aValue;
    }
}
//The following is written in the main function of another class:
Member[] Members = new Member[10];
for(int i = 0; i < 10; i++){
    Members[i].SetAValue(5);
}

Signature

-Aki "Sus" Laukkanen

Andrew Thompson - 31 May 2004 14:26 GMT
> class Member {
>     private long aValue;

This would be better called
    private long value;

>     public void SetAValue(long aValue){
>         this.aValue = aValue;

       public void setValue(long value){
        this.value = value;
>     }
> }
> //The following is written in the main function of another class:
> Member[] Members = new Member[10];
> for(int i = 0; i < 10; i++){

    // instantiate a new 'Member' object..
    Members[i] = new Member();
    // should work now,,
    Members[i].setValue(5);
> }

HTH

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Ryan Stewart - 31 May 2004 14:30 GMT
> Okay, here's what i've got stuck with this time:
> I have an array of objects, and I want to set properties of each object
[quoted text clipped - 15 lines]
> Members[i].SetAValue(5);
> }

You haven't created any Members, just an array to hold them.

Member[] members = new Member[10];
for (int i = 0; i < members.length; i++) {
   members[i] = new Member();
   members[i].SetAValue(5);
}

Also, variable and method names should start with a lowercase letter.
Aki Laukkanen - 31 May 2004 14:46 GMT
...
> You haven't created any Members, just an array to hold them.
>
[quoted text clipped - 3 lines]
>     members[i].SetAValue(5);
> }
That solved the initial problem (thanks :) , but now I get the error
message "non-static variable this cannot be referenced from a static
context ..."

Signature

-Aki "Sus" Laukkanen

Andrew Thompson - 31 May 2004 15:09 GMT
> now I get the error
> message "non-static variable this cannot be referenced from a static
> context ..."

The error is not in the snippets you have
mentioned, I suggest a self contained
example of your current code (short!)
<http://www.physci.org/codes/sscce.jsp>

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Aki Laukkanen - 31 May 2004 15:50 GMT
>>now I get the error
>>message "non-static variable this cannot be referenced from a static
[quoted text clipped - 4 lines]
> example of your current code (short!)
> <http://www.physci.org/codes/sscce.jsp>

Figured the 2nd problem out already;
I hadn't made a proper class declaration for the main class. (duh!) %)
Thanks again for helping me out with the first puzzler.
Signature

-Aki "Sus" Laukkanen

Andrew Thompson - 31 May 2004 15:58 GMT
..
>> <http://www.physci.org/codes/sscce.jsp>
>>
> Figured the 2nd problem out already;

Cool.  Keep the SSCCE in mind
for the next question.   ;-)

> I hadn't made a proper class declaration for the main class. (duh!) %)
> Thanks again for helping me out with the first puzzler.

You're welcome.

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.