Results 1 to 3 of 3
  1. #1
    JanVH is offline Member
    Join Date
    May 2011
    Location
    Ghent, Belgium
    Posts
    2
    Rep Power
    0

    Default Problem: NullPointerException

    Hello, this is my first thread here, hope it is in the correct subforum..
    For school we have to create some sort of MMORPG (a simple version of course).
    So the code picks up the data for the gameboard from the Database and builds a 2D array of boxes. Each box is empty, filled with an obstacle or filled with an apponent. This all works out really well until I get to the GUI.

    I want to transform the 2D array of Boxes to an 2D array of Strings (each box has an image to load from the database).

    Java Code:
        public static void main(String[] args) throws IOException {
        	
        	Vak[][] spelbordA = toonBordVak();
        	String[][] spelbordArrayImg;
        	spelbordArrayImg = new String[spelbordA.length][spelbordA[0].length];	
    		for (int i = 0; i<spelbordA.length; i++) {
        		for (int j=0; j< spelbordA[i].length; j++) {
        			if (spelbordA[i][j].getObstakelType().equals("muur"))
        				spelbordArrayImg[i][j] = "img/muur.jpg";
        			else if (spelbordA[i][j].getObstakelType().equals(null))
        				spelbordArrayImg[i][j] = "img/leeg.jpg";
        			else if (spelbordA[i][j].getObstakelType().equals("combi"))
        				spelbordArrayImg[i][j] = "img/politie.jpg";
        			else if (spelbordA[i][j].getPersonage().getCategorie().getOmschrijving().equals("Gent"))
        				spelbordArrayImg[i][j] = "img/gent.jpg";
        			else if (spelbordA[i][j].getPersonage().getCategorie().getOmschrijving().equals("Anderlecht"))
        				spelbordArrayImg[i][j] = "img/anderlecht.jpg";
        			else if (spelbordA[i][j].getPersonage().getCategorie().getOmschrijving().equals("Brugge"))
        				spelbordArrayImg[i][j] = "img/brugge.jpg";
        			else if (spelbordA[i][j].getIsEinde() == true)
        				spelbordArrayImg[i][j] = "img/stadion.jpg";
        			else if (spelbordA[i][j].getObstakelType().equals("brand"))
        				spelbordArrayImg[i][j] = "img/vuur.jpg";
        		else spelbordArrayImg[i][j] = "img/leeg.jpg";
    
        		}
    
    		}
    So the method toonBordVak() creates a correct 2D array of boxes ("Vak" in Dutch), but when I run the code, it keeps getting a NullPointerException when using the .equals() method...
    The spelbordA[i][j] I have checked and the 2D array is filled with all sorts of obstacles, but something goes wrong with the transformation to a 2D strings of arrays..

    Anybody knows a solution for this error?
    Thanks in advance!
    Last edited by JanVH; 05-06-2011 at 12:07 PM.

  2. #2
    JanVH is offline Member
    Join Date
    May 2011
    Location
    Ghent, Belgium
    Posts
    2
    Rep Power
    0

    Default

    For the people who should care, I found the solution. It was quite simple.
    Had to check each time if the method didn't gave "null".

    if (spelbordA[i][j].getObstakelType().equals("muur"))
    had to be
    if (spelbordA[i][j].getObstakelType != null && spelbordA[i][j].getObstakelType().equals("muur"))

  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi if you saw in API to class Object#equals you would find that this method can check value on null too, i.e. you can use follows constructions
    Java Code:
    private static final YOUR_CONS = "muur";
    if (YOUR_CONS.equals(value)) {
    //to make somthings
    }
    instead check on null.
    Skype: petrarsentev
    http://TrackStudio.com

Similar Threads

  1. problem with nullPointerException
    By Bat0u89 in forum New To Java
    Replies: 3
    Last Post: 03-22-2010, 05:25 PM
  2. NullPointerException() problem
    By mollybaba4 in forum New To Java
    Replies: 1
    Last Post: 02-02-2010, 04:25 PM
  3. NullPointerException problem
    By Kris in forum New To Java
    Replies: 4
    Last Post: 10-01-2009, 03:34 PM
  4. nullPointerException problem
    By conandor in forum Networking
    Replies: 1
    Last Post: 08-14-2007, 02:22 PM
  5. NullPointerException problem
    By warship in forum AWT / Swing
    Replies: 5
    Last Post: 08-10-2007, 05:43 PM

Posting Permissions

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