• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Objects and Instance Variables Confusion...

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!








 
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And Welcome to Java Ranch
 
Lachlan Hope
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I'll give that a shot!

And thanks for the welcome
 
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 121
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not Myobject, is it?
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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()".
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 121
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Stephan Mueller
I implicitly meant the same thing
 
Campbell Ritchie
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pratik D mehta wrote: . . . it will be MyObject . . .

 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic