Results 1 to 19 of 19
  1. #1
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default 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 08:58 AM. Reason: Taking up too much space!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Its generating an Array Index Out of Bounds exception.
    Copy and post the complete runtime message. Also indicate which line of your code it is referring to.

  3. #3
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    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//
    }}}}

  4. #4
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    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:

    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++;}
                    }
                }
            }
    runtime error message:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at neigh1.actionPerformed(neigh1.java:208)
    etc
    Last edited by samanyu; 06-03-2011 at 09:00 AM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at neigh1.actionPerformed(neigh1.java:208)
    This is saying that the error is occuring at line 208. Which is, in fact, this line:

    Java Code:
    exch.setarray();
    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:
    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//
    }
    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.

  6. #6
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    The doubt i have is, I have used
    Java Code:
    public neigh2 exch = null;
    to initialize the exch variable.
    But i don't know if i can use the
    Java Code:
    exch= new neigh2( m, n)
    , because if you take a look at the constructor for class neigh2, it is:
    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;    
        }
    Which I think might reinitialize my array. Will it?
    How can I check that? What other means of circumventing this do I have?

  7. #7
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    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?

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The line

    Java Code:
    public neigh2 exch = null;
    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).

    Which I think might reinitialize my array. Will it?
    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.)

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    if the above code doesn't change the values stored inside the array, its fine by me.
    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.

  10. #10
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    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?

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

    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//
            }
        }
    }
    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.

  12. #12
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    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.

  13. #13
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    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.

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You're welcome - I'm glad you've got it sorted out. The program looks interesting.

  15. #15
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    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?

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by samanyu View Post
    if I need to access variables stored in a sub class from the parent class,
    Stop right there. A parent class should never need to know anything about it's subclasses. If you think your parent class does then you have a flaw in your design.

  17. #17
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    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.

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by samanyu View Post
    Its getting way too long and complicated for comfort. Soon I'll be posting asking for help to shorten it. :)
    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.

    Quote Originally Posted by samanyu View Post
    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?
    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)

  19. #19
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 01-31-2011, 09:20 PM
  2. Array index out of bounds error
    By blackstyle18 in forum New To Java
    Replies: 3
    Last Post: 12-28-2010, 03:37 AM
  3. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 05:02 PM
  4. Array Index Out Of Bounds and Problem in Assigning Values
    By chronoz1300 in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 08:14 PM
  5. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 06:11 AM

Tags for this Thread

Posting Permissions

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