Results 1 to 19 of 19
- 06-02-2011, 09:13 AM #1
Member
- Join Date
- Jun 2011
- Location
- Tsukuba, Japan
- Posts
- 63
- Rep Power
- 0
having an array index out of bounds problem
Dear All,
This is my code. Its generating an Array Index Out of Bounds exception. Also, I am worried that it might give a Null Pointer Exception, after the array issue is sorted out.
Any suggestions?
I removed the code, as it was too long. Cheers.Last edited by samanyu; 06-03-2011 at 07:58 AM. Reason: Taking up too much space!
- 06-02-2011, 09:45 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Copy and post the complete runtime message. Also indicate which line of your code it is referring to.Its generating an Array Index Out of Bounds exception.
- 06-02-2011, 09:58 AM #3
Member
- Join Date
- Jun 2011
- Location
- Tsukuba, Japan
- Posts
- 63
- Rep Power
- 0
the runtime message is given below. I don't think the array is necessarily a problem. I only can't seem to call my method,
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at neigh1.actionPerformed(neigh1.java:208)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6288)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:605 3)
at java.awt.Container.processEvent(Container.java:204 1)
at java.awt.Component.dispatchEventImpl(Component.jav a:4651)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Component.dispatchEvent(Component.java:44 81)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:44 81)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 613)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
the problem lies in:
else if(ev.getSource() == neighselButton){
int c=0; a=0;
for( int i=0; i<(810/30); i++) {
for( int j=0; j<(630/30); j++) {
if (exch.neigharr[j][i]==1) {
c++;}}}
set=new int[c][2];
for( int i=0; i<(810/30); i++) {
for( int j=0; j<(630/30); j++) {
if (exch.neigharr[j][i]==1) {
set [a][0]=j;
set [a][1]=i;
a=a+1;
// prompt to save, and call rule1//
}}}}
- 06-02-2011, 10:02 AM #4
Member
- Join Date
- Jun 2011
- Location
- Tsukuba, Japan
- Posts
- 63
- Rep Power
- 0
i made some changes to the program, i have posted the complete code, folowed by where the problem lies, followed by the runtime message.
i think the problem lies here:
runtime error message:Java Code:public void setarray() { int c=0; exch2.a=0; for( int i=0; i<(810/30); i++) { for( int j=0; j<(630/30); j++) { if (neigharr[j][i]==1) { c++;}}} exch2.set=new int[c][2]; for( int i=0; i<(810/30); i++) { for( int j=0; j<(630/30); j++) { if (neigharr[j][i]==1) { exch2.set [exch2.a][0]=j; exch2.set [exch2.a][1]=i; exch2.a++;} } } }
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at neigh1.actionPerformed(neigh1.java:208)
etcLast edited by samanyu; 06-03-2011 at 08:00 AM.
- 06-02-2011, 10:24 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
This is saying that the error is occuring at line 208. Which is, in fact, this line:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at neigh1.actionPerformed(neigh1.java:208)
The NullPointerException message means that you are using a variable as if it had a nonnull value when it is actually null. In this case you are calling a method of exch but, in fact, exch is null. You can check this by adding a bit of debugging code:Java Code:exch.setarray();
So where did you think you had assigned exch a nonnull value? You have to go back to that place and figure out why that didn't happen.Java Code:else if(ev.getSource() == neighselButton){ [color=blue]System.out.println("About to call setarray() on " + exch);[/color] exch.setarray(); // prompt to save, and call rule1// }
- 06-02-2011, 10:35 AM #6
Member
- Join Date
- Jun 2011
- Location
- Tsukuba, Japan
- Posts
- 63
- Rep Power
- 0
The doubt i have is, I have used
to initialize the exch variable.Java Code:public neigh2 exch = null;
But i don't know if i can use the, because if you take a look at the constructor for class neigh2, it is:Java Code:exch= new neigh2( m, n)
Which I think might reinitialize my array. Will it?Java Code:neigh2( int w, int h){ width = w; height = h; neigharr = new int[w][h]; neighbuffer = new int[w][h]; neigharr[13][10] = 25; neighbuffer[13][10]= 25; }
How can I check that? What other means of circumventing this do I have?
- 06-02-2011, 10:37 AM #7
Member
- Join Date
- Jun 2011
- Location
- Tsukuba, Japan
- Posts
- 63
- Rep Power
- 0
if the above code doesn't change the values stored inside the array, its fine by me. But I think it will, should I write a test program separately, or can I use a small command insert into this program?
- 06-02-2011, 10:44 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
The line
is certainly where the variable is declared and initialised. But it is not assigned a nonnull value. On the contrary it is assigned null! If exch is not assigned a nonnull value then or later you will get a NullPointerException (as you've found).Java Code:public neigh2 exch = null;
Yes, the constructor reinitialises (assigns a new value to) two arrays. Is that a problem? Whether it is a problem or not the fact remains that if you want to call methods on the variable exch then you must give that variable a nonnull value and the only way you have provided to do that is to call the neigh2 constructor. (A bad name, by the way. It - the class and the constructor - should be Neigh2 or something more descriptive.)Which I think might reinitialize my array. Will it?
- 06-02-2011, 10:48 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Then it should be fine: because at the time you call the constructor exch is null. In other words there is no array to change at that point. At (at least) one other place you do call the constructor: that will create an entirely new neigh2 object with an entirely new pair of arrays.if the above code doesn't change the values stored inside the array, its fine by me.
- 06-02-2011, 01:25 PM #10
Member
- Join Date
- Jun 2011
- Location
- Tsukuba, Japan
- Posts
- 63
- Rep Power
- 0
i want to be able to access the array in the Neigh2 class from another class, the pre-existing array with values stored inside it. What do you suggest?
Should I create a new array to store it, in a class where the constructor does not change the array?
- 06-02-2011, 10:43 PM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
This is really nothing more than a guess - so I hope I'm not putting you wrong...
Your Neigh2 seems to represent the "state", so it might well be that you should call the constructor once (eg in the Neigh1 constructor) and there after just use its methods to set() clear() etc.
You will get ArrayIndexOutOfBounds exceptions - as you suggested earlier on. If so, think about the i and j loop variables in setarray(). In particular think about how big the first array index can get, and how big the second one can get: if you get them backwards then you will "overrun" the matrix in one direction.Java Code:public class Neigh1 extends JFrame implements ActionListener { // etc public neigh2 exch; public Neigh1(){ exch = new Neigh2(810 / 30, 630 / 30); // etc } // etc public void actionPerformed( ActionEvent ev ){ // etc else if( ev.getSource() == blankItemC1 ){ // reinitialise to all cooperators int n=0; //exch= new neigh2((810/30),(630/30)); exch.blank(n); da1.repaint(); } else if( ev.getSource() == rulselButton) { ruleselect(); } else if(ev.getSource() == neighselButton){ exch.setarray(); // prompt to save, and call rule1// } } }
- 06-03-2011, 01:43 AM #12
Member
- Join Date
- Jun 2011
- Location
- Tsukuba, Japan
- Posts
- 63
- Rep Power
- 0
I did try that, I still got a Null-Pointer exception. But I thought of some other things to try. I will get back to you on this.
- 06-03-2011, 02:06 AM #13
Member
- Join Date
- Jun 2011
- Location
- Tsukuba, Japan
- Posts
- 63
- Rep Power
- 0
Solved it, I think. Your guess worked, I just had to place the command higher than the call to the DisplayArea constructor.
There's an earthquake here in celebration of the problem overcome. Got the array index exception sorted too.
Thanks. You were great.
- 06-03-2011, 07:37 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
You're welcome - I'm glad you've got it sorted out. The program looks interesting.
- 06-03-2011, 07:56 AM #15
Member
- Join Date
- Jun 2011
- Location
- Tsukuba, Japan
- Posts
- 63
- Rep Power
- 0
Its getting way too long and complicated for comfort. Soon I'll be posting asking for help to shorten it. :)
I wanted to ask another question, if I need to access variables stored in a sub class from the parent class, and I use the var-name= new constructor() command, will my existing files be wiped out?
If the parent class is used before the sub class, does it matter? Will it simply initialize before I modify the variable in the sub-class?
- 06-03-2011, 07:58 AM #16
- 06-03-2011, 08:08 AM #17
Member
- Join Date
- Jun 2011
- Location
- Tsukuba, Japan
- Posts
- 63
- Rep Power
- 0
The current design is basically like this, I have a class Works1, which contains the main() method. This class works with class GridChar's variables to display a GUI and a clickable grid. By clicking the grid, we can change the state of the cells. This is stored in an array initialised in the GridChar class.
When you click a button on the main GUI, a class called Neigh1 is called, which contains another grid much like Works1, here, the variables belong to a class called Neigh2.
Neigh1 calls another class Rule1, with the same properties.
Now I need to iterate the arrays I have intialised in GridChar, Neigh2 and Rule2, and to display the iterations on the grid controlled by Work1.
Am I being coherent? What should I do? Should I initialise all the required arrays in GridChar? Will that work? Because then to use those arrays, I might need to keep calling on Grid Char's constructor which will remove all previously stored data.
Suggest something, please.
- 06-03-2011, 08:09 AM #18
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Yes - the different classes should go into different files. And the function of each of the methods described (not what you are going to do with the method but what, objectively, the method does with its input.) The general idea is to deal with the complexity by breaking it up into self contained bits.
Another couple of rules of thumb are to make all of the insrance variables private, and none of the nonfinal variables static.
I'm not sure what you mean by parent class/subclass here. Perhaps a small example would clarify? (It sounds like the sort of question where composing an example could well lead to the answer)
- 06-03-2011, 08:12 AM #19
Member
- Join Date
- Jun 2011
- Location
- Tsukuba, Japan
- Posts
- 63
- Rep Power
- 0
Similar Threads
-
problem with array error index out of bounds exception
By cn23271 in forum New To JavaReplies: 4Last Post: 01-31-2011, 08:20 PM -
Array index out of bounds error
By blackstyle18 in forum New To JavaReplies: 3Last Post: 12-28-2010, 02:37 AM -
array Index out of Bounds exception== 0
By Allgorythm in forum New To JavaReplies: 6Last Post: 02-11-2010, 04:02 PM -
Array Index Out Of Bounds and Problem in Assigning Values
By chronoz1300 in forum New To JavaReplies: 2Last Post: 12-28-2009, 07:14 PM -
Array Index out of bounds
By leapinlizard in forum New To JavaReplies: 5Last Post: 04-29-2009, 05:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks