Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Sierra

Thread: nested class/method

  1. #1
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default nested class/method

    hello,

    Im working on some code right now that uses a few diffren classes and methods. what i would liek to do is create a class that calls methods from other classes.. so for example i call
    Java Code:
    classA.method1();
    classB.method2();
    classB.method3();
    to do something in my code, what i want to do is put these method calls inside another class and method for example:
    Java Code:
    public class classC{
    
    public void method4(){
    classA.method1();
    classB.method2();
    classB.method3();
    }
    
      }
    so that in my main class i can just call to method4() and it does all of these things ? mostly the reason for this is to keep the clutter and redundancy down in the main method .. i guess my question is, can this be done or am i wasting my time and redundancy is the best answer. i can post actual code if that would help but its somewhat of a vague question and i figured i can continue to figure out why it doesnt work as long as its an error in my code and not something that doesnt work by design.

    Thanks
    Russ

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: nested class/method

    Erm... why shouldn't you be able to do it like that? Just try it and ask if it does not work, right? ;)

    BUT... you cannot call it for _classes_ but for objects... You need to create a new object of that class. Well you can but then you need to make the methods static and that is not really advisable.
    I like likes!

  3. #3
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: nested class/method

    i did try and im getting an error, so i just wanted to make sure that im not trying something thats "not allowed" or something before i waste a bunch of time figuring out why it does not work. but from what your saying it should work, which is what i thought, so i guess im going to inspect my code some more.

    Thanks so much for the help
    Russ

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: nested class/method

    I could probably immediate tell you why the error occurred but I need to know what error it is... ^^
    If you have a problem related to an error it is probably best to post the error message and at least the lines of code affected.
    I like likes!

  5. #5
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: nested class/method

    error message is :
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at Deck.getCard(Deck.java:67)
    at DealCard.getCard(DealCard.java:14)
    at BlackJack.main(BlackJack.java:12)

    i know im getting the error message because a null value is being called for the index of the arraylist, i just need to figure out why its null, i will try for a few and post back in a bit if i cant figure it out but i think ill get it, and i like to fix my code on my own if i can because im such a noob to java the experience is usually a good lesson :) i will post back later if i cant figure it out on my own, but thank you for wanting to help.

    Russ

  6. #6
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: nested class/method

    i figured it out, i have an arraylist of objects and what i was neglecting to do is call the method that creates the arraylist so when i was calling methods to use it there was no list to be found.

    Thanks
    Russ

  7. #7
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: nested class/method

    Well to be precise this means that you use an index in an array reference that is not valid - the item is not null it does not even exist, else you would get a NullPointerException. This is a slight difference when looking for the error. I hope you found it.
    Russd772 likes this.
    I like likes!

  8. #8
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: nested class/method

    thanks, thats good to know, and yeah i figured it out.

Similar Threads

  1. inner vs nested class
    By sai_1245 in forum New To Java
    Replies: 1
    Last Post: 01-30-2012, 06:11 AM
  2. Adding custom Jtable to GUI in nested class
    By smallmos1 in forum New To Java
    Replies: 1
    Last Post: 03-16-2011, 01:55 PM
  3. Use a 'nested class' here?
    By jezza10181 in forum New To Java
    Replies: 1
    Last Post: 01-24-2011, 02:15 PM
  4. Nested List class for Simulation Queue
    By Cheddz in forum Advanced Java
    Replies: 2
    Last Post: 02-19-2010, 02:19 AM
  5. Replies: 4
    Last Post: 09-05-2008, 05:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •