-
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
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:
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
-
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.
-
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
-
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.
-
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
-
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
-
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.
-
Re: nested class/method
thanks, thats good to know, and yeah i figured it out.