Results 1 to 16 of 16
  1. #1
    Guen is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default method not working out side of class when all others do

    Java Code:
    public check {
    
    int[][] test = new int[3][3];
    
    public int getVal(int col, int row) {
         return test[col][row]
    }
    }
    The above works fine in the class, but not out. All other methods relating to the array work fine outside of the class.
    Java Code:
    public class2 {
    check test;
    int Number = test.getVal(2,2)
    }
    }
    Returns null pointer exception.

    Why? It works inside the class and I can add to the array etc from another class using setVal(int col, int row) method.

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

    Default

    Do you mean "public class check {"? In that case a better name would be Check. (Also Number should be number)

    Perhaps in the code that throws the NullPointerException you are declaring test to be of type check. But not assigning any value to it. That will give a NPE when you attempt to dereference test by saying "test.getVal(2, 2)".

    -----------------

    But this is only a guess: the code as posted isn't very coherent. Perhaps you could post a small runnable example that illustrates the problem (SSCCE)

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    i think test needs to be instantiated otherwise it will remain null,

    i.e.
    check test;

    should be:
    check test = new check();

    on top of that, you will also get a nullpointer if the array in the class check doesn't have any values set to it.

    this is all assuming that the code you posted was typed quickly and in your real code you've declared the check class properly i.e. public class check {

  4. #4
    Guen is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    This was example code as I cannot post real code. Yes public class, sorry. And I know those var names are not very good, I was typing quickly. Im also aware I missed out semicolons. It was just a rough idea of my code.

    Also in another class I do use = new check();, but it still doesnt work. I dont want a new object in the code above so (also according to my tutor) = new check(); is wrong. I tried that and it makes a new state/object.

    the array does have vaules added further down.
    Last edited by Guen; 04-23-2011 at 12:24 PM.

  5. #5
    Guen is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    OK, so it works with = new check();

    But I dont want to create a new object. All my classes work on the same object including array, if I create a 'new' check I get a copy without the values I have added from another class. What is an alternative to = new check? The set methods work with just check test; ??

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Guen View Post
    OK, so it works with = new check();

    But I dont want to create a new object. All my classes work on the same object including array, if I create a 'new' check I get a copy without the values I have added from another class. What is an alternative to = new check? The set methods work with just check test; ??
    I think there's something you don't understand: for the expression o.f() where o is an object of any type and f() is any method defined for the type of o, you are saying f() to an object o, i.e. there has to be an object o to which you can say f(). If you don't want to create a new object o (which is understandable) you have to find another (already existing) object o, i.e. you have to pass such an object to that something that wants to say f() to the object. That 'something' is an object of the 'other class' you mentioned.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Guen is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks, this is my problem. I do not know how to send the object into the class without creating a new instance of it. Could someone please post code as an example?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Guen View Post
    Thanks, this is my problem. I do not know how to send the object into the class without creating a new instance of it. Could someone please post code as an example?
    There has to be at least one object (from whatever class) that has to 'own' or 'know' about this object. You should build a small get() method in that class that passes the object around so other objects (from other classes) can 'ask' for this object. Think about those objects a persons: if you have a piece of cake and I want some I have to ask for it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Guen is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    I have to follow a template and I cannot add a get method. If I did im not sure what I need to send in it though?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Guen View Post
    I have to follow a template and I cannot add a get method. If I did im not sure what I need to send in it though?
    Can you please clarify then what is your current problem, your current "template" and restrictions, and all information necessary to allow us to help you.

  11. #11
    Guen is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    template has a method to set a value in an array which is in the initial class, but used from outside the class. Other irrelevant methods that affect the array, such as findLine. These all work. the method to return the value from the array (as above) only works inside the class. I need to call it from outside the class (as above). I do not want to create a new instance of the class (= new check();).

    sorry i cannot post code

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Guen View Post
    template has a method to set a value in an array which is in the initial class, but used from outside the class. Other irrelevant methods that affect the array, such as findLine. These all work. the method to return the value from the array (as above) only works inside the class. I need to call it from outside the class (as above). I do not want to create a new instance of the class (= new check();).

    sorry i cannot post code
    You will need to call the method on an instance of the class, period. How you get this instance will depend a lot on how your program is structured and your current code. Unfortunately the quality of our answers will depend directly on the quality of information that you supply us, and I don't know how much help will be able to give though given the limited information you're allowed to give us. Perhaps you can create a small compilable and runnable example program that doesn't have the exact code but that demonstrates your problem.

  13. #13
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Guen, you can instantiate the class without 'creating' a new object... just use it as a temporary variable so it gets garbaged once you're done with it...

    Java Code:
    public int getValue() {
        check test = new check();
        int Number = test.getVal(2,2);
        return Number;
    }

    test becomes null once Number is returned

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ozzyman View Post
    Guen, you can instantiate the class without 'creating' a new object... just use it as a temporary variable so it gets garbaged once you're done with it...

    Java Code:
    public int getValue() {
        check test = new check();
        int Number = test.getVal(2,2);
        return Number;
    }

    test becomes null once Number is returned
    I don't see how this will help since you'll be getting the state of an object that doesn't really mean anything.

  15. #15
    Guen is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    getValue is the method name I need to use. I want it to return the vaule at the point (col, row) in the array stored in check. The method returns the correct value in the class, but gets a null pointer outside the class. I only want one vaule and am not allowed the whole array to be exported directly.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Guen View Post
    getValue is the method name I need to use. I want it to return the vaule at the point (col, row) in the array stored in check. The method returns the correct value in the class, but gets a null pointer outside the class. I only want one vaule and am not allowed the whole array to be exported directly.
    Wouldn't it be nice if you showed us some more of your code; now we're just guessing while you say that you're not allowed do that etc.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. ugh another method that isn't working
    By jjth39347 in forum New To Java
    Replies: 14
    Last Post: 04-17-2011, 09:57 PM
  2. Why isn't my toString method working
    By arri3oi in forum New To Java
    Replies: 7
    Last Post: 12-13-2010, 05:34 AM
  3. dispose() method not working
    By R&R in forum New To Java
    Replies: 19
    Last Post: 11-15-2010, 01:46 AM
  4. makeButton method not working
    By ljk8950 in forum AWT / Swing
    Replies: 8
    Last Post: 08-10-2008, 10:20 PM
  5. Deploy Jar Side By Side
    By Adrian in forum Eclipse
    Replies: 0
    Last Post: 04-11-2008, 02:09 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
  •