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 4 of 4

Thread: method not working outside of class

  1. #1
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default method not working outside of class

    I have a 2D-array in a class, and methods that affect the array.

    From another class I can call a method to add to the array which works fine.

    My method to get a value from an array is:

     
    public int getValue(int col, int row){
     
    		return myArray[col][row];
    	}

    I can call it in that class and it works fine (returns correct int), but from another class I always get a null pointer exception. All other methods work from another class (ie to set a value in the array). Why cant I return the value?

    int Number = class.getValue(); -- null pointer.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: method not working outside of class

    Sounds like your reference to "class" is null. Are you sure you're initializing it?

    That's just a guess though. If you want more help, you'll have to provide an SSCCE that demonstrates the problem. That doesn't mean you should post your whole program- just the bare essentials.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: method not working outside of class

    I can use other methods, for example

    state.setArray(2);

    Which puts a value in row 2 and then prints the array. These work fine.
    It is definitely initialised. I cannot post code.

    I have tried

    public int getValue(int col, int row) {
    int Number = myArray[col][row];
    return Number;
    }
    state state;
    state.getValue(1,2);
     
    also
     
    state teststate = new state();
    teststate.getvaule(1,2);
     
    for a copy of state

    But I get the same error. I dont see why that would work if the other one didnt.
    Last edited by Scotty; April 22nd, 2011 at 02:57 PM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: method not working outside of class

    If you can't post an SSCCE, then we can't really help you. Are you sure the array is initialized?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. contains method not working?
    By DudeJericho in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2011, 01:57 PM
  2. Override class method
    By mekie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 07:06 PM
  3. Getting a string value from within a method in another class
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2010, 04:17 PM
  4. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM