Results 1 to 12 of 12
  1. #1
    eszeikial is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default Problem with simple int[][] generator

    Java Code:
    import java.util.Random;
    public class world {
    	int[][] map;
    	Random worldgen = new Random();
    	
    	[B]public void createWorld(){
    		for (int row=0;row<5;row++){
    			for(int column=0;column<5;column++){
    				map[row][column] = worldgen.nextInt(2); // The line im getting an error for.
    			}
    		}
    	}[/B]
    	
    	public void showWorld(int x[][]){
    		for (int row=0;row<x.length;row++){
    			for(int column=0;column<x[row].length;column++){
    				System.out.print(x[row][column]+"\t");
    			}
    			System.out.println();
    		}
    	}
    	
    	public int[][] loadWorld(){
    			return map;
    	}
    
    	
    }
    This is my first time posting here, and would appreciate support.
    I'm trying to simply create an array randomly, and i'm not sure why this isn't working. in my main class i created the object for this class, but i get an error when i call p.createWorld();
    Last edited by eszeikial; 07-25-2011 at 03:10 AM.

  2. #2
    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 eszeikial View Post
    // The line im getting an error for.
    We don't read minds. Copy and paste the full and exact error message you get.

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

    Default

    Hi, welcome to the forum.

    A couple of things: when you post code you should put [code] at the start of the code and [/code] at the end: that way it will be readable when it appears here. You can edit your post and add these tags, although you will have to put in the indentation again.

    Also if you have a runtime error it is a good idea to copy and post the whole thing here. I'm sure someone can help you understand what it means.

  4. #4
    eszeikial is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Exception in thread "main" java.lang.NullPointerException
    at world.createWorld(world.java:9) // the code i'm showing.
    at Runner.main(Runner.java:9) // my main class with void main string args....
    Last edited by eszeikial; 07-25-2011 at 03:12 AM.

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

    Default

    Java Code:
    int[][] map;
    All that does is declare a variable called map. Where do you create the 2D array?

  6. #6
    eszeikial is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    I never created it, but I thought that my createWorld method added values to it? I thought that was creating it >_<

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

    Default

    If you want to put some apples into a carboard box does simply putting the apple down make the box magically appear?

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

    Default

    "NullPointerException" means that you are using a variable as if it had a value, but that variable is actually null (ie does not have a value).

    Look at world.java:9

    Java Code:
    [color=red]map[/color][row][column] = [color=red]worldgen[/color].nextInt(2); // The line im getting an error for.
    There are two variables there that could be null. (row and column are OK because they are primitives). There is nothing wrong with worldgen because you give it a value in the line "Random worldgen = new Random();". That only leaves map.

    map has not been given a value, hence you get the error. It is not enough to assign values to the elements of an array: the array variable itself must be given a value. Read your textbook (or something like mindprod.com) to see how a multidimensional array can be declared and initialised. Post back with what you've tried if you have problems.

  9. #9
    eszeikial is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Ah.
    So declaring it isn't enough? I need to create it as well.
    my mistake, I found a page online explaining the difference between declaring something and creating it.

    I understand you're trying to help, but your method of teaching is rather condescending. (edit) -referring to junky, pbrockway is much more noob friendly apparently.
    Last edited by eszeikial; 07-25-2011 at 03:22 AM.

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

    Default

    My style is not condescending. Rather than just give you the answer I spur you into thinking for yourself. If you do nmot like this method then that is your problem not mine.

  11. #11
    eszeikial is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    I suppose you're right in a way, and I do appreciate the help.
    you did make me think on my own instead of just giving me an answer, but your attitude toward me did seem condescending. your signature didn't help either.

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

    Default

    My signature is just a bit of fun. Besides why do you care unless you think you are an idiot and it is aimed at you.

Similar Threads

  1. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  2. A simple problem that i can't fix
    By baf06 in forum New To Java
    Replies: 3
    Last Post: 04-06-2011, 03:50 PM
  3. Very simple problem
    By Andyj in forum Java Certification
    Replies: 3
    Last Post: 09-17-2010, 07:49 AM
  4. Need help with simple problem
    By newb_tewb in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 03:18 AM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 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
  •