Results 1 to 4 of 4
  1. #1
    Calaminh is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default What am I doing wrong? java.lang.NullPointerException

    Error:

    java.lang.NullPointerException
    at SaccoGrid.get(SaccoGrid.java:82)
    at SaccoGrid.get(SaccoGrid.java:9)
    at info.gridworld.actor.Actor.putSelfInGrid(Actor.jav a:121)
    at info.gridworld.actor.ActorWorld.add(ActorWorld.jav a:79)
    at SaccoGridTester.main(SaccoGridTester.java:14)

    I looked at the lines where it said to check, but I don't see anything wrong with it.




    Java Code:
    import info.gridworld.grid.*;
    
    import info.gridworld.actor.*;
    
    import java.util.ArrayList;
    
     
    
    public class SaccoGrid implements Grid<Actor>
    
    {
    
        private Actor[][] myArr;
    
       
    
        //Constructor:  sets up myArr to be a 20X20 2D array
    
        public SaccoGrid()
    
        {
            
            int[][] myArr = new int[20][20];
    
        }
    
       
    
        //Constructor: sets up myArr to have r rows and c columns
    
        public SaccoGrid(int r, int c)
    
        {
    
            int[][] myArr = new int[r][c]; 
    
        }
    
       
    
        public boolean isValid(Location loc)
    
        {
    
            if(myArr[loc.getRow()][loc.getCol()] == null)
            {
                return false;
            }
            
            else
            {
                return true;
            }
    
        }
    
     
    
        public int getNumRows()
    
        {
    
            return myArr.length;
    
        }
    
     
    
        public int getNumCols()
    
        {
            
            return myArr[0].length;
            
        }
    
     
    
        public Actor get(Location loc) 
        { 
            
            if(myArr[loc.getRow()][loc.getCol()] == null) 
            {
                return null; 
            } 
           
            else 
            {
                return myArr[loc.getRow()][loc.getCol()]; 
            }
            
        } 
    
     
    
        public Actor put(Location loc, Actor act)
    
        {
            
            Actor tmpActor = myArr[loc.getRow()][loc.getCol()];
            myArr[loc.getRow()][loc.getCol()] = act;
            
            return tmpActor;   //dummy statement
    
        }
    
     
    
        public Actor remove(Location loc)
    
        {
            
            tmpActor = myArr[loc.getRow()][loc.getCol()];
            myArr[loc.getRow()][loc.getCol()] = null;
            
            return tmpActor;   //dummy statement
    
        }
    
     
    
        public ArrayList<Location> getOccupiedLocations()
    
        {
            ArrayList<Location> tmpArrayList = new ArrayList<Location>();
            
            
            for(int r = 0; r<= myArr.length; r++)
            {
                for(int c = 0; c<= myArr[0].length; c++)
                {
                    
                    if(myArr[r][c] != null)
                    {
                        tmpArrayList.add(new Location(r, c));
                    }
                    
                }
            }
    
            return tmpArrayList;   //dummy statement
    
        }
    
     
    
        public ArrayList<Location> getOccupiedAdjacentLocations(Location loc)
    
        {
    
            ArrayList<Location> tmpArrayList = new ArrayList<Location>();
            
            
            for(int r = loc.getRow()-1; r<=loc.getRow()+1;r++)
            {
                for(int c =loc.getCol()-1;c<=loc.getCol()+1; c++)
                {
                    Location tmp = new Location(r, c);
                    
                    if(myArr[r][c] != null)
                    {
                        if(isValid(tmp) && !tmp.equals(loc))
                        {
                            tmpArrayList.add(new Location(r, c));
                        }
                    }
                    
                }
                
            }
    
            return tmpArrayList;   //dummy statement
    
        }
    
     
    
        public ArrayList<Location> getEmptyAdjacentLocations(Location loc)
    
        {
            ArrayList<Location> tmpArrayList = new ArrayList<Location>();
            
            
            for(int r = loc.getRow()-1; r<=loc.getRow()+1;r++)
            {
                for(int c =loc.getCol()-1;c<=loc.getCol()+1; c++)
                {
                    Location tmp = new Location(r, c);
                    
                    if(myArr[r][c] == null)
                    {
                        if(isValid(tmp) && !tmp.equals(loc))
                        {
                            tmpArrayList.add(new Location(r, c));
                        }
                    }
                    
                }
                
            }
    
            return tmpArrayList;   //dummy statement
            
        }
    
     
    
        public ArrayList<Location> getValidAdjacentLocations(Location loc)
    
        {
             
            ArrayList<Location> toReturn = new ArrayList<Location>();
             
             for( int r = loc.getRow()-1; r<=loc.getRow()+1;r++)
             {
                 for( int c =loc.getCol()-1;c<=loc.getCol()+1; c++)
                 {
                     
                     Location l = new Location(r,c);
                     if( isValid(l)&& !l.equals(loc))
                     {
                         toReturn.add(l);
                     }
                 }
             }
             
             return toReturn;
             
        }
       
    
     
    
        public ArrayList<Actor> getNeighbors(Location loc)
    
        {
    
            ArrayList<Actor> tmpArrayList = new ArrayList<Actor>();
            
            
            for(int r = 0; r<= myArr.length; r++)
            {
                for(int c = 0; c<= myArr[0].length; c++)
                {
                    
                    if(myArr[r][c] != null)
                    {
                        tmpArrayList.add(myArr[r][c]);
                    }
                    
                }
            }
    
            return tmpArrayList;   //dummy statement
    
        }
    
    }

    Runner:
    Java Code:
    import info.gridworld.actor.*; 
    import info.gridworld.grid.*; 
    public class SaccoGridTester 
    {  
        public static void main(String[] args)    
        {      
            SaccoGrid sGrid = new SaccoGrid();
    
            
            SaccoGrid s = new SaccoGrid(12,52); 
            //testing second constructor          
            ActorWorld world = new ActorWorld(sGrid);         
            //Using world to add to the grid         
            world.add(new Location(4,4), new Bug());        
            world.add(new Location(5,5), new Bug());         
            world.add(new Location(6,6), new Bug());          
            //adding to the Grid directly         
            sGrid.put(new Location(6,5), new Rock());          
            System.out.println("The total occupied Locations are: "+sGrid.getOccupiedLocations());                   
            Location fromLoc = new Location(5,4);          
            System.out.println("The valid adjacent Locations to "+fromLoc+" are: "+sGrid.getValidAdjacentLocations(fromLoc));         
            System.out.println("The empty adjacent Locations "+fromLoc+" are: "+sGrid.getEmptyAdjacentLocations(fromLoc));          
            System.out.println("The occupied Locations next to "+fromLoc+" are: "+sGrid.getOccupiedAdjacentLocations(fromLoc));          
            System.out.println("The Actors at the Locations next to "+fromLoc+" are: "+sGrid.getNeighbors(fromLoc));          
            world.show();     
        } 
    }
    I know that java.lang.NullPointerException means that nothing is being called upon, but from line 82, it's calling myArr, so what am I doing wrong? Ty.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: What am I doing wrong? java.lang.NullPointerException

    You are re-declaring myArr inside of your constructors, so this variable is only visible inside of the constructors:

    Java Code:
        private Actor[][] myArr;
     
        
     
        //Constructor:  sets up myArr to be a 20X20 2D array
     
        public SaccoGrid()
     
        {
             
            int[][] myArr = new int[20][20];
     
        }
    The solution is to not re-declare the array in the constructor.

    Also, I'm confused. In the constructor myArr is a 2-dimensional array of int, while in the class it's a 2-dimensional array of Actor objects. What is it really?

  3. #3
    Calaminh is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: What am I doing wrong? java.lang.NullPointerException

    Quote Originally Posted by Fubarable View Post
    You are re-declaring myArr inside of your constructors, so this variable is only visible inside of the constructors:

    Java Code:
        private Actor[][] myArr;
     
        
     
        //Constructor:  sets up myArr to be a 20X20 2D array
     
        public SaccoGrid()
     
        {
             
            int[][] myArr = new int[20][20];
     
        }
    The solution is to not re-declare the array in the constructor.

    Also, I'm confused. In the constructor myArr is a 2-dimensional array of int, while in the class it's a 2-dimensional array of Actor objects. What is it really?

    Thanks for replying Fubarable. They are meant to be an array of actors and not ints. But can you explain to me why re-declaring is erroneous? My teacher gave us a template of constructors and methods to fill out, so if he gave us two constructors to fill in, then shouldn't the instance field of myArr be initialized by the constructors? But from your solution, declaring myArr is wrong? Ty.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: What am I doing wrong? java.lang.NullPointerException

    Yes, you can initialize the 2-D array in the constructor, just don't re-declare it there. A variable is only visible from within the scope that it was declared in, and you want to initialize the variable that was declared in the class.

Similar Threads

  1. java.lang.NullPointerException
    By alaa in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 07:21 PM
  2. java.lang.NullPointerException
    By Travis in forum AWT / Swing
    Replies: 7
    Last Post: 09-18-2009, 06:49 PM
  3. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 01:36 PM
  4. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 2
    Last Post: 02-27-2009, 11:11 AM
  5. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 07:02 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
  •