• Post Reply Bookmark Topic Watch Topic
  • New Topic

Referencing Java classes  RSS feed

 
n kav
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all! I'm still suffering with some problems of referencing Java classes, if somebody can help me to solve it.

I have three classes. Code looks like this...

In first class, MainFrame.java, I have action defined on jButton as:

private void btnContactActionPerformed(java.awt.event.ActionEvent evt) {
......
final ContactList conlst = new ContactList(mysql.mc); // this have to be declared as final
......

ContactUpdate conUpd = new ThirdClass(...);
...

}



In ContactList.java I have

public void fill() {
System.out.println("Does this work?");
}


And finally, ContactUpdate.java contains:
...
ContactList clst;
...
try {
clst.fill()
} catch (Exception ex) {
ex.printStackTrace();
}


I'm getting error NullPointerException all the time, and I guess that I'm still have some beginers problem with class referencing.

Can you help me please? Thank you in advance!
[ March 13, 2007: Message edited by: n kav ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your ContactUpdate class, I see that you declare a Contact list object:


and then you try to use it:

but have you initialized it first w/ a call to its constructor? i.e.,


I'm also confused about your use of the "final" keyword. Isn't this supposed to be in class definition statements, not in object creation and instantiation statements? I could be wrong, I'm a beginner myself.

Finally, please read the forum FAQ's about using code tags in y our posts. They make your code much easier to read.

/Pete
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your variable "conlist" is a local variable which is invoked or used inside what appears to be an anonymous inner class; inner class methods don't like it when there is a possibility of a local variable being re-assigned while they are using it, so they insist on them being declared final. The requirement about "final" applies both to local variables and arguments/parameters, but not to fields.

Pete Stein is correct about forgetting to instantiate an object as the likeliest cause of a NullPointerException. A very frequent mistake. You aren't creating it inside the ActionListener, are you? If you are, then you have created it is a local variable; as soon as control passes outside that inner class' methods the variable will go out of scope and vanish. Another possible cause for NullPointerException.
Don't go declaring the same variable twice.
Remember the numbers in the stack trace will tell you where your Exception occurred; this should allow you to guess which Object you haven't got in existence yet.

Why have you added an actionPerformed method of that name to the JButton? I presume you are using it as part of an ActionListener anonymous inner class? It ought only to have one method, which looks like this:-
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Does clst ever get instantiated?
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm also confused about your use of the "final" keyword. Isn't this supposed to be in class definition statements, not in object creation and instantiation statements?


Using final in this context makes that reference a "constant". The actual object state can be changed if it is mutable, but the reference value can not be.

An example: private final ArrayList<String> strList;

Once strList is instantiated, it can never reference a different ArrayList object, but the contents of the ArrayList can change.
[ March 13, 2007: Message edited by: David McCombs ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David McCombs:


Using final in this context makes that reference a "constant". The actual object state can be changed if it is mutable, but the reference value can not be.

God, I love this site. Thanks.
/Pete
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!