The Artima Developer Community
Sponsored Link

Java Answers Forum
Why do i get a nullpointer exception here

4 replies on 1 page. Most recent reply: Aug 20, 2003 6:11 AM by Draganka Tusheva

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Draganka Tusheva

Posts: 8
Nickname: tusheva
Registered: Aug, 2003

Why do i get a nullpointer exception here Posted: Aug 19, 2003 6:15 AM
Reply to this message Reply
Advertisement
Unfortunatelly I didn`t got any answer yet for my previous posted topic...
Anyway I wanted to put my code now because I have done so many stupid things to see why my programm throws a null pointer exception and still I can not see why..
Here it comes

import java.util.*;

/**
*This class will make the hash table.The key that the hash table uses will be
*the sourceID of the relation.And the value will be a vector which consists of
*the relations that have the same sourceID as the so given relation.
*the program gives a nullpointer exception.(...)
* @author tusheva
*/
public class AnnotationThread {
private Vector v;
private Hashtable hashtable;
/** Creates a new instance of AnnotationThread
Relation is interface.there are classes that implement the interface so that an instance of one of them will be a parameter for the constructor. */
public AnnotationThread(Relation r) {
String key = r.getSourceID(); // just a function to get a field with name SourceID that belongs to every class.
Vector v = new Vector();
Hashtable hashtable = new Hashtable();
hashtable.put(key,v);

}

public Hashtable createHashtable(Relation r){
String key1 = r.getSourceID();
// Now here comes the code where I try to avoid the nullpointer exception.
Vector v = new Vector();
v.add("lialia") ;

if (this.v.isEmpty()) {
v.add(r);
this.hashtable.put(key1,v);
}
else {
Vector v1=(Vector) hashtable.get(key1);
this.v.add(v1);
this.hashtable.put(key1,v);
}
return this.hashtable;
}

// The main program.Analogy is one of the classes that implement Relation...I am not actually sure what can I do with System.out,println() in this case but at least I don`t get it why i am stuck with this exception....
public static void main (String args[]){
Analogy a = new Analogy(0.1f,0.2f,"a","b");
AnnotationThread example = new AnnotationThread(a);
Hashtable show = new Hashtable ();
show = example.createHashtable(a);
System.out.println(show.get(a.getSourceID()));
}

}


I will be very glad if i get some help with this
Sincerely


Greg Lehane

Posts: 33
Nickname: glehane
Registered: Jun, 2003

Re: Why do i get a nullpointer exception here Posted: Aug 19, 2003 10:29 AM
Reply to this message Reply
Draganka, you should put the java code tags around any source you post. Otherwise, your posting is quite unreadable.

There is a couple of things going on here. First, your are declaring your variables more than once, and your scoping seems to be a little messy. You have

private Vector v;
 
// some code...
 
... void someMethod() {
  
  Vector v = new Vector();
  this.v.getX();
  
}
// should be
 
... void someMethod() {
  
  v = new Vector();
  this.v.getX();
  
}
 
// or
... void someMethod() {
  
  Vector v = new Vector();
  v.getX();
  
}


By declaring a new object (Vector) within the scope of a method, it's only visible to that method. So either omit this.* or don't declare a new vector with the same name.

You're also passing "Analogy" objects to methods that are expecting a "Relation" object. Not sure what's going on here.

Make sure to look up a tutorial on scope.

Hope this helps,

=- Greg

Draganka Tusheva

Posts: 8
Nickname: tusheva
Registered: Aug, 2003

Re: Why do i get a nullpointer exception here Posted: Aug 20, 2003 5:47 AM
Reply to this message Reply
Yes after that i just omitted this and it was fine... I did the declaration of the vector more than once just to make sure that I won,t get the nullpointer exception... Thank you and I will read something about scoping..

Draganka Tusheva

Posts: 8
Nickname: tusheva
Registered: Aug, 2003

Greg... Posted: Aug 20, 2003 6:08 AM
Reply to this message Reply
And to all the others who are willing to post opinion...
So here is the program again this time I don*t think that it is too messy...

import java.util.*;

/**
*This class will make the hash table.The key that the hash table uses will be
*the sourceID of the relation.And the value will be a vector which consists of
*the relations that have the same sourceID as the so given relation.
*the program gives a nullpointer exception.(...)
* @author tusheva
*/
public class AnnotationThread {
private Vector v;
private Hashtable hashtable;
/** Creates a new instance of AnnotationThread
*Just to mention that Relation is interface that is
*implemented by the class Analogy so that when I call
*the makeHashtable() methos in main I call it with an
*instance of Analogy that implements Relation so
*that is not a problem I hope..
*/

public AnnotationThread(Relation r) {
Vector v = new Vector();
Hashtable hashtable = new Hashtable();
}

public Hashtable createHashtable(Relation r){
String key1 = r.getSourceID();
if (v.isEmpty()) {
v.add(r);
hashtable.put(key1,v);
}
else {
Vector v1=(Vector) hashtable.get(key1);
v.add(v1);
hashtable.put(key1, v);
}
return hashtable;

}


public static void main (String args[]){
Analogy a = new Analogy(0.1f,0.2f,"a","b");
AnnotationThread example = new AnnotationThread(a);
System.out.println(example.createHashtable(a));
}

}



And maybe it will be useful to attach the Analogy class...

/**
*One of the structure relations named Analogy.
* @author tusheva
*/
public class Analogy implements Relation {
/**The fields of the instances of the class Analogy*/
private float sourceWeight;
private float destinationWeight;
private static final String RELATIONTYPE = "Analogy";
private String sourceID;
private String destinationID;

/** Creates a new instance of Analogy*/
public Analogy(float sourceWeight, float destinationWeight,String sourceID,String destinationID) {
this.sourceWeight = sourceWeight;
this.destinationWeight = destinationWeight;
this.sourceID=sourceID;
this.destinationID=destinationID;
all.add(this);

}

/**
* Calculates the weight for the source and destination node (according to
* its relation type)
* @return the weight
*/
public float calculateWeight(){
float weight;
weight=this.sourceWeight+this.destinationWeight-this.sourceWeight*this.destinat ionWeight;
return weight;
}


/**
* Sets the source weight.
* @param sourceWeight the source weight
*/
public void setSourceWeight(float sourceWeight) {
this.sourceWeight = sourceWeight;
}
/**
* Sets the destination weight.
* @param destinationWeight the destination weight
*/

public void setDestinationWeight(float destinationWeight) {
this.destinationWeight = destinationWeight;
}
/**
* Gets the source weight.
*/
public float getSourceWeight() {
return this.sourceWeight;
}
/**
* Gets the destination weight.
*/
public float getDestinationWeight() {
return this.destinationWeight;
}
/**
* Gets the relationstype.
*/
public String getRelationType() {
return this.RELATIONTYPE;
}

public void setSourceID(String sourceID) {
this.sourceID=sourceID;
}


public void setDestinationID(String destinationID) {
this.destinationID=destinationID;
}


public String getSourceID(){
return this.sourceID;
}


public String getDestinationID(){
return this.destinationID;
}



}


And still I do get this exception ... I mean the nullpointer exception ... After all I do initialise v and hashtable in the constructor... Does this mean that they are not visible for creteHashtable() .. I will really appreciate an answer
Cheers

Draganka Tusheva

Posts: 8
Nickname: tusheva
Registered: Aug, 2003

I get rid of the exception Posted: Aug 20, 2003 6:11 AM
Reply to this message Reply
Yes i think that the problem was with that the methos doen;t seem to see the declaration of the vector and the hashtable .Now that I initialise them before the constructor it seems that everything is ok...
Thanks

Flat View: This topic has 4 replies on 1 page
Topic: java on a web server Previous Topic   Next Topic Topic: To store xml files in database

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use