Results 1 to 16 of 16
- 04-22-2011, 11:52 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
method not working out side of class when all others do
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 check { int[][] test = new int[3][3]; public int getVal(int col, int row) { return test[col][row] } }
Returns null pointer exception.Java Code:public class2 { check test; int Number = test.getVal(2,2) } }
Why? It works inside the class and I can add to the array etc from another class using setVal(int col, int row) method.
- 04-23-2011, 12:10 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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)
-
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 {
- 04-23-2011, 12:21 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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.
- 04-23-2011, 12:37 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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; ??
- 04-23-2011, 01:05 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-23-2011, 02:06 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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?
- 04-23-2011, 02:14 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-23-2011, 07:34 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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?
-
- 04-23-2011, 07:43 PM #11
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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.
-
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
-
- 04-24-2011, 11:46 AM #15
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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.
- 04-24-2011, 11:49 AM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
ugh another method that isn't working
By jjth39347 in forum New To JavaReplies: 14Last Post: 04-17-2011, 09:57 PM -
Why isn't my toString method working
By arri3oi in forum New To JavaReplies: 7Last Post: 12-13-2010, 05:34 AM -
dispose() method not working
By R&R in forum New To JavaReplies: 19Last Post: 11-15-2010, 01:46 AM -
makeButton method not working
By ljk8950 in forum AWT / SwingReplies: 8Last Post: 08-10-2008, 10:20 PM -
Deploy Jar Side By Side
By Adrian in forum EclipseReplies: 0Last Post: 04-11-2008, 02:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks