Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: how to access array f objects in java

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post how to access array f objects in java

    Hi.... im just trying out some new programs using inheritance... can sumone pleeassee teme how to use array of objects in java??? ive donw it in c++ but i guess its no tthe same here in java...can anyone pleeasee help me out..?


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post need help in using array of objects to access classes...

    hi...im just running over some programs in java inheritance... and i so desperatly need to use array of objects for acesing some of my subclasses.....ive done this in c++ ,....well i guess its not the same here in java... can someone help me out ..??????

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to access array f objects in java

    you can use an array list:
    ArrayList<object> arr= new ArrayList<object>();
    or using a regular array:
    object[] arr= new object[size];

    and then for an array its pretty much the same as C++ and for an arraylist, you use .add to add objects, .get to get objects, .set to set an index of the list to an objects .delete to delete an object at some point, .contains to check if an object is in the list and so on and so forth

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: need help in using array of objects to access classes...

    Can you post the code here?

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to access array f objects in java

    ummm... actually wat I meant was not ecaxtly this.... its like.... I access a class repeatedly using an object..and I want to store each access to the class in an array of the object tht I created for the class.... is this possible in java..??? is my question too complicated..?

  6. #6
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: how to access array f objects in java

    Update the array during each access...

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post problem with accesing the classes with objects

    import java.util.Scanner;
    class A
    {
    double bp,da,hra,gp;
    int pf,ca;
    void professor()
    {
    bp=37000;
    da=0.4*37000;
    hra=10000;
    pf=1000;
    ca=1000;
    gp=bp+hra+da+ca;
    System.out.println(+gp);
    }
    void associate()
    {
    bp=37000;
    da=0.4*37000;
    hra=8000;
    pf=1000;
    ca=1000;
    gp=bp+hra+da+ca;
    }
    void assistant()
    {
    bp=30000;
    da=0.4*30000;
    hra=8000;
    pf=800;
    ca=800;
    gp=bp+hra+da+ca;
    }
    }
    class Emp extends A
    {
    public static void main(String args[])
    {
    int n,i;
    String name;
    String desg;
    String s1="professor";
    Emp[] obj= new Emp[4];
    Scanner sc = new Scanner(System.in);
    for(i=1;i<3;i++)
    {
    System.out.println("\n Enter the desg : ");
    desg=sc.nextLine();
    if(desg.equals(s1))
    {
    obj[i].professor();
    }
    }

    }
    }


    this is my coding..... at the line where ive used "obj[I].professor" its shows me a java.lang.nullpointer error.... can sumone please teme wat is the other way for doing this??

  8. #8
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    610
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: problem with accesing the classes with objects

    You've never initialized the objects in the array. Please use code tags when posting code and include the full stack trace.

  9. #9
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to access array f objects in java

    and how do I do tht??
    this is my coding.......
    import java.util.Scanner;
    class A
    {
    double bp,da,hra,gp;
    int pf,ca;
    void professor()
    {
    bp=37000;
    da=0.4*37000;
    hra=10000;
    pf=1000;
    ca=1000;
    gp=bp+hra+da+ca;
    System.out.println(+gp);
    }
    void associate()
    {
    bp=37000;
    da=0.4*37000;
    hra=8000;
    pf=1000;
    ca=1000;
    gp=bp+hra+da+ca;
    }
    void assistant()
    {
    bp=30000;
    da=0.4*30000;
    hra=8000;
    pf=800;
    ca=800;
    gp=bp+hra+da+ca;
    }
    }
    class Emp extends A
    {
    public static void main(String args[])
    {
    int n,i;
    String name;
    String desg;
    String s1="professor";
    Emp[] obj= new Emp[4];
    Scanner sc = new Scanner(System.in);
    for(i=1;i<3;i++)
    {
    System.out.println("\n Enter the desg : ");
    desg=sc.nextLine();
    if(desg.equals(s1))
    {
    obj[i].professor();
    }
    }

    }
    }

    and it shows me an java.lang.nullpointer error...

    --- Update ---

    how do I update the array..?

  10. #10
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: how to access array f objects in java

    NullPointerException means, that you're trying to do something with null. Hence, check up every object in main() to be not null. Please, wrap your code in the [code=Java]/*...*/[//code] - it's really difficult to read.

  11. #11
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with accesing the classes with objects

    and how do I use code tags and full stack trace?

  12. #12
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    610
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: problem with accesing the classes with objects

    By reading the big announcement at the top of this forum. The full stack trace ist the complete error message you get at runtime.

  13. #13
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to access array f objects in java

    do I have to add tht before and after my code to convert it to a presentable form?

  14. #14
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: how to access array f objects in java

    yes. [code=Java] before, [//code] (with 1 slash) after

  15. The Following User Says Thank You to angstrem For This Useful Post:

    Fanta Hubert (May 27th, 2013)

  16. #15
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to access array f objects in java

    import java.util.Scanner;
    class A
    {
        double bp,da,hra,gp;
        int pf,ca;
        void professor()
        {
            bp=37000;
            da=0.4*37000;
            hra=10000;
            pf=1000;
            ca=1000;
            gp=bp+hra+da+ca;
            System.out.println(+gp);
        }
        void associate()
        {
            bp=37000;
            da=0.4*37000;
            hra=8000;
            pf=1000;
            ca=1000;
            gp=bp+hra+da+ca;
            System.out.println(+gp);
        }
        void assistant()
        {
            bp=30000;
            da=0.4*30000;
            hra=8000;
            pf=800;
            ca=800;
            gp=bp+hra+da+ca;
            System.out.println(+gp);
        }
        void display()
        {
            System.out.println(+gp);
        }
    }
    class Emp extends A
    {
        public static void main(String args[])
        {
            int n,i;
                String name;
                String desg;
                String s1="professor";
                String s2="associate";
                A obj=new A();
            Scanner sc = new Scanner(System.in);
            for(i=2;i<4;i++)
            {
          System.out.println("\n Enter the desg : ");
              desg=sc.nextLine();
              if(desg.equals(s1))
              {
                  obj.professor();
              }
              else if(desg.equals(s2))
              {
                  obj.associate();
              }
            }
           for(i=1;i<3;i++)
           {
               obj.display();
           }
        }
    }


    --- Update ---

    now this is my code..... well after accessing function professor and associate... I want to print both their gross pays ...well my output displays the gross pay of the associate itself two times.. which means tht the object has only the last acces In it... how to change this..?

  17. #16
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: how to access array f objects in java

    So do you want some kind of memory in your object to remember the results of all your accesses and computations?

  18. #17
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to access array f objects in java

    EXACTLY...!!!! exactly........any idea how to do tht..??
    I used to have an object array for it in c++... well im zero abt it in java....please help me out..

  19. #18
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: how to access array f objects in java

    The best way is to use collection, a List for example.

  20. #19
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: problem with accesing the classes with objects

    Quote Originally Posted by Fanta Hubert View Post
    and how do I use code tags and full stack trace? ... this is my coding...
    Many useful tips can be found on the Announcements page, including help on the use of code tags.
    Quote Originally Posted by Fanta Hubert View Post
    at the line where ive used "obj[I].professor" its shows me a java.lang.nullpointer error....
    Always include the full text of the error message with the code and questions when you post. This is for speed of service on your behalf, and a huge time saver on the other end. The message will give clues to the location and a variable name. Figure out why that variable does not have a valid value when it is being accessed.
    Quote Originally Posted by Fanta Hubert View Post
    can sumone please teme wat is the other way for doing this??
    Are you asking someone to tell you what the other way is? If so please use full words. Storage is cheap these days, those extra letters are okay, please include them.
    ...The other way to do what exactly?

    --- Update ---

    Please do not double post your question.
    Threads merged

Similar Threads

  1. Replies: 1
    Last Post: April 26th, 2013, 02:38 AM
  2. java computer science array access issues
    By dylanoo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2013, 07:43 PM
  3. Array of objects
    By spark in forum Object Oriented Programming
    Replies: 2
    Last Post: August 1st, 2012, 03:06 PM
  4. all objects in array the same?
    By abrohm in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 17th, 2011, 11:21 AM
  5. How can i store ArrayList objects in Access database
    By frankycool in forum JDBC & Databases
    Replies: 0
    Last Post: November 3rd, 2009, 11:44 PM