Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default More trouble with classes and arrays

    Okay, I got this file:
    globals.java:
    Java Code:
    public class globals {
    	static public frame frames[] = new frame[maxFramesAllowed];		// Holds the grid data, maximum of 1000 frames right now
    	
    	static public class frame {
    		public cell cells[] = new cell[maxCellsAllowed];
    		public char nextFreeCell = 0;
    		public char delay = 1;
    		
    		public frame() {
    			// nothing
    		}
    		
    		public frame(int frameDelay) {
    			delay = (char) frameDelay;
    		}
    		
    		public frame(char frameDelay) {
    			delay = frameDelay;
    		}
    	}
    	
    	static public class cell {
    		public char x;
    		public char y;
    		public char value;
    		
    		public cell(int xVal, int yVal, int val) {
    			x = (char) xVal;
    			y = (char) yVal;
    			value = (char) val;
    		}
    		
    		public cell(char xVal, char yVal, char val) {
    			x = xVal;
    			y = yVal;
    			value = val;
    		}
    	}
    }
    When I ran it with the regular portion of my program, I got errors about null pointers/objects.

    So iIcreated this program to test the class:
    TEST_globals.java
    Java Code:
    public class TEST_globals {
    	public globals g;
    	
    	public static void main(String argv[]) {
    		for (int i = 0; i < 5; i++) {
    			globals.frames[i] = new globals.frame(i * 2);
    			System.out.println("Delay for frame[" + i + "]: " + globals.frames[i].delay);
    			
    			for (int j = 0; j < 5; j++) {
    				System.out.println("\tFor frame[" + i + "], next free cell at[" + globals.frames[i].nextFreeCell + "]");
    				globals.frames[i].cells[globals.frames[i].nextFreeCell] = new globals.cell(j, j, i * j);
    				globals.frames[i].nextFreeCell++;
    			}
    		}
    	}
    }
    The result:


    Please help. :(
    Last edited by CuddlyKittens11; 04-26-2011 at 03:55 AM. Reason: Tag fixing
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Before I read and respond I just wanted to point out that you forgot the [ code] part of your first snippet.

    Also, I am not really sure what you are asking, would you mind clarifying your question for me?
    Last edited by sunde887; 04-26-2011 at 03:57 AM.

  3. #3
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    What I want is for my data, when I access it, not to turn up as a "null", as it is doing right now.
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Others may have better advice, but are you adding information to the cells?

  5. #5
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    Yeah, I am going to, but I'm trying to fix the issue with the null data first.
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you try to access an array element before you fill it you will get a null pointer exception. If you create the array and don't fill the array and try to do anything with the items in the array, it will cause a null pointer exception. Can you show me the code that generate the null pointer exception and the stack trace?

  7. #7
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    You mean this, I also have declared in "globals.java":
    Java Code:
    static public int maxFramesAllowed = 10;    // Small right now for simplicity
    static public frame frames[] = new frame[maxFramesAllowed];
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    A stack trace looks something like this
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
            at TestArray.main(testarray.java:5)
    And lets us see what causes the problems. Is the above the code that was giving you null errors?

    Normally when you get a null error it's cause by trying to do something on an object that hasn't been instantiated. Here is a small example I created which causes a NullPointerException

    Java Code:
    public class TestArray{
    	public static void main(String[] args){
    		Object[] x = new Object[5];
    		for(int i = 0; i < x.length; ++i){
    			System.out.println(x[i].toString());
    		}
    	}
    }
    To avoid null pointers you need to fill the array. In the above example you would do something like this

    Java Code:
    public class TestArray{
    	public static void main(String[] args){
    		Object[] x = new Object[5];
                    for(int i = 0; i < x.length; ++i){
                            x[i] = new Object();
                    }
    		for(int i = 0; i < x.length; ++i){
    			System.out.println(x[i].toString());
    		}
    	}
    }
    From the fact that you are receiving null pointer exceptions, I am assuming you are attempting to call methods on an array with items that have not been initialized. Like I said, this is an assumption.

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Quote Originally Posted by CuddlyKittens11 View Post
    You mean this, I also have declared in "globals.java":
    Java Code:
    static public int maxFramesAllowed = 10;    // Small right now for simplicity
    static public frame frames[] = new frame[maxFramesAllowed];
    So what you have there is an array of 10 references to frame objects. Each one of those references is null. You have to fill the array, like this:

    Java Code:
    for(int i = 0; i < frames.length; ++i)
         frames[i] = new frame(...);
    BTW... it'll be very helpful to you later, as well as helpful to anyone reading your code, if you adopt the standard Java conventions for naming and capitalization.

  10. #10
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    Hey guys, I figured out my problem, it had something to do with the char datatypes I was using.

    Thanks though.
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

Similar Threads

  1. Trouble with arrays and classes
    By CuddlyKittens11 in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2011, 01:42 AM
  2. I'm having trouble with extending classes
    By ziongio in forum New To Java
    Replies: 2
    Last Post: 03-15-2011, 08:33 AM
  3. Having trouble with Classes
    By Skyton in forum Eclipse
    Replies: 7
    Last Post: 03-04-2011, 02:33 PM
  4. Arrays trouble
    By gto400no1 in forum New To Java
    Replies: 1
    Last Post: 04-14-2010, 02:20 AM
  5. HELP: Still having trouble getting arrays :(
    By Psyclone in forum New To Java
    Replies: 4
    Last Post: 02-06-2010, 02:05 AM

Posting Permissions

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