Results 1 to 10 of 10
- 04-26-2011, 02:40 AM #1
More trouble with classes and arrays
Okay, I got this file:
globals.java:
When I ran it with the regular portion of my program, I got errors about null pointers/objects.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; } } }
So iIcreated this program to test the class:
TEST_globals.java
The result: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++; } } } }

Please help. :(Last edited by CuddlyKittens11; 04-26-2011 at 02:55 AM. Reason: Tag fixing
Good with: C/C++, DarkGDK, PHP, MySQL
Current reading: The Linux Programming Interface
- 04-26-2011, 02:52 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 02:57 AM.
- 04-26-2011, 02:57 AM #3
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
- 04-26-2011, 03:02 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Others may have better advice, but are you adding information to the cells?
- 04-26-2011, 03:03 AM #5
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
- 04-26-2011, 03:08 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 04-26-2011, 04:24 AM #7
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
- 04-26-2011, 04:40 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
A stack trace looks something like this
And lets us see what causes the problems. Is the above the code that was giving you null errors?Java Code:Exception in thread "main" java.lang.NullPointerException at TestArray.main(testarray.java:5)
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
To avoid null pointers you need to fill the array. In the above example you would do something like thisJava 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()); } } }
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.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()); } } }
- 04-26-2011, 07:44 AM #9
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:
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.Java Code:for(int i = 0; i < frames.length; ++i) frames[i] = new frame(...);
- 04-27-2011, 01:25 AM #10
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
-
Trouble with arrays and classes
By CuddlyKittens11 in forum Advanced JavaReplies: 3Last Post: 04-25-2011, 12:42 AM -
I'm having trouble with extending classes
By ziongio in forum New To JavaReplies: 2Last Post: 03-15-2011, 07:33 AM -
Having trouble with Classes
By Skyton in forum EclipseReplies: 7Last Post: 03-04-2011, 01:33 PM -
Arrays trouble
By gto400no1 in forum New To JavaReplies: 1Last Post: 04-14-2010, 01:20 AM -
HELP: Still having trouble getting arrays :(
By Psyclone in forum New To JavaReplies: 4Last Post: 02-06-2010, 01:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks