Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Null Pointer Exception

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Null Pointer Exception

    Well I just started coding in Java with seperate files and classes, and I believe this is an organization problem;

    SOOOO I have 3 files:
    All have 1 class and methods.

    Here's one
    public class runprogram {
     
    public static void main(String[] args) {
     
    runprogram run = new runprogram();
    run.runprogram();
    }
    public void runprogram(){
    GetDataMethods f = new GetDataMethods();
    Cell cell = new Cell();
    cell.assignvalues();
    System.out.println(cell.cellarray[4].value);
    }
    }
     
     
    }

    Here's the other
    public class Cell {
    int value;
    int xloc;
    int yloc;
    int possiblevalues[];
    Cell[] cellarray = new Cell[81];
    //Prints out values of Cell
    public void assignvalues(){
    	GetDataMethods g = new GetDataMethods();
    	Cell one = new Cell();
    		int currentnumber = 0;
    		for (int row = 0; row <= 8; row++)
    		{
    		     for(int col = 0; col <= 8; col++)
    		     {
    		    	 int x = 0;
    		    	 currentnumber = g.newboard[row][col];
    		    	  one.cellarray[x] = new Cell();
    		         System.out.println(one.cellarray[x].value=currentnumber);
    		         x++;
     
    		     	}
    		     }
    		}
    	 }

    And here's my last one
    This one has alot more methods I posted only the relevant part of it.


    public class GetDataMethods {
    	int emptycells[];
    	//The Sudoku
        int[][] newboard = {{0,0,0,0,6,0,0,0,9},
                            {0,6,0,0,0,0,3,0,1},
                            {2,0,0,0,0,9,8,0,0},
                            {1,9,6,0,0,2,0,0,8},
                            {0,0,0,3,0,8,0,0,0},
                            {8,0,0,9,0,0,1,7,5},
                            {0,0,1,7,0,0,0,0,4},
                            {6,0,7,0,0,0,0,8,0},
                            {4,0,0,0,9,0,0,0,0}};
    //The Possible Values
    public int possibilityboard[][];
    int value;
    }
    That's all dandy, however, I get a null pointer exception error if I try using the method assignvalues from Cell class, the file Cell, out side of file Cel.
    Any idea what I'm doing wrong?

    Basically, why do I get a nullpointer in runprogram?
    Last edited by MysticDeath; October 23rd, 2009 at 02:00 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Null Pointer Exception

    Not sure if this is the exact solution, but in the loops within assignvalues(), you are always assigning a value to one.cellarray[0] (you reset x to zero with every look). So if you ever try to access one of the other memebers of the cellarray array - such as you are in runprogram - you will get a null pointer exception. Try moving int x = 0 outside the for loops
    Last edited by copeg; October 23rd, 2009 at 03:31 PM.

  3. #3
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer Exception

    Never mind I solved it;
    Remove the decleration of Cell in the file were the objects are created, then simply in the runprogram declare a new instance of the class, and call the method assignvalues.
    Last edited by MysticDeath; October 24th, 2009 at 02:11 PM.

Similar Threads

  1. NullPointerException:null problem
    By derky in forum Exceptions
    Replies: 8
    Last Post: September 18th, 2009, 03:06 PM
  2. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM
  3. Error of "ClassNotFound Exception"
    By multicoder in forum Exceptions
    Replies: 3
    Last Post: July 1st, 2009, 02:58 PM
  4. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM