Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Creating a 2d array of your own classes in Java?

Options
  • 26-11-2008 3:23am
    #1
    Closed Accounts Posts: 995 ✭✭✭


    Would anyone be able to take a look at this code and tell me why exactly it giving me exceptions?
    public class myClass1{
        public int value;
        public int value2;
    
        public myClass1(int value, int value2){
            this.value = value;
            this.value2 = value2;
        }
    }
    
    
    public class myClass2{
        myClass1[][] array;
    
    
        public myClass2(){
            createArray();
        }
    
        public void createArray(){
            for(int i = 0; i <= 6; i++){
                for(int j = 0; j <= 6; j++){
                    array[j][i] = new myClass1(j,i);
                }
            }
        }
        public myClass1 getObj()
        {
            return array[0][0];
        }
    }
    
    public class myClass3{
    
        myClass2 tempClass = new myClass2();
        myClass1[] temp;
    
    
        //default constructor
    
        public myClass3(){
               getStuff();
        }
    
    
        public void getStuff(){
            for(int i = 0; i <7; i++)
            {
                temp[i] = tempClass.getObj();
            }
        }
    }
    
    
    I'm trying to instantiate a 2D array of myClass1 objects in my Class2. Random elements of the 2D array are meant to be returned to an array of myClass1 in myClass3, but for the sake of Debugging, I have it set to return only the first element.

    I keep getting an "exception in thread "main" java.lang.NullPointerException" when I go to compile it. Saying that it's a problem with the myClass2.createArray(), myClass2().


    Sorry for the wreck the head class names. Can anyone tell me if there's anything with the logic behind this?


    Any help appreciated.


Comments

  • Registered Users Posts: 15,094 ✭✭✭✭javaboy


    It's late and I only gave this a quick glance but I think the problem is you never actually create the array object i.e. you need a line like this:
    myClass1[][] array=new myClass1[6][6];
    

    The way your code is now expects Java to handle a dynamic array which it doesn't really do.


  • Registered Users Posts: 6,240 ✭✭✭hussey


    as javaboy said (except it should be of length 7 as it is <=6
    (this doesn't make sense as class3 uses length 6 .. so which is it OP?
    public void createArray(){
            for(int i = 0; [B]i <= 6[/B]; i++){
                for(int j = 0; [B]j <= 6[/B]; j++){
                    array[j][i] = new myClass1(j,i);
                }
            }
        }
    public void getStuff(){
            for(int i = 0; i <7; i++)
            {
                temp[i] = tempClass.getObj();
            }
        }
    
    


    should be
    public void createArray(){
        	[B]array = new myClass1[7][7];[/B]
            for(int i = 0; i < array.length; i++){
                for(int j = 0; j < array[i].length; j++){
                    array[j][i] = new myClass1(j,i);
                }
            }
        }
    ....
    public void getStuff(){
        	[B]temp = new myClass1[6];[/B]
            for(int i = 0; i <temp.length; i++)
            {
                temp[i] = tempClass.getObj();
            }
        }
    


  • Closed Accounts Posts: 995 ✭✭✭Ass


    hussey wrote: »
    as javaboy said (except it should be of length 7 as it is <=6
    (this doesn't make sense as class3 uses length 6 .. so which is it OP?
    The values are changed slightly from my actual code, but in the code above that shouldn't matter because it's returning the same value no matter what. The position of the array that are being returned are not incrementing in myClass2.


    Anyway, I think I've got my code working now. I'm too tired to continue on at the minute, so I'll get up bright and early to do some testing in the morning.

    Thanks for the help guys.


  • Registered Users Posts: 6,240 ✭✭✭hussey


    Ass Face wrote: »
    The values are changed slightly from my actual code, but in the code above that shouldn't matter because it's returning the same value no matter what. The position of the array that are being returned are not incrementing in myClass2.


    Anyway, I think I've got my code working now. I'm too tired to continue on at the minute, so I'll get up bright and early to do some testing in the morning.

    We helped with the null pointer .. make an attempt at your random stuff using the hint below
    hint Math.Random, at the moment your code will always follow the same pattern
    public myClass1 getObj()
        {
            return array[ <random number between 0- length of array> ][ <random number between 0- length of array>];
        }
    


  • Closed Accounts Posts: 995 ✭✭✭Ass


    Thanks but I have the whole project done now. How ever, it looks like it may all be in vain because looking at what is requested, I may have to go back and do it a different way all together, despite having all of the functionality requested for the program. : (


    Oh well.


  • Advertisement
  • Closed Accounts Posts: 815 ✭✭✭KStaford


    also

    be real careful writing code like
    public int value;
    myClass1[][] array;

    value and array are reserved words in most languages. Even if they dont cause a problem initially, using reserved words will come back to bite you at some stage.

    Use code like

    public int i;
    myClass1[][] myArr;


  • Closed Accounts Posts: 995 ✭✭✭Ass


    Yeah, I actually changed a lot of the code for the sake of demonstrating it here. All of the variable names and class names that I posted are different to my those in my actual code.

    It's an assignment, but for the sake of passing, I don't want to put up my actual code in case google throws him a link to this thread and my lecturer thinks I plagiarised it or something.


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    Way OT here... but do they actually tell you to do this in uni?

    I would have thought 2D-arrays were basically the scope of Fortran and friends, and for Java you should be basically modelling your data as types...

    </philosophical>


  • Closed Accounts Posts: 995 ✭✭✭Ass


    Way OT here... but do they actually tell you to do this in uni?

    I would have thought 2D-arrays were basically the scope of Fortran and friends, and for Java you should be basically modelling your data as types...

    </philosophical>
    The module is Graphical User Interfaces. We're told how the window should function, what it should look like and what it should contain. All of the other mechanics are left for us to decide. We aren't really requested to do it any specific way.

    With a history in C, C++ and C#, this just seemed like the most logical way. I'm not very good with Java. I'm sure there are better ways of doing it using Java to it's greatest potential and I'm sure I'd be able to do it eventually, how ever I'm on a pretty tight deadline here. I've a pretty big project in every week, and have had one in every week for the past month and a half. It's a matter of getting one done as best I can with in the time frame, and on to the next. Lately I've been spending about 11 - 12 hours a day in college programming for the last while. While most of it all is pretty good fun, it gets a bit tiring after a while especially when you have to work on weekends.

    Hard work was never easy though.


Advertisement