Results 1 to 7 of 7
  1. #1
    XZ001 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default NullPointerException

    I know similar questions have probably been asked about NullPointerException before, and I have in fact read some, but they don't seem to be helping. Here's part of the code I'm using:

    Java Code:
    public void LoadLevel(String s) throws Exception {
    		int x, y, w, h, r;
    		Wall wall;
    		BufferedReader saveFile = new BufferedReader(new FileReader(s));
    		r = Integer.parseInt(saveFile.readLine());
    		while (r > 0){
    			x = Integer.parseInt(saveFile.readLine());
    			y = Integer.parseInt(saveFile.readLine());
    			w = Integer.parseInt(saveFile.readLine());
    			h = Integer.parseInt(saveFile.readLine());
    			wall = new Wall(x, y, w, h);
    			Walls.add(wall);
    			r -= 1;
    		}
    	}
    This code is meant to read a text document and use the data in it to create walls in a game. I already have the Wall class created and that part works fine, but when I run the script it give me a NullPointerException on this line:

    Java Code:
    Walls.add(wall);
    From what I learned from reading other posts on this topic I know that it's trying to say that "wall" has a null value, but it is defined on the line directly above it. Can someone clarify what I need to do?

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

    Default Re: NullPointerException

    I believe it is Walls that has the null value. Where have you declared and initialised this List?

  3. #3
    XZ001 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: NullPointerException

    Oh sorry. Walls was defined, but I forgot to post that part of the code. It's defined earlier by this:

    Java Code:
    private List<Wall> Walls;

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: NullPointerException

    yes but did you create it?

    walls = new List<Wall>();

  5. #5
    XZ001 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: NullPointerException

    I did not create it, but I just tried using that same code and it gave me the error: Cannot instantiate the type List<Wall>

  6. #6
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: NullPointerException

    put arrayList<Wall>(); that should work, you can create List, since List is an interface.

    My List example was just there saying you can use a data type you wish, ArrayList is what id go for here.

  7. #7
    XZ001 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

Similar Threads

  1. NullPointerException help
    By laichunkitjacky in forum New To Java
    Replies: 4
    Last Post: 12-04-2011, 05:20 PM
  2. NullPointerexception
    By danewithoutwax in forum New To Java
    Replies: 4
    Last Post: 11-26-2011, 12:51 PM
  3. I keep getting NullPointerException
    By coasterguy10 in forum New To Java
    Replies: 6
    Last Post: 09-19-2011, 05:53 PM
  4. NullPointerException
    By mjz in forum JDBC
    Replies: 1
    Last Post: 08-06-2009, 11:46 AM
  5. NullPointerException
    By tommyyyy in forum New To Java
    Replies: 9
    Last Post: 03-26-2009, 10:51 PM

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
  •