File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Objects and Instance Variables Confusion... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects and Instance Variables Confusion..." Watch "Objects and Instance Variables Confusion..." New topic
Author

Objects and Instance Variables Confusion...

Lachlan Hope
Greenhorn

Joined: Oct 03, 2010
Posts: 2
Hi all,

I have a situation that I think might be one of those things that should be obvious, but for some reason isn't clicking.

I am writing a small Java program.

In it, I have a class that, in its instance variables, uses objects from another class, so:

public class myclass1 {

private myobject mo

where 'myobject' is an object of a type defined in a different class.

within this class there are two methods.

The first actually creates the objects:

void method1(){

myobject mo = new myobject();

}

Then the second needs to update these objects, so:

void method2(){

mo.setvariable1("randomstring");
}

However, when method2 gets called, it gives a nullPointer Exception.

I am assuming this is something to do with the fact that while the objects are declared as instance variables, their own instance variables don't exist yet, or can't be accessed or something, but I can't quite figure it out...

How could this be put together so that both method1 and method2 could access the objects in question? This seems to work when using Swing components, like if method2 were also to call a get() method on a JTextArea defined in method1 this would work, but on my own, created objects there is an error..

Apologies for the long post, appreciate any help!








Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121



I think this is your scenario , so first call method1() to create the object and than call method2()
Else it will give a null pointer exception
also the reference myobject mo is a instance variable so it will be assigned null automatically, if it does not refer to any object
and Also
you have used -- myobject mo = new myobject();
after private myobject mo
so no need of reassigning mo to type myobject
you can directly use mo = new myobject();

Hope this clears your Doubt

Please try to use the code you have written next time with code tags so it is easy to understand whats happening.


Understanding is Everything - Peter Lord
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

And Welcome to Java Ranch
Lachlan Hope
Greenhorn

Joined: Oct 03, 2010
Posts: 2
Thanks I'll give that a shot!

And thanks for the welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Useful suggestion, but please note there are all sorts of stylistic problems about names of methods, capital letters, etc., in that code.
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

yes
I would correct that
use setVariable instead of setvariable
and class name would also start with capital letter.
Random and Myobject
These things are very important . Thank you Campbell for bringing this to notice.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
It's not Myobject, is it?
Stephan Mueller
Ranch Hand

Joined: May 05, 2010
Posts: 50
Lachlan Hope wrote:
public class myclass1 {
private myobject mo

where 'myobject' is an object of a type defined in a different class.
within this class there are two methods.
The first actually creates the objects:

void method1(){
myobject mo = new myobject();
}

Then the second needs to update these objects, so:

void method2(){
mo.setvariable1("randomstring");
}


Also
you have used -- myobject mo = new myobject();
after private myobject mo
so no need of reassigning mo to type myobject
you can directly use mo = new myobject();


Based on this scenario, the second quote misinterprets the problem. It's not that you are reassigning something but he problem is that you never have an non-null-initialized instance variable "mo".
Your method1() just creates a local object (local to method1()) which is discarded after the method is left. You want
"mo = new myobject()" instead of "myobject mo = new myobject()".


1. Make it run - 2. Make it run correctly - 3. Make it pretty OR fast/small - 4. ??? - 5. Profit
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

Campbell Ritchie wrote:It's not Myobject, is it?

Ok it will be MyObject
I took that as a single word , which was incorrect
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

yes Stephan Mueller
I implicitly meant the same thing
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Pratik D mehta wrote: . . . it will be MyObject . . .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects and Instance Variables Confusion...